关于C pipe的问题,新手,求赐教啊

linxiaosu 2013-03-15 02:48:26
请大家帮忙看看这个的输出应该是啥? 运行系统 centOS 64bit

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>

void * fw(void * fd)
{
int i=0;
int w;
while(1){
char str[10];
sprintf(str,"%d",i);
write(*(int *)fd,str,sizeof(str));
sleep(1);
i++;
}
}

void * fr(void * fd)
{
while(1){
char buffer[30];
read(*(int *)fd,buffer,sizeof(buffer));
printf("%s\n",buffer);
sleep(5);
}
}

int main()
{
int fd[2];
pthread_t p1,p2;
pipe(fd);
pthread_create(&p1,NULL,fw,&fd[1]);
pthread_create(&p2,NULL,fr,&fd[0]);
pthread_join(p1,NULL);
pthread_join(p2,NULL);
}
...全文
214 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
linxiaosu 2013-03-18
  • 打赏
  • 举报
回复
好吧,自己解决了,是写入跟读出的缓冲区不匹配造成的
linxiaosu 2013-03-18
  • 打赏
  • 举报
回复
哪位高手能赐教一下啊
linxiaosu 2013-03-15
  • 打赏
  • 举报
回复
引用 2 楼 ywj1225 的回复:
Plain Text code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990……
这是你的运行结果?我的是这样的 0 1 4 7 10 让我很费解啊
wjain 2013-03-15
  • 打赏
  • 举报
回复

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
...
...
...


linxiaosu 2013-03-15
  • 打赏
  • 举报
回复
坐等求赐教啊!!!

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