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