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

main()
{
  char str[10], *res; int x;
  FILE *fd=fopen("/home/me/Desktop/pipe","r");
  int pid, i;

  while(1) {
    res=fgets(str,10,fd);
    if(res!=NULL) {
      x=atoi(str); printf("x: %d.\n", x);
      if(x < 1){
        printf("Stopping daemon\n");
        exit(0);
      }
      printf("Main process: \n");
      system("ps -e -o pid,ppid,comm | grep uppg1");
      switch(pid = fork()){
        case -1:
        printf("Fork 1st child Failed\n");
        exit(0);

        case 0:
            for(i = 0; i < x ; i++){
                int _pid;
                switch(fork()){
                case -1:
                    printf("Fork 2nd child Failed\n");
                    exit(0);
                case 0:
                    sleep(15);
                    exit(0);
                default:
                    printf("Created child %i\n",i+1);
                }
            }
            exit(0);

        default:
            wait();
            system("ps -e -o pid,ppid,comm | grep uppg1");

      }

    sleep(15);
    printf("Processes after 15 sec sleep:\n");
    system("ps -e -o pid,ppid,comm | grep uppg1");

    }

  }

}
