1 #include<stdio.h>
2#include<sys/wait.h>
3#include<unistd.h>
4#include<string.h>
5#include<stdlib.h>
6int main()
7 {
8 int pipefd[2];
9 if(pipe(pipefd)==-1)
10 {
11 perror("pipe");
12 return -1;
13 }
14 pid_t pid=fork();
15 if(pid<0)
16 {
17 perror("pid");
18 return -1;
19 }
20 else if(pid==0)
21 {
22 close(pipefd[0]);
23 char buf[20]="we are young";
24 int i=0;
25 while(i<10)
26 {
27 write(pipefd[1],buf,strlen(buf));
28 sleep(1);
29 i++;
30 }
31 exit(1);
32 }
33 else
34 {
35 close(pipefd[1]);
36 char buf[200];
37 int j=0;
38 while(j<10)
39 {
40 memset(buf,'\0',sizeof(buf));
41 ssize_t size= read(pipefd[0],buf,sizeof(buf));
42 if(size>0)
43 printf("%s\n",buf);
44 j++;
45 }
46 if(waitpid(pid,NULL,0)==pid)
47 {
48 printf("wait success\n");
49 }
50 }
51 return 0;
52 }
Makefile:
1 Pipe:pipe.c
2 gcc -o $@ $^
3.PHONY:clean
4clean:
5 rm -f pipe
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。