Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563105
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2014-12-23 10:15:20

单生产者单消费者
1.有名信号量的单生产者单消费者

点击(此处)折叠或打开

  1. #include "../unipc.h"

  2. #define NBUFF 10
  3. #define SEM_MUTEX "mutex"
  4. #define SEM_NEMPTY "nempty"
  5. #define SEM_NSTORED "nstored"
  6. #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
  7. int nitems;

  8. struct {
  9.     int buff[NBUFF];
  10.     sem_t *mutex,*nempty,*nstored;
  11. } shared;

  12. void *produce(void *arg)
  13. {
  14.     int i;
  15.     for(i = 0;i < nitems; i++) {
  16.         sem_wait(shared.nempty);
  17.         sem_wait(shared.mutex);
  18.         shared.buff[i%NBUFF] = i;
  19.         sem_post(shared.mutex);
  20.         sem_post(shared.nstored);
  21.     }
  22.     return NULL;
  23. }

  24. void *consume(void *arg)
  25. {
  26.     int i;
  27.     for(i = 0;i < nitems; i++) {
  28.         sem_wait(shared.nstored);
  29.         sem_wait(shared.mutex);
  30.         if(i != shared.buff[i%NBUFF]) {
  31.             printf("i = %d,buff[%d]=%d\n",i,i,shared.buff[i]);
  32.         }
  33.         sem_post(shared.mutex);
  34.         sem_post(shared.nempty);
  35.     }
  36.     return NULL;
  37. }

  38. int main(int argc ,char *argv[])
  39. {
  40.     pthread_t ptid_produce,ptid_consume;

  41.     if(argc != 2 ) {
  42.         printf("usage produce1 <#items>\n");
  43.         return -1;
  44.     }
  45.     nitems = atoi(argv[1]);
  46.     
  47.     shared.mutex = sem_open(SEM_MUTEX,O_CREAT|O_EXCL,FILE_MODE,1);
  48.     shared.nempty = sem_open(SEM_NEMPTY,O_CREAT|O_EXCL,FILE_MODE,NBUFF);
  49.     shared.nstored = sem_open(SEM_NSTORED,O_CREAT|O_EXCL,FILE_MODE,0);

  50.     pthread_create(&ptid_produce,NULL,produce,NULL);
  51.     pthread_create(&ptid_consume,NULL,consume,NULL);

  52.     pthread_join(ptid_produce,NULL);
  53.     pthread_join(ptid_consume,NULL);

  54.     sem_unlink(SEM_MUTEX);
  55.     sem_unlink(SEM_NEMPTY);
  56.     sem_unlink(SEM_NSTORED);

  57.     return 0;
  58. }
基于内存的信号量的单生产者单消费者

点击(此处)折叠或打开

  1. #include "../unipc.h"

  2. #define NBUFF 10
  3. #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
  4. int nitems;

  5. struct {
  6.     int buff[NBUFF];
  7.     sem_t mutex,nempty,nstored;
  8. } shared;

  9. void *produce(void *arg)
  10. {
  11.     int i;
  12.     for(i = 0;i < nitems; i++) {
  13.         sem_wait(&shared.nempty);
  14.         sem_wait(&shared.mutex);
  15.         shared.buff[i%NBUFF] = i;
  16.         sem_post(&shared.mutex);
  17.         sem_post(&shared.nstored);
  18.     }
  19.     return NULL;
  20. }

  21. void *consume(void *arg)
  22. {
  23.     int i;
  24.     for(i = 0;i < nitems; i++) {
  25.         sem_wait(&shared.nstored);
  26.         sem_wait(&shared.mutex);
  27.         if(i != shared.buff[i%NBUFF]) {
  28.             printf("i = %d,buff[%d]=%d\n",i,i,shared.buff[i]);
  29.         }
  30.         sem_post(&shared.mutex);
  31.         sem_post(&shared.nempty);
  32.     }
  33.     return NULL;
  34. }

  35. int main(int argc ,char *argv[])
  36. {
  37.     pthread_t ptid_produce,ptid_consume;

  38.     if(argc != 2 ) {
  39.         printf("usage produce1 <#items>\n");
  40.         return -1;
  41.     }
  42.     nitems = atoi(argv[1]);

  43.     sem_init(&shared.mutex,0,1);
  44.     sem_init(&shared.nempty,0,NBUFF);
  45.     sem_init(&shared.nstored,0,0);

  46.     pthread_create(&ptid_produce,NULL,produce,NULL);
  47.     pthread_create(&ptid_consume,NULL,consume,NULL);

  48.     pthread_join(ptid_produce,NULL);
  49.     pthread_join(ptid_consume,NULL);

  50.     sem_destroy(&shared.mutex);
  51.     sem_destroy(&shared.nempty);
  52.     sem_destroy(&shared.nstored);
  53.     return 0;
  54. }



阅读(702) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~