
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <signal.h>
#include <wait.h>

//CSE 660
//Jason Kim
//Lab2

using namespace std;

int main()
{
	int x,y,z,a,b,c,d;
	int pida;
	pida = getpid();
	cout << "Process A starting with pid of " << pida << endl;
	char *arg[1]={0};
	char *arg2[25]={"hello","world"};

	x=fork();
	if(x==0)
	{
		execv("BB.o",arg); //process BB
	}
	
	y=fork();
	if(y==0)
	{
		execv("CC.o",arg); //process c1
	}
	cout << "Process A Waiting for Child to Terminate" << endl;
	
	wait(NULL); //waits for child to terminate

	z=fork();
	if(z==0)
	{
		execv("DD.o",arg); //process DD
	}
	
	sleep(5);
	a=fork();
	if(a==0)
	{
		execv("CC.o",arg);//process c2
	}
	sleep(2);
	c=fork();
	if(c==0)
	{
		execv("EE.o",arg);//process EE
	}
	sleep(2);
	
	int *m=0;
	waitpid(z,m,0); //waits for process DD

	
		
	cout << "Process A - Killing Process C1 and C2" << endl;
	kill(y,9); //kills C1 and C2
	kill(a,9);	
	
	
	d=fork();			
	if(d==0)
	{
		execv("FF.o",arg2);
	}
	
	sleep(2);
	cout << "Terminating Process A" << endl;

	return 0;

}

