-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
38 lines (35 loc) · 760 Bytes
/
pipe.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
int main(int argc, char *argv[]){
int fd[2];
int ret=0;
char str[100]="hello,comoputer";
char buff[100]={0,};
int *fdw = &fd[1];
int *fdr = &fd[0];
ret = pipe(fd);
if(ret == -1){
printf("failed created!\n");
return -1;
}
pid_t pid;
pid = fork();
if(pid<0){
printf("fork error\n");
return -1;
}
if(pid == 0){
sleep(2);
close(*fdr);
ret = write(*fdw, str, strlen(str));
return 0;
}else{
close(*fdw);
ret = read(*fdr, buff, sizeof(buff));
printf("receive %d bytes data %s\n",ret,buff);
}
return 0;
}