免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1417 | 回复: 6
打印 上一主题 下一主题

求助:生产者/消费者通过buffer通讯的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-01 04:05 |只看该作者 |倒序浏览
生产者,2个线程
消费者,2个线程
想通过buffer把string“Hello World”的字符打印到stdout,现程序compile通过,可运行没结果,请大家帮忙看看。
   
  1. /*
  2.    The size of buffer is more than one. Multiple producers, multiple consumers
  3. */

  4. #include <stdio.h>;
  5. #include <pthread.h>;

  6. #define N_PRODUCERS 2    /* the number of producers */
  7. #define N_CONSUMERS 2    /* the number of consumers */
  8. #define BUFFER_SIZE 3    /* the size of the buffer  */

  9. int  count = 0;          /* the number of data items in the buffer */
  10. char buf[BUFFER_SIZE];   /* the buffer (circular buffer) */
  11. int  head = 0;           /* index of the head data in the buffer */
  12. int  tail = 0;           /* index of the tail data in the buffer */

  13. pthread_cond_t  empty, full;
  14. pthread_mutex_t buf_mutex_lock, produced_lock, consumed_lock;

  15. char *message = "Hello_world.";


  16. void get_from_buf(char *ch)
  17. {
  18.    while (count == 0)
  19.    {
  20.       /* Wait until producer adds something */
  21.       pthread_cond_wait(&full, &consumed_lock);  
  22.    }

  23.    pthread_mutex_lock(&buf_mutex_lock);
  24.    *ch = buf[head];
  25.    head = (head+1) % BUFFER_SIZE;
  26.    count--;
  27.    pthread_mutex_unlock(&buf_mutex_lock);
  28.    pthread_cond_signal(&empty);
  29. }


  30. void put_to_buf(char *ch)
  31. {
  32.    char *p = message;
  33.    
  34.    while (count == BUFFER_SIZE)
  35.    {
  36.       /* Wait until a consumer removes something. */
  37.       pthread_cond_wait(&empty, &produced_lock);
  38.    }

  39.    pthread_mutex_lock(&buf_mutex_lock);
  40.    buf[tail] = *ch = *(p++);
  41.    tail = (tail+1) % BUFFER_SIZE;
  42.    count++;
  43.    pthread_mutex_unlock(&buf_mutex_lock);
  44.    pthread_cond_signal(&full);
  45. }


  46. /*****************************************************************
  47. Producer
  48. *****************************************************************/
  49. void producer(void)
  50. {
  51.    char ch;
  52.    
  53.    do
  54.    {
  55.       pthread_mutex_lock(&produced_lock);
  56.       put_to_buf(&ch);
  57.       pthread_mutex_unlock(&produced_lock);
  58.    } while (ch != '\0');
  59.    pthread_exit(NULL);
  60. }


  61. /*****************************************************************
  62. Consumer
  63. *****************************************************************/
  64. void consumer(void)
  65. {
  66.    char ch;
  67.    
  68.    do
  69.    {
  70.       pthread_mutex_lock(&consumed_lock);
  71.       get_from_buf(&ch);
  72.       if (ch != '\0')                  /* print the character */
  73.          printf("%c ", ch);
  74.       pthread_mutex_unlock(&consumed_lock);
  75.    } while (ch != '\0');
  76.    pthread_exit(NULL);
  77. }


  78. int main(int argc, char **argv)
  79. {
  80.    int i;
  81.    
  82.    pthread_t producer_threads[N_PRODUCERS];
  83.    pthread_t consumer_threads[N_CONSUMERS];
  84.    pthread_attr_t attr;
  85.    pthread_attr_init(&attr);
  86.    pthread_mutex_init(&buf_mutex_lock, NULL);
  87.    pthread_mutex_init(&produced_lock, NULL);
  88.    pthread_mutex_init(&consumed_lock, NULL);
  89.    pthread_cond_init(&empty, NULL);
  90.    pthread_cond_init(&full, NULL);


  91.    /* Create producers. */
  92.    for (i = 0; i < N_PRODUCERS; i++)
  93.    {        
  94.       if (pthread_create(&producer_threads[i], &attr, (void*) &producer, NULL) != 0)
  95.       {
  96.                 /* Error. */
  97.       }
  98.    }

  99.    /* Create consumers. */
  100.    for (i = 0; i < N_CONSUMERS; i++)
  101.    {
  102.       if (pthread_create(&consumer_threads[i], &attr, (void*)&consumer, NULL) != 0)
  103.       {
  104.                  /* Error. */
  105.       }
  106.    }

  107.    /* Wait for them to finish. */
  108.    for (i = 0; i < N_CONSUMERS; i++)
  109.    {
  110.       pthread_join(producer_threads[i], NULL);
  111.       pthread_join(consumer_threads[i], NULL);
  112.    }

  113.    /* We're done. */
  114.    return 0;
  115. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-06-01 06:34 |只看该作者

求助:生产者/消费者通过buffer通讯的问题

你上面有一个问题,那就是put_to_buf中,p是局部量,退出后,上次的状态就消失了。

所以,你的结果就会都是H H.....

因此,你可以改成

  1. static char *p = message;
复制代码


就ok了。

论坛徽章:
0
3 [报告]
发表于 2003-06-01 10:01 |只看该作者

求助:生产者/消费者通过buffer通讯的问题

提醒一句:
线程中如果使用了static变量,编程的时候要注意不要发生重入。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2003-06-01 10:24 |只看该作者

求助:生产者/消费者通过buffer通讯的问题

对,上面的例子中那个buf_lock_mutex就起了防止重入的作用。

论坛徽章:
0
5 [报告]
发表于 2003-06-01 10:31 |只看该作者

求助:生产者/消费者通过buffer通讯的问题

俺这阵子跟unix底层system call干上了,多谢各位捧场!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-06-02 13:58 |只看该作者

求助:生产者/消费者通过buffer通讯的问题

呵呵,投你一票。。。

论坛徽章:
0
7 [报告]
发表于 2003-06-05 12:04 |只看该作者

求助:生产者/消费者通过buffer通讯的问题

加信号灯
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP