#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

/* pstree output when trying arbre 5

-+= 52015 yunes ./arbre 5
 |-+- 52016 yunes ./arbre 5
 | |-+- 52018 yunes ./arbre 5
 | | |-+- 52021 yunes ./arbre 5
 | | | |--- 52024 yunes (arbre)
 | | | |--- 52026 yunes ./arbre 5
 | | | |--- 52027 yunes ./arbre 5
 | | | |--- 52028 yunes ./arbre 5
 | | | \--- 52029 yunes ./arbre 5
 | | |--- 52022 yunes ./arbre 5
 | | |--- 52023 yunes ./arbre 5
 | | \--- 52025 yunes ./arbre 5
 | |--- 52019 yunes ./arbre 5
 | \--- 52020 yunes ./arbre 5
 \--- 52017 yunes ./arbre 5

*/

// warning: no real error handling there...
int main(int argc,char *argv[]) {
  int n;
  sscanf(argv[1],"%d",&n);
  for (int i=0; i<n-1; i++) {
    pid_t p = fork();
    switch(p) {
    case -1:
      break;
    case 0:
      break;
    default:
      for (int j=0; j<=i; j++) {
        switch(fork()) {
        case -1: break;
        case 0: sleep(10); exit(0);
        default: break;
        }
      }
      sleep(10); // test
      wait(NULL);
      exit(0);
    }
  }
}