日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Linux下的sem_wait等待函數(shù)簡介(linuxsemwait)

在Linux操作系統(tǒng)中,進(jìn)程間同步和互斥的機(jī)制有許多種,其中最常用的是信號量(Semaphore)。信號量是由Dijkstra首次引入的一種系統(tǒng)結(jié)構(gòu),其本質(zhì)是一把鎖,用于保護(hù)共享資源。在經(jīng)典的生產(chǎn)者-消費(fèi)者問題中,生產(chǎn)者和消費(fèi)者進(jìn)程之間需要共享一個緩沖區(qū),因此需要使用信號量來保證緩沖區(qū)的正確性。

而在使用信號量時,一個非常重要的函數(shù)是sem_wt函數(shù)。這個函數(shù)的作用是阻塞當(dāng)前進(jìn)程,直到某一個信號量的值變得大于0。而當(dāng)一個進(jìn)程對信號量執(zhí)行減操作時,如果信號量的值為0,那么該進(jìn)程就會被阻塞,直到另外一個進(jìn)程對信號量執(zhí)行加操作將其值變?yōu)榉?。

sem_wt的函數(shù)原型如下:

int sem_wt(sem_t *sem);

其中sem_t是一個信號量類型,表示一個信號量的實例,*sem則是具體的信號量變量。sem_wt函數(shù)的返回值為0表示函數(shù)執(zhí)行成功;否則,就表示函數(shù)執(zhí)行失敗。

在實際使用時,sem_wt函數(shù)常常與sem_post函數(shù)配合使用。sem_post函數(shù)是另外一個信號量函數(shù),用于增加信號量的值。通過調(diào)用sem_post函數(shù),我們可以將一個被阻塞的進(jìn)程喚醒,從而使其繼續(xù)執(zhí)行。注意:sem_wt和sem_post函數(shù)只能用于已經(jīng)初始化的信號量上,否則將出現(xiàn)系統(tǒng)錯誤。

一般情況下,sem_wt函數(shù)是在臨界區(qū)中調(diào)用的。在許多并發(fā)程序中,臨界區(qū)是程序中需要互斥訪問的臨界資源,在進(jìn)入臨界區(qū)之前,應(yīng)該對臨界區(qū)進(jìn)行加鎖,當(dāng)退出臨界區(qū)時,再釋放鎖。sem_wt函數(shù)可以保證臨界區(qū)不會同時被多個進(jìn)程占用,從而保證臨界區(qū)的數(shù)據(jù)正確性和一致性。

在信號量的應(yīng)用中,sem_wt函數(shù)被廣泛地用于等待共享資源的可用性。舉例來說,假設(shè)有10個進(jìn)程需要訪問一個共享變量,但是這個變量只能同時被一個進(jìn)程訪問。這時,我們就可以使用一個信號量來實現(xiàn)對共享變量的互斥訪問控制。當(dāng)一個進(jìn)程訪問共享變量之前,需要獲取信號量的內(nèi)部資源,如果相應(yīng)的信號量取得資源的量為0,則進(jìn)程需要一直等待,直到資源可用。

sem_wt函數(shù)是Linux環(huán)境下實現(xiàn)進(jìn)程同步和互斥機(jī)制的重要函數(shù)之一。通過使用這個函數(shù),我們可以更加精確地控制進(jìn)程的執(zhí)行順序和訪問共享資源的安全性。對于需要進(jìn)行并發(fā)編程的開發(fā)者來說,熟練掌握sem_wt函數(shù)的使用方法,是非常關(guān)鍵的一環(huán)。

相關(guān)問題拓展閱讀:

  • linux 信號量問題 編譯錯誤 好像不識別sem_t定義的變量
  • Linux多進(jìn)程和線程同步的幾種方式

linux 信號量問題 編譯錯誤 好像不識別sem_t定義的變量

幫你修改了一手搭下,編譯運(yùn)行沒問題,修改的地方都標(biāo)出來了,

由于不知道你程序的功能襪薯老,所以沒有對你的程序邏輯進(jìn)行分析

#include

#include

#include

#include

//–以下是修改的部分

sem_t in;

sem_t out;

sem_t handout;

sem_t handin;

sem_t goout;

//–

int counter=0;

void * studentIn(void *a)

{

sem_wait(&in);//修改

counter++;

printf(“%d\n”,counter);

if(counter==30)

{

sem_post(&handout);//告升修改

return NULL;

}

sem_post(&in);//修改

return NULL;

}

void * fteacherhandout(void *b)

{

sem_wait(&handout);//修改

printf(“teacher said:hand out over\n”);

sem_post(&handin);//修改

return NULL;

}

void * studentout(void *c)

{

sem_wait(&handin);//修改

sem_wait(&out);//修改

counter–;

printf(“%d\n”,counter);

if(counter==0)

{

sem_post(&goout);//修改

return NULL;

}

sem_post(&out);//修改

}

void * fteacherout(void *d)

