aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/parent.c
blob: df0e8f7daa8bac9689f54c816b36fb6eed5f80ea (plain) (blame)
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
/* klaar@ida

   pintos -v -k --fs-disk=2 --qemu -p ../examples/parent -a parent -p ../examples/child -a child -- -f -q run parent | grep PASS > result.txt
   grep -c 'Lab 0' result.txt
   grep -c 'Lab 1' result.txt
   grep -c 'Lab 2' result.txt
   grep -c 'Lab 3' result.txt
   
   A test program that calls itself recursively. In the last step of
   the recursion child.c is started. Do not use with large values for
   CHILDREN or DEPTH.

   Shall produce 64 PASS messages, 16 of each, when CHILDREN=4 and DEPTH=3

   CHILDREN^DEPTH=count(PASS) (4^3=64)
*/
#include <syscall.h>
#include <stdlib.h>
#include <stdio.h>

#define CHILDREN 4
#define DEPTH 3

int main(int argc, char* argv[])
{
  int i;
  int pid[CHILDREN];
  int depth = DEPTH - 1;
  char cmd[10];

  if (argc == 2)
    depth = atoi(argv[1]) - 1;
  
  for(i = 0; i < CHILDREN; i++)
  {
    if (depth)
      snprintf(cmd, 10, "parent %i", depth);
    else
      snprintf(cmd, 10, "child %i", i);

    printf("%s\n", cmd);
    pid[i] = exec(cmd);
  }
//  if (depth <= 1)
  {
    for(i = 0; i < CHILDREN; i++)
    {
      wait(pid[i]);
    }
  }
  exit(0);
}