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

main(int argc, char *argv[]){
int child=atoi(argv[1]);
int fds[2]; pipe(fds);
int sum=0, rc,i;
if(!fork()){
	sleep(1);
	close(fds[1]);
	for(i=0;i<child;i++){
		sleep(1);
		if(read(fds[0],&rc,sizeof(int))){
			sum=sum+rc;
		}
	}
	close(fds[0]);	
	printf("\n Sum of pids: %d\n\n",sum);
	exit(0);
}
system("ps -o pid,ppid,comm");
for(i=0;i<child;i++){
	if(!fork()){
		sleep(1);
		close(1);
		dup(fds[1]);
		close(fds[1]);
		close(fds[0]);
		execlp("./g","./g",NULL);
	}
	system("ps -o pid,ppid,comm");

	wait(0);
}
close(fds[1]);
close(fds[0]);
wait(0);

system("ps -o pid,ppid,comm");



}
