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

关于C pipe的问题,新手,求赐教啊
本帖最后由 linxiaosu 于 2013-03-15 14:50:12 编辑
请大家帮忙看看这个的输出应该是啥? 运行系统 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);
}
管道 pipe

------解决方案--------------------

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
...
...
...