利用POSIX互斥锁和条件变量实现的信号量


利用POSIX互斥锁、条件变量,和内存映射文件,实现的信号量机制,用于进程间的同步。

  1. /* sem.h */  
  2. struct semaphore  
  3. {  
  4.     pthread_mutex_t lock;  
  5.     pthread_cond_t nonzero;  
  6.     unsigned count;  
  7. };  
  8. typedef struct semaphore semaphore_t;  
  9.   
  10.   
  11. semaphore_t*    semaphore_create(char *semaphore_name);  
  12. semaphore_t*    semaphore_open(char *semaphore_name);  
  13. void            semaphore_post(semaphore_t *semap);  
  14. void            semaphore_wait(semaphore_t *semap);  
  15. void            semaphore_close(semaphore_t *semap);  
  16.   
  17.   
  18. /* sem.c */  
  19. #include <sys/types.h>   
  20. #include <sys/stat.h>   
  21. #include <sys/mman.h>   
  22. #include <fcntl.h>   
  23. #include <pthread.h>   
  24. #include "sem.h"   
  25.   
  26.   
  27. semaphore_t* semaphore_create(char *semaphore_name)  
  28. {  
  29.     int fd = open(semaphore_name, O_RDWR | O_CREAT | O_EXCL, 0666);  
  30.     if (fd < 0) return (NULL);  
  31.     (void) ftruncate(fd, sizeof(semaphore_t));  
  32.   
  33.     pthread_mutexattr_t psharedm;  
  34.     pthread_condattr_t psharedc;  
  35.     (void) pthread_mutexattr_init(&psharedm);  
  36.     (void) pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED);  
  37.     (void) pthread_condattr_init(&psharedc);  
  38.     (void) pthread_condattr_setpshared(&psharedc, PTHREAD_PROCESS_SHARED);  
  39.     semaphore_t* semap = (semaphore_t *) mmap(NULL,  
  40.                                               sizeof(semaphore_t),  
  41.                                               PROT_READ | PROT_WRITE,  
  42.                                               MAP_SHARED,  
  43.                                               fd,  
  44.                                               0);  
  45.     close (fd);  
  46.     (void) pthread_mutex_init(&semap->lock, &psharedm);  
  47.     (void) pthread_cond_init(&semap->nonzero, &psharedc);  
  48.       
  49.     // 我觉得应该给个大于零的初值   
  50.     semap->count = 0;  
  51.     return (semap);  
  52. }  
  53.   
  54.   
  55. semaphore_t* semaphore_open(char *semaphore_name)  
  56. {  
  57.     int fd = open(semaphore_name, O_RDWR, 0666);  
  58.     if (fd < 0) return (NULL);  
  59.   
  60.     semaphore_t* semap = (semaphore_t *) mmap(NULL,  
  61.                                               sizeof(semaphore_t),  
  62.                                               PROT_READ | PROT_WRITE,  
  63.                                               MAP_SHARED,  
  64.                                               fd,  
  65.                                               0);  
  66.     close (fd);  
  67.     return (semap);  
  68. }  
  69.   
  70.   
  71. void semaphore_post(semaphore_t *semap)  
  72. {  
  73.     pthread_mutex_lock(&semap->lock);  
  74.     // 计数为零,说明可能有线程已经阻塞在该条件变量上   
  75.     if (semap->count == 0)  
  76.     {  
  77.         pthread_cond_signal(&semapx->nonzero);  
  78.     }  
  79.     semap->count++;  
  80.     pthread_mutex_unlock(&semap->lock);  
  81. }  
  82.   
  83.   
  84. void semaphore_wait(semaphore_t *semap)  
  85. {  
  86.     pthread_mutex_lock(&semap->lock);  
  87.     // 计数为零,说明已无资源,等待   
  88.     while (semap->count == 0)  
  89.     {  
  90.         pthread_cond_wait(&semap->nonzero, &semap->lock);  
  91.     }  
  92.     semap->count--;  
  93.     pthread_mutex_unlock(&semap->lock);  
  94. }  
  95.   
  96.   
  97. void semaphore_close(semaphore_t *semap)  
  98. {  
  99.     munmap((void *) semap, sizeof(semaphore_t));  
  100. }  

相关内容