Linux下的信号量----生产者与消费者

来源:互联网 发布:mac 键盘符号 编辑:程序博客网 时间:2024/04/30 09:49

关于信号量的函数:

初始化信号量                  int sem_init (sem_t *sem, int pshared, unsigned int value) 

        第一个参数是信号量;第二个参数pshared设为0,意思是信号量用于同一进程间同步;第三个参数value是计数器的初始值。

P操作                               int sem_wait (sem_t *sem)

V操作                               int sem_post (sem_t *sem)         

删除信号量                     int sem_destory (sem_t *sem)

生产者与消费者模型则复合321原则:三种关系,两种角色,一种交易场所

三种关系:

           生产者与消费者:互斥,同步

           生产者与生产者:互斥

           消费者与消费者:互斥


测试代码如下:

  1 #include<stdio.h>  2 #include<pthread.h>  3 #include<semaphore.h>  4   5 int ring[64];  6 sem_t semblank;  7 sem_t semdata;  8   9  10 void * con(void* arg) 11 { 12     int step=0; 13     while(1) 14     { 15         sem_wait(&semdata); 16         int data=ring[step]; 17         step++; 18         step %= 64; 19         printf("con done:%d\n",data); 20         sem_post(&semblank); 21//       sleep(2); 22     } 23 } 24  25 void * pro(void* arg) 26 { 27     int step=0; 28     while(1) 29     { 30         sem_wait(&semblank); 31         int data=rand()%1234; 32         ring[step]=data; 33         step++; 34         step%=64; 35         printf("pro don:%d\n",data); 36         sem_post(&semdata); 37 //      sleep(2); 38     } 39 } 40 int main() 41 { 42     sem_init(&semblank,0,64); 43     sem_init(&semdata,0,0); 44     pthread_t conr,pror; 45     pthread_create(&conr,NULL,con,NULL); 46     pthread_create(&pror,NULL,pro,NULL); 47  48     pthread_join(conr,NULL); 49     pthread_join(pror,NULL); 50     sem_destroy(&semblank); 51     sem_destroy(&semdata); 52  53     return 0; 54 }                             

运行现象是生产几次消费几次



可以在此代码的基础上测试两种情况

(1)生产者pror在V操作后sleep 2秒,现象是生产一次,消费一次;

  1 #include<stdio.h>  2 #include<pthread.h>  3 #include<semaphore.h>  4   5 int ring[64];  6 sem_t semblank;  7 sem_t semdata;  8   9  10 void * con(void* arg) 11 { 12     int step=0; 13     while(1) 14     { 15         sem_wait(&semdata); 16         int data=ring[step]; 17         step++; 18         step %= 64; 19         printf("con done:%d\n",data); 20         sem_post(&semblank); 21//       sleep(2); 22     } 23 } 24  25 void * pro(void* arg) 26 { 27     int step=0; 28     while(1) 29     { 30         sem_wait(&semblank); 31         int data=rand()%1234; 32         ring[step]=data; 33         step++; 34         step%=64; 35         printf("pro don:%d\n",data); 36         sem_post(&semdata); 37         sleep(2); 38     } 39 } 40 int main() 41 { 42     sem_init(&semblank,0,64); 43     sem_init(&semdata,0,0); 44     pthread_t conr,pror; 45     pthread_create(&conr,NULL,con,NULL); 46     pthread_create(&pror,NULL,pro,NULL); 47  48     pthread_join(conr,NULL); 49     pthread_join(pror,NULL); 50     sem_destroy(&semblank); 51     sem_destroy(&semdata); 52  53     return 0; 54 }

(2)消费者在V操作后sleep 2秒,现象是一瞬间生产很多,然后消费一次,生产一次;

  1 #include<stdio.h>  2 #include<pthread.h>  3 #include<semaphore.h>  4   5 int ring[64];  6 sem_t semblank;  7 sem_t semdata;  8   9  10 void * con(void* arg) 11 { 12     int step=0; 13     while(1) 14     { 15         sem_wait(&semdata); 16         int data=ring[step]; 17         step++; 18         step %= 64; 19         printf("con done:%d\n",data); 20         sem_post(&semblank); 21         sleep(2); 22     } 23 } 24  25 void * pro(void* arg) 26 { 27     int step=0; 28     while(1) 29     { 30         sem_wait(&semblank); 31         int data=rand()%1234; 32         ring[step]=data; 33         step++; 34         step%=64; 35         printf("pro don:%d\n",data); 36         sem_post(&semdata); 37 //      sleep(2); 38     } 39 } 40 int main() 41 { 42     sem_init(&semblank,0,64); 43     sem_init(&semdata,0,0); 44     pthread_t conr,pror; 45     pthread_create(&conr,NULL,con,NULL); 46     pthread_create(&pror,NULL,pro,NULL); 47  48     pthread_join(conr,NULL); 49     pthread_join(pror,NULL); 50     sem_destroy(&semblank); 51     sem_destroy(&semdata); 52  53     return 0; 54 }


原创粉丝点击