{

sem_wait(&goout);//修改

printf(“teacher go out”);

return NULL;

}

void main()

{

int i=0;

//–以下是修改的部分

sem_init(&in,0,1);

sem_init(&out,0,1);

sem_init(&handin,0,0);

sem_init(&handout,0,0);

sem_init(&goout,0,0);

//

pthread_t thread1,thread2,teacher1,teacher2;

pthread_attr_t attr;

pthread_attr_init(&attr);

for(i=0;i

{

pthread_create(&thread1,&attr,studentIn,NULL);

}

for(i=0;i

{

pthread_create(&thread2,&attr,studentout,NULL);

}

pthread_create(&teacher1,&attr,fteacherhandout,NULL);

pthread_create(&teacher2,&attr,fteacherout,NULL);

return;

Linux多進(jìn)程和線程同步的幾種方式

Linux 線程同步的橘螞笑三種方法

線程的更大特點(diǎn)是資源的共享性,但資源共享中的同步問題是多線程編程的難點(diǎn)。linux下提供了多種方式來處理線程同步,最常用的是互斥鎖、條件變量和信號量。

一、互斥鎖(mutex)

通過鎖機(jī)制實現(xiàn)線程間的同步。

初始化鎖。在Linux下,線程的互斥量數(shù)據(jù)類型是pthread_mutex_t。在使用前,要對它進(jìn)行初始化。

靜態(tài)分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

動態(tài)分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);

加鎖。對共享資源的訪問,要對互斥量進(jìn)行加鎖,如果互斥量已經(jīng)上了鎖,調(diào)用線程會阻塞,直到互斥量被解鎖。

int pthread_mutex_lock(pthread_mutex *mutex);

int pthread_mutex_trylock(pthread_mutex_t *mutex);

解鎖。在完成了對共享資源的訪問后,要對互斥量進(jìn)行解鎖。

int pthread_mutex_unlock(pthread_mutex_t *mutex);

銷毀鎖。鎖在是使用完成后,需要進(jìn)行銷毀以釋放資源。

int pthread_mutex_destroy(pthread_mutex *mutex);

view plain copy

#include

#include

#include

#include

#include “iostream”

using namespace std;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int tmp;

void* thread(void *arg)

{

cout

#include

#include “stdlib.h”

#include “unistd.h”

pthread_mutex_t mutex;

pthread_cond_t cond;

void hander(void *arg)

{

free(arg);

(void)pthread_mutex_unlock(&mutex);

}

void *thread1(void *arg)

{

pthread_cleanup_push(hander, &mutex);

while(1)

{

printf(“thread1 is running\n”);

pthread_mutex_lock(&mutex);

pthread_cond_wait(&cond, &mutex);

printf(“thread1 applied the condition\n”);

pthread_mutex_unlock(&mutex);

sleep(4);

}

pthread_cleanup_pop(0);

}

void *thread2(void *arg)

{

while(1)

{

printf(“thread2 is running\n”);

pthread_mutex_lock(&mutex);

pthread_cond_wait(&cond, &mutex);

printf(“thread2 applied the condition\n”);

pthread_mutex_unlock(&mutex);

sleep(1);

}

}

int main()

{

pthread_t thid1,thid2;

printf(“condition variable study!\n”);

pthread_mutex_init(&mutex, NULL);

pthread_cond_init(&cond, NULL);

pthread_create(&thid1, NULL, thread1, NULL);

pthread_create(&thid2, NULL, thread2, NULL);

sleep(1);

do

{

pthread_cond_signal(&cond);

}while(1);

sleep(20);

pthread_exit(0);

return 0;

}

view plain copy

#include

#include

#include “stdio.h”

#include “stdlib.h”

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

struct node

{

int n_number;

struct node *n_next;

}*head = NULL;

static void cleanup_handler(void *arg)

{

printf(“Cleanup handler of second thread./n”);

free(arg);

(void)pthread_mutex_unlock(&mtx);

}

static void *thread_func(void *arg)

{

struct node *p = NULL;

pthread_cleanup_push(cleanup_handler, p);

while (1)

{

//這個mutex主要是用來保證pthread_cond_wait的并發(fā)性

pthread_mutex_lock(&mtx);

while (head == NULL)

{

//這個while要特別說明一下,單個pthread_cond_wait功能很完善,為何

//這里要有一個while (head == NULL)呢?因為pthread_cond_wait里的線

//程可能會被意外喚醒,如果這個時候head != NULL,則不是我們想要的情況。

//這個時候,應(yīng)該讓線程繼續(xù)進(jìn)入pthread_cond_wait

// pthread_cond_wait會先解除之前的pthread_mutex_lock鎖定的mtx,

//然后阻塞在等待對列里休眠,直到再次被喚醒(大多數(shù)情況下是等待的條件成立

//而被喚醒,喚醒后,該進(jìn)程會先鎖定先pthread_mutex_lock(&mtx);,再讀取資源

//用這個流程是比較清楚的

pthread_cond_wait(&cond, &mtx);

p = head;

head = head->n_next;

printf(“Got %d from front of queue/n”, p->n_number);

free(p);

}

pthread_mutex_unlock(&mtx); //臨界區(qū)數(shù)據(jù)操作完畢,釋放互斥鎖

}

pthread_cleanup_pop(0);

return 0;

}

int main(void)

{

pthread_t tid;

int i;

struct node *p;

//子線程會一直等待資源,類似生產(chǎn)者和消費(fèi)者,但是這里的消費(fèi)者可以是多個消費(fèi)者,而

//不僅僅支持普通的單個消費(fèi)者,這個模型雖然簡單,但是很強(qiáng)大

pthread_create(&tid, NULL, thread_func, NULL);

sleep(1);

for (i = 0; i n_number = i;

pthread_mutex_lock(&mtx); //需要操作head這個臨界資源,先加鎖,

p->n_next = head;

head = p;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&mtx); //解鎖

sleep(1);

}

printf(“thread 1 wanna end the line.So cancel thread 2./n”);

//關(guān)于pthread_cancel,有一點(diǎn)額外的說明,它是從外部終止子線程,子線程會在最近的取消點(diǎn),退出

//線程,而在我們的代碼里,最近的取消點(diǎn)肯定就是pthread_cond_wait()了。

pthread_cancel(tid);

pthread_join(tid, NULL);

printf(“All done — exiting/n”);

return 0;

}

三、信號量(sem)

如同進(jìn)程一樣,線程也可以通過信號量來實現(xiàn)通信,雖然是輕量級的。信號量函數(shù)的名字都以”sem_”打頭。線程使用的基本信號量函數(shù)有四個。

信號量初始化。

int sem_init (sem_t *sem , int pshared, unsigned int value);

這是對由sem指定的信號量進(jìn)行初始化,設(shè)置好它的共享選項(linux 只支持為0,即表示它是當(dāng)前進(jìn)程的局部信號量),然后給它一個初始值VALUE。

等待信號量。給信號量減1,然后等待直到信號量的值大于0。

int sem_wait(sem_t *sem);

釋放信號量。信號量值加1。并通知其他等待線程。

int sem_post(sem_t *sem);

銷毀信號量。我們用完信號量后都它進(jìn)行清理。歸還占有的一切資源。

int sem_destroy(sem_t *sem);

view plain copy

#include

#include

#include

#include

#include

#include

#define return_if_fail(p) if((p) == 0){printf (“:func error!/n”, __func__);return;}

typedef struct _PrivInfo

{

sem_t s1;

sem_t s2;

time_t end_time;

}PrivInfo;

static void info_init (PrivInfo* thiz);

static void info_destroy (PrivInfo* thiz);

static void* pthread_func_1 (PrivInfo* thiz);

static void* pthread_func_2 (PrivInfo* thiz);

int main (int argc, char** argv)

{

pthread_t pt_1 = 0;

pthread_t pt_2 = 0;

int ret = 0;

PrivInfo* thiz = NULL;

thiz = (PrivInfo* )malloc (sizeof (PrivInfo));

if (thiz == NULL)

{

printf (“: Failed to malloc priv./n”);

return -1;

}

info_init (thiz);

ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);

if (ret != 0)

{

perror (“pthread_1_create:”);

}

ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);

if (ret != 0)

{

perror (“pthread_2_create:”);

}

pthread_join (pt_1, NULL);

pthread_join (pt_2, NULL);

info_destroy (thiz);

return 0;

}

