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

main()
{
   pid_t pid;
   int i;

   pid=fork();
   if(pid==-1)
   {
       fprintf(stderr,"Fork Failed\n");
       exit(1);
   }
   else if(pid!=0)
   {
       exit(0);
   }

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

  while(1) {
    res=fgets(str,10,fd);
    if(res!=NULL) {
      x=atoi(str);

      if(x<=0)
      {
          exit(0);
      }

      pid=fork();
      if(pid==-1)
      {
          fprintf(stderr,"Fork Failed\n");
          exit(1);
      }
      else if(pid==0){

        for(i=0;i<x;i++)
            {
                pid=fork();
                if(pid==-1)
                {
                    fprintf(stderr,"Fork Failed\n");
                    exit(1);
                }
                else if(pid==0)
                {
                    sleep(5);
                    exit(0);
                }
            }
        exit(0);
      }

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


    }
    sleep(1);
  }

}
