-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe.c
50 lines (43 loc) · 784 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
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
//code with pipe/fork
//father write
//child read
int main(int argc, char *argv)
{
int fd[2];
int ret;
int pid;
char *msg = "message from father";
char buf[1024];
ret = pipe(fd);
if (ret < 0) {
//printf("%s: pipe return error\n", __func__);
perror("pipe");
exit(1);
}
pid = fork();
if (pid < 0) {
//printf("%s: fork return error, pid = %d\n", __func__, pid);
perror("fork");
exit(1);
}
if (pid) {
// farther process
close(fd[0]);
ret = write(fd[1], msg, strlen(msg));
if (ret < 0)
exit(1);
}
else {
// child process
close(fd[1]);
ret = read(fd[0], buf, sizeof(buf));
if (ret < 0)
exit(1);
printf("print: %s\n", buf);
}
}