static void info_init (PrivInfo* thiz)

{

return_if_fail (thiz != NULL);

thiz->end_time = time(NULL) + 10;

sem_init (&thiz->s1, 0, 1);

sem_init (&thiz->s2, 0, 0);

return;

}

static void info_destroy (PrivInfo* thiz)

{

return_if_fail (thiz != NULL);

sem_destroy (&thiz->s1);

sem_destroy (&thiz->s2);

free (thiz);

thiz = NULL;

return;

}

static void* pthread_func_1 (PrivInfo* thiz)

{

return_if_fail(thiz != NULL);

while (time(NULL) end_time)

{

sem_wait (&thiz->s2);

printf (“pthread1: pthread1 get the lock./n”);

sem_post (&thiz->s1);

printf (“pthread1: pthread1 unlock/n”);

sleep (1);

}

return;

}

static void* pthread_func_2 (PrivInfo* thiz)

{

return_if_fail (thiz != NULL);

while (time (NULL) end_time)

{

sem_wait (&thiz->s1);

printf (“pthread2: pthread2 get the unlock./n”);

sem_post (&thiz->s2);

printf (“pthread2: pthread2 unlock./n”);

sleep (1);

}

return;

}

香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。


標(biāo)題名稱:Linux下的sem_wait等待函數(shù)簡介(linuxsemwait)
當(dāng)前路徑:http://www.5511xx.com/article/cdishse.html