生活随笔
收集整理的這篇文章主要介紹了
Linux编程练习 --多线程5--信号量(semaphore)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這一篇練習信號量的應用
信號量本質上是一個非負的整數計數器,也是UNIX中古老的實現進程互斥和同步的手段,Linux下信號量概念是在線程中,信號則在進程控制中,不過原理差不多,最基本最經典的操作莫過于P、V操作了,能實現進程、線程的互斥和同步操作,非常值得深入理解。
1.P、V操作原語
?P操作:
?proceduce P(var s:semaphore)
{
? begin
????? s:=s-1;
????? if(s<0)then W(s);
? end
}
?
V操作:
?proceduce V(var s:semaphore)
{
? begin
????? s:=s+1;
????? if(s<=0)then R(s);
? end
}?
?
2.基本操作
數據類型:信號量的數據類型為結構sem_t,它本質上是一個長整型的數。
函數:
(1)sem_init
功能:???????? 用于創建一個信號量,并初始化信號量的值。
頭文件:?????? <semaphore.h>
函數原型:???? int sem_init (sem_t* sem, int pshared, unsigned int value);
函數傳入值:?? sem:信號量。
?????????????????? pshared:決定信號量能否在幾個進程間共享。由于目前LINUX還沒有實現進
?????????????????????????????? 程間共享信息量,所以這個值只能取0。
?????????????????? value:初始計算器
函數返回值:?? 0:成功。
?????????????????? -1:失敗。
(2)其他函數。
//等待信號量
int sem_wait?????? (sem_t* sem);
int sem_trywait?? (sem_t* sem);
//發送信號量
int sem_post?????? (sem_t* sem);
//得到信號量值
int sem_getvalue (sem_t* sem);
//刪除信號量
int sem_destroy?? (sem_t* sem);
功能:sem_wait和sem_trywait相當于P操作,它們都能將信號量的值減一,兩者的區別在
??????? 于若信號量的值小于零時,sem_wait將會阻塞進程,而sem_trywait則會立即返回。
??????? sem_post相當于V操作,它將信號量的值加一,同時發出喚醒的信號給等待的進程
?????? (或線程)。
??????? sem_getvalue 得到信號量的值。
??????? sem_destroy 摧毀信號量。
函數傳入值: sem:信號量。
函數返回值: 同上。
好了,了解完基本操作,繼續做一個練習:
這里用信號量實現互斥資源訪問的功能:
?? #include?<stdlib.h>???? #include?<stdio.h>???? #include?<pthread.h>???? #include?<semaphore.h>?? #include?<errno.h>????? ?? int?gnum?=?0;?? ?? sem_t?sem;?? ?? static?void?pthread_func_1?(void);????? static?void?pthread_func_2?(void);????? ???? int?main?(void)????? {????? ??? ??pthread_t?pt_1?=?0;????? ??pthread_t?pt_2?=?0;????? ??int?ret?=?0;????? ???? ??sem_init(&sem,0,1);?? ???? ??ret?=?pthread_create?(&pt_1,???????????? ?????????????????????????NULL,???????????? ????????????????????????(void?*)pthread_func_1,?? ????????????????????????NULL);???????????? ??if?(ret?!=?0)????? ??{????? ?????perror?("pthread_1_create");????? ??}????? ???? ??ret?=?pthread_create?(&pt_2,???????????? ????????????????????????NULL,????????????? ????????????????????????(void?*)pthread_func_2,??? ????????????????????????NULL);???????????? ??if?(ret?!=?0)????? ??{????? ?????perror?("pthread_2_create");????? ??}????? ???? ??pthread_join?(pt_1,?NULL);????? ??pthread_join?(pt_2,?NULL);????? ???? ??printf?("main?programme?exit!/n");???? ??return?0;????? }????? ?? static?void?pthread_func_1?(void)????? {????? ??int?i?=?0;????? ??????? ??for?(;;)????? ??{????? ????printf?("This?is?pthread1!/n");?????? ????sem_wait(&sem);?????????? ????sleep?(1);??? ?????? ????gnum++;?? ????printf?("Thread1?add?one?to?num:%d/n",gnum);?? ?????? ????sem_post?(&sem);?????????? ?? ??????? ??}????? }????? ??? static?void?pthread_func_2?(void)????? {????? ??int?i?=?0;????? ???? ??for?(;;)????? ??{????? ????printf?("This?is?pthread2!/n");??? ????sem_wait(&sem);??????? ????sleep?(1);?? ?????? ????gnum++;?? ????printf?("Thread2?add?one?to?num:%d/n",gnum);?? ?????? ????sem_post?(&sem);?????????? ???? ??}????? ???? ??pthread_exit?(0);????? }????
編譯,運行,可以看出和上次互斥鎖結果一樣的
總結
以上是生活随笔為你收集整理的Linux编程练习 --多线程5--信号量(semaphore)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。