-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMypipe(linux).txt
64 lines (62 loc) · 1.7 KB
/
Mypipe(linux).txt
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
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/wait.h>
int main()
{
int pipefd[2];
int ret = pipe(pipefd);
if(ret==-1)
{
perror("pipe");
// exit(1);
}
pid_t id = fork();
if(id==0)
{//child ,write
int count = 0;
int i =0;
char* sendmsg = NULL;
close(pipefd[0]);
while(i<100)
{
sendmsg="Hello Dad !";
// printf("i am sendingmsg...");
ssize_t ret = write(pipefd[1],sendmsg,strlen(sendmsg));// [*]
//printf("ret=%d\n",ret);
printf("i am sendingmsg,count: %d\n",count++);
// sleep(1);
i++;
}
// close(pipefd[1]);
return 0;
}
else
{//father
int status = 0;
int i=0;
char buf[30];
// memset(buf,'\0',sizeof(buf));
close(pipefd[1]);
while(i<5)
{
memset(buf,'\0',sizeof(buf));
int ret = read(pipefd[0],buf,sizeof(buf));
// printf("%d\n",ret);
printf("%s\n",buf);
i++;
}
printf("Dad not read from now\n");
sleep(20);
// close(pipefd[0]);
// pid_t id = wait(&status);
// if(id > 0)
// {
// printf("child is exit,id:%d,the exit sig :%d\n",id,status&0xff);//[*]
// }
// else
// {
// perror("wait");
// }
}
}