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
|
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#define N 5
char buf[N];
void
pong(int *parent_to_child, int *child_to_parent) {
if (read(parent_to_child[0], buf, N) < 0) {
printf("read failed\n");
}
printf("%d: received %s\n", getpid(), buf);
if (write(child_to_parent[1], "pong", 4) != 4) {
printf("write failed\n");
}
}
void
ping(int *parent_to_child, int *child_to_parent) {
if (write(parent_to_child[1], "ping", 4) != 4) {
printf("write failed\n");
}
if (read(child_to_parent[0], buf, N) < 0) {
printf("read failed\n");
}
printf("%d: received %s\n", getpid(), buf);
}
int
main(int argc, char *argv[])
{
int parent_to_child[2];
int child_to_parent[2];
int pid;
if (pipe(parent_to_child) < 0 || pipe(child_to_parent) < 0) {
printf("pipe failed\n");
}
if ((pid = fork()) < 0) {
printf("fork failed\n");
}
if (pid == 0) {
pong(parent_to_child, child_to_parent);
} else {
ping(parent_to_child, child_to_parent);
}
exit(0);
}
|