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

main()
{
    if(fork()){ exit(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);

            if(x <= 0){ printf("Stopping demon\n"); exit(0);}

            printf("Adding %d children under init.\n", x);

            for(i=0; i<x; i++)
            {
                if(!fork())
                {
                    if(fork())
                    {
                        exit(0);
                    }else{
                        sleep(15);
                        exit(0);
                    }
                }else{
                    wait(0);
                }
            }

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