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

main()
{
  int pid;

  pid = fork();
  if(pid == 0){

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

    while(1) {
      res=fgets(str,10,fd);
      if(res!=NULL) {
        x=atoi(str); 
  	printf("x: %d.\n", x);

	if(x <= 0){
	  printf("Process terminated\n");
	  exit(0);
	}

	if(x > 0){
	  i = x;
	  while(i>0){

	    pid = fork();
	    if(pid == 0){
	      sleep(15);
	      exit(0);
	    }	  
	  
	    i--;
	  }
	  system("ps -e -o pid,ppid,comm | grep uppg1");
	  while(x>0){
	    wait(0);
	    x--;
	  }
	}

        system("ps -e -o pid,ppid,comm | grep uppg1");
      }
      sleep(1);
    }



  }


  //end of child
  exit(0);
}
