sem_wait sem_post信號量操做進本函數

sem_wait   sem_post

信號量的數據類型爲結構sem_t,它本質上是一個長整型的數。函數sem_init()用來初始化一個信號量。它的原型爲:  

extern int sem_init __P ((sem_t *__sem, int __pshared, unsigned int __value));  函數

sem爲指向信號量結構的一個指針;pshared不爲0時此信號量在進程間共享,不然只能爲當前進程的全部線程共享;value給出了信號量的初始值。  post

函數sem_post( sem_t *sem )用來增長信號量的值。當有線程阻塞在這個信號量上時,調用這個函數會使其中的一個線程不在阻塞,選擇機制一樣是由線程的調度策略決定的。  測試

函數sem_wait( sem_t *sem )被用來阻塞當前線程直到信號量sem的值大於0,解除阻塞後將sem的值減一,代表公共資源經使用後減小。函數sem_trywait ( sem_t *sem )是函數sem_wait()的非阻塞版本,它直接將信號量sem的值減一。  spa

函數sem_destroy(sem_t *sem)用來釋放信號量sem。 .net

信號量用sem_init函數建立的,下面是它的說明:
  #include<semaphore.h>
 int sem_init (sem_t *sem, int pshared, unsigned int value);

 這個函數的做用是對由sem指定的信號量進行初始化,設置好它的共享選項,並指定一個整數類型的初始值。pshared參數控制着信號量的類型。若是 pshared的值是0,就表示它是當前里程的局部信號量;不然,其它進程就可以共享這個信號量。咱們如今只對不讓進程共享的信號量感興趣。 (這個參數受版本影響), pshared傳遞一個非零將會使函數調用失敗。

  這兩個函數控制着信號量的值,它們的定義以下所示:線程

[cpp]  view plain  copy
 
  1. #include <semaphore.h>  
  2.  int sem_wait(sem_t * sem);  
  3.  int sem_post(sem_t * sem);  
 這兩個函數都要用一個由sem_init調用初始化的信號量對象的指針作參數。
 sem_post函數的做用是給信號量的值加上一個「1」,它是一個「原子操做」---即同時對同一個信號量作加「1」操做的兩個線程是不會衝突的;而同時對同一個文件進行讀、加和寫操做的兩個程序就有可能會引發衝突。信號量的值永遠會正確地加一個「2」--由於有兩個線程試圖改變它。
 sem_wait函數也是一個原子操做,它的做用是從信號量的值減去一個「1」,但它永遠會先等待該信號量爲一個非零值纔開始作減法。也就是說,若是你對一個值爲2的信號量調用sem_wait(),線程將會繼續執行,介信號量的值將減到1。若是對一個值爲0的信號量調用sem_wait(),這個函數就會地等待直到有其它線程增長了這個值使它再也不是0爲止。若是有兩個線程都在sem_wait()中等待同一個信號量變成非零值,那麼當它被第三個線程增長一個「1」時,等待線程中只有一個可以對信號量作減法並繼續執行,另外一個還將處於等待狀態。
 信號量這種「只用一個函數就能原子化地測試和設置」的能力下正是它的價值所在。還有另一個信號量函數sem_trywait,它是sem_wait的非阻塞搭檔。

 最後一個信號量函數是sem_destroy。這個函數的做用是在咱們用完信號量對它進行清理。下面的定義:
 #include<semaphore.h>
 int sem_destroy (sem_t *sem);
 這個函數也使用一個信號量指針作參數,歸還本身打敗的一切資源。在清理信號量的時候若是還有線程在等待它,用戶就會收到一個錯誤。
 與其它的函數同樣,這些函數在成功時都返回「0」。

 

[cpp]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <pthread.h>  
  6. #include <semaphore.h>  
  7. sem_t bin_sem;  
  8. void *thread_function1(void *arg)  
  9. {  
  10. printf("thread_function1--------------sem_wait\n");  
  11. sem_wait(&bin_sem);  
  12. printf("sem_wait\n");  
  13. while (1)  
  14. {  
  15. }  
  16. }  
  17. void *thread_function2(void *arg)  
  18. {  
  19. printf("thread_function2--------------sem_post\n");  
  20. sem_post(&bin_sem);  
  21. printf("sem_post\n");  
  22. while (1)  
  23. {  
  24. }  
  25. }  
  26. int main()  
  27. {  
  28. int res;  
  29. pthread_t a_thread;  
  30. void *thread_result;  
  31. res = sem_init(&bin_sem, 0, 0);  
  32. if (res != 0)  
  33. {  
  34.  perror("Semaphore initialization failed");  
  35. }  
  36.  printf("sem_init\n");  
  37. res = pthread_create(&a_thread, NULL, thread_function1, NULL);  
  38. if (res != 0)  
  39. {  
  40.  perror("Thread creation failure");  
  41. }  
  42. printf("thread_function1\n");  
  43. sleep (5);  
  44. printf("sleep\n");  
  45. res = pthread_create(&a_thread, NULL, thread_function2, NULL);  
  46. if (res != 0)  
  47. {  
  48.  perror("Thread creation failure");  
  49. }  
  50. while (1)  
  51. {  
  52. }  
  53. }  
  54. sem_init  
  55. thread_function1  
  56. thread_function1--------------sem_wait  
  57. sleep  
  58. thread_function2--------------sem_post  
  59. sem_wait  
  60. sem_post

 

轉自:sem_wait sem_post信號量操做進本函數指針

相關文章
相關標籤/搜索