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

int main()
{
    int *p, x, i;
    p=&x;

    pid_t pid;

    switch(pid=fork())
    {

       case -1: perror("fork1 failed"); exit(1);

       case 0:
                 switch(pid=fork())
                 {

                    case -1: perror("fork2 failed"); exit(1);

                    case 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); printf("x: %d. \n",x);
                            system("ps -e -o pid,ppid,comm | grep uppg1");
                    }

                        }
                            sleep(1);
                    }

                 }

                exit(0);
    }

        wait(0);




    switch(pid=fork())
    {

       case -1: perror("fork3 failed"); exit(1);

       case 0:  if(&p == 0)
                {
                    return 0;
                }
                else
                printf("Adding %d children under init.",&p);
                for(i=0 ; i<(&p) ; i++)
                {

                    switch(pid=fork())
                    {

                        case -1: perror("fork4 failed"); exit(1);

                        case 0: sleep(15);
                                exit(0);


                    }

                }

            exit(0);
    }

        wait(0);

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

}
