summaryrefslogtreecommitdiff
path: root/user/pingpong.c
blob: 7b03a765af0a65a4c3fbcc0ab831f0fe3abffe82 (plain)
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
#include "kernel/types.h"
#include "user/user.h"

int
main(int argc, char* argv[])
{
  int p[2];

  if (argc > 1) {
    fprintf(2, "usage: pingpong\n");
    exit(1);
  }

  pipe(p);

  int pid = fork();

  if (pid == 0) {
    short n;
    read(p[0], &n, sizeof(n));
    if (n == 42) {
      fprintf(1, "%d: received ping\n", getpid());
    }
    n++;
    write(p[1], &n, sizeof(n));
    close(p[0]);
    close(p[1]);
    exit(0);
  }

  short n = 42;

  write(p[1], &n, sizeof(n));
  read(p[0], &n, sizeof(n));
  if (n == 43) {
    fprintf(1, "%d: received pong\n", getpid());
  }
  close(p[0]);
  close(p[1]);

  wait(0);

  exit(0);
}