Port tests/timerfd.c
[akaros.git] / tests / pipetest.c
1 #include <sched.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <parlib/vcore.h>
6
7 void child_handler(int readfd, int writefd)
8 {
9         char c;
10         read(readfd, &c, 1);
11         close(readfd);
12         printf("Child read from pipe0\n");
13         printf("Child writing to pipe1\n");
14         write(writefd, "", 1);
15         close(writefd);
16         exit(0);
17 }
18
19 void parent_handler(int readfd, int writefd)
20 {
21         /* Force the child to run first. */
22         for (int i = 0; i < 10; i++)
23                 sched_yield();
24
25         char c;
26         printf("Parent writing to pipe0\n");
27         write(writefd, "", 1);
28         close(writefd);
29         read(readfd, &c, 1);
30         close(readfd);
31         printf("Parent read from pipe1\n");
32         exit(0);
33 }
34
35 int main(int argc, char **argv)
36 {
37         int pipe0[2];
38         int pipe1[2];
39         pipe(pipe0);
40         pipe(pipe1);
41         pid_t child = fork();
42         if (child == 0) {
43                 close(pipe0[1]);
44                 close(pipe1[0]);
45                 child_handler(pipe0[0], pipe1[1]);
46         } else {
47                 close(pipe0[0]);
48                 close(pipe1[1]);
49                 parent_handler(pipe1[0], pipe0[1]);
50         }
51         return 0;
52 }