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

main()
{
	if(fork())
		exit(0);
	//system("ps -e -o pid,ppid,comm | grep uppg1");
	
	char str[20], *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)
			{
				printf("stopping daemon\n");
				exit(0);
			}
			printf("adding %d children under init\n",x);
			if(!fork())
			{
				int i;
				for(i = 0; i<x;i++)
				{
					if(!fork())
					{
						sleep(15);
						exit(0);
					}	
				}
			exit(0);
			}
			wait(0);
			//sleep(1);
			system("ps -e -o pid,ppid,comm | grep uppg1");
		}
		sleep(1);
	}


}
