现在的位置: 首页 > 综合 > 正文

sem_wait sem_post信号量操作进本函数

2013年01月15日 ⁄ 综合 ⁄ 共 2722字 ⁄ 字号 评论关闭

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给出了信号量的初始值。  

函数sem_post( sem_t *sem )用来增加信号量的值。当有线程阻塞在这个信号量上时,调用这个函数会使其中的一个线程不在阻塞,选择机制同样是由线程的调度策略决定的。  

函数sem_wait( sem_t *sem )被用来阻塞当前线程直到信号量sem的值大于0,解除阻塞后将sem的值减一,表明公共资源经使用后减少。函数sem_trywait ( sem_t *sem )是函数sem_wait()的非阻塞版本,它直接将信号量sem的值减一。  

函数sem_destroy(sem_t *sem)用来释放信号量sem。 

信号量用sem_init函数创建的,下面是它的说明:
  #include<semaphore.h>
 int sem_init (sem_t *sem, int pshared, unsigned int value);

 这个函数的作用是对由sem指定的信号量进行初始化,设置好它的共享选项,并指定一个整数类型的初始值。pshared参数控制着信号量的类型。如果 pshared的值是0,就表示它是当前里程的局部信号量;否则,其它进程就能够共享这个信号量。我们现在只对不让进程共享的信号量感兴趣。 (这个参数受版本影响), pshared传递一个非零将会使函数调用失败。

  这两个函数控制着信号量的值,它们的定义如下所示:

  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”。

  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.   
  8. sem_t bin_sem;  
  9. void *thread_function1(void *arg)  
  10. {  
  11. printf("thread_function1--------------sem_wait\n");  
  12. sem_wait(&bin_sem);  
  13. printf("sem_wait\n");  
  14. while (1)  
  15. {  
  16. }  
  17. }  
  18.   
  19. void *thread_function2(void *arg)  
  20. {  
  21. printf("thread_function2--------------sem_post\n");  
  22. sem_post(&bin_sem);  
  23. printf("sem_post\n");  
  24. while (1)  
  25. {  
  26. }  
  27. }  
  28.   
  29.   
  30.   
  31. int main()  
  32. {  
  33. int res;  
  34. pthread_t a_thread;  
  35. void *thread_result;  
  36.   
  37. res = sem_init(&bin_sem, 0, 0);  
  38. if (res != 0)  
  39. {  
  40.  perror("Semaphore initialization failed");  
  41. }  
  42.  printf("sem_init\n");  
  43. res = pthread_create(&a_thread, NULL, thread_function1, NULL);  
  44. if (res != 0)  
  45. {  
  46.  perror("Thread creation failure");  
  47. }  
  48. printf("thread_function1\n");  
  49. sleep (5);  
  50. printf("sleep\n");  
  51. res = pthread_create(&a_thread, NULL, thread_function2, NULL);  
  52. if (res != 0)  
  53. {  
  54.  perror("Thread creation failure");  
  55. }  
  56. while (1)  
  57. {  
  58. }  
  59. }  
  60.   
  61.   
  62. sem_init  
  63. thread_function1  
  64. thread_function1--------------sem_wait  
  65. sleep  
  66. thread_function2--------------sem_post  
  67. sem_wait  
  68. sem_post  

【上篇】
【下篇】

抱歉!评论已关闭.