- 论坛徽章:
- 2
|
程序代码,如下:
1 #include<stdio.h>
2 #include<unistd.h>
3 int main()
4 {
5 int fd[2];
6 char buf1[100],buf2[100];
7 printf("please input a string:1\n"
8 gets(buf1);
9 pipe(fd);
10 write(fd[1],buf1,sizeof(buf1));
11 printf("please input a string:2\n"
12 gets(buf1);
13 write(fd[1],buf1,sizeof(buf1));
14 printf("please input a string:3\n"
15 gets(buf1);
16 write(fd[1],buf1,sizeof(buf1));
17 read(fd[0],buf2,100);
18 printf("pipe read 1:%s\n",buf2);
19 read(fd[0],buf2,100);
20 printf("pipe read 2:%s\n",buf2);
21 read(fd[0],buf2,100);
22 printf("pipe read 3:%s\n",buf2);
23 return 0;
}
运行结果,如下:
[root@localhost home]# ./pipe
please input a string:1
zhejiang
please input a string:2
hangzhou
please input a string:3
qiandaohu
pipe read 1:zhejiang
pipe read 2:hangzhou
pipe read 3:qiandaohu
说管道是一种文件系统,那么为什么我3次写之后,不能,一次把3次写的数据读出来???
每个文件系统在内核中以i节点来记录它的数据块链表,难道管道文件不是这样的吗???还是说,对管道每写一次就对应一个V节点和i节点??? |
|