免费注册 查看新帖 |

Chinaunix

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

[C] 两个线程实现文件写入,编译通过,可是提示段错误郁闷 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-11 21:30 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>

  5. #define BUFFER_SIZE 10

  6. int buffer[BUFFER_SIZE];
  7. int in = 0;
  8. int out = 0;
  9. int nextProduced;
  10. int nextConsumed;
  11.        
  12. void thread_producer(int *);
  13. void thread_consumer(int *,FILE * );

  14. int main(void)
  15. {
  16.         FILE *fout;
  17.         fout = fopen("data.out","w");
  18.         pthread_t producer_id;
  19.         pthread_t consumer_id;
  20.         int ret_p, ret_c;
  21.         ret_p = pthread_create(&producer_id,NULL,(void *) thread_producer,NULL);
  22.         ret_c = pthread_create(&consumer_id,NULL,(void *) thread_consumer,NULL);
  23.         if(ret_p!=0)
  24.         {
  25.                 printf("Create thread_producer failed.\n");
  26.                 exit(1);
  27.         }
  28.         if(ret_c!=0)
  29.         {
  30.                 printf("Create thread_consumer failed.\n");
  31.                 exit(1);
  32.         }
  33.         pthread_join(producer_id,NULL);
  34.         pthread_join(consumer_id,NULL);
  35.         fclose(fout);
  36.         return 0;
  37. }

  38. void thread_producer(int* buffer)
  39. {
  40.         while(1)
  41.         {
  42.                 while(((in+1)%BUFFER_SIZE) == out)/* buffer is full */
  43.                         ;
  44.                 scanf("%d",&nextProduced);
  45.                 buffer[in] = nextProduced;
  46.                 in = (in+1)%BUFFER_SIZE;
  47.         }
  48.         return ;
  49. }

  50. void thread_consumer(int* buffer,FILE *fout)
  51. {
  52.         while(1)
  53.         {
  54.                        /*buffer is empty*/
  55.                 while(in == out)
  56.                         ;
  57.                 nextConsumed = buffer[out];
  58.                 fprintf(fout,"%d\n",nextConsumed);
  59.                 out = (out+1)%BUFFER_SIZE;
  60.         }
  61.         return ;
  62. }
复制代码
刚开始学操作系统,试着写了点,上来就错了阿。。誰来帮帮我阿。

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
2 [报告]
发表于 2013-10-12 10:11 |只看该作者
回复 1# 373202184


    数据对步在哪里?nextConsumed,nextProduced,in,out在两个线程里同时访问,而且还同步访问终端,不进行同步当然问题多多。

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
3 [报告]
发表于 2013-10-12 11:47 |只看该作者
1. 文件写和读, Offset已经变了。
2. 要同步。

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
4 [报告]
发表于 2013-10-12 12:03 |只看该作者
回复 1# 373202184
全局变量buffer,函数形参buffer
main局部变量fout,函数形参fout
pthread_create最后一个参数都为NULL


   

论坛徽章:
0
5 [报告]
发表于 2013-10-13 09:48 |只看该作者
两个线程不是共享主代码段的数据么?Producer 写如数据后才可以让Consumer读取然后写入文件呀。
while(in == out)  /*Buffer 已经空了,Consumer一直在等待阿。*/
       ;
回复 2# myworkstation


   

论坛徽章:
0
6
发表于 2013-10-13 09:50
回复 4# 井蛙夏虫

额,这样有什么问题么?


   

论坛徽章:
0
7 [报告]
发表于 2013-10-13 09:51 |只看该作者
回复 3# folklore
麻烦详细解释一下可否


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP