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

using namespace std;

void dowork(pid_t mpid,int i){
	cout << "Child " << i << endl;
	cout << " PID:  " << mpid << endl;
	cout << " PPID: " << getppid() << endl;
	sleep(5);
}

int main(){
	cout << "Parent" << endl;
	cout << " PID:  " << getpid() << endl;
	cout << " PPID: " << getppid() << endl << endl;

	pid_t pid;
	int i;

	for (i=0;i<3;i++){
		pid = fork();
		if (pid==0){
			break;
		}
	}

	if (pid==0){
		cout << "Scheduling" << endl;
		dowork(getpid(),i);
	}
	else {
		cout << "\n\nParent waiting for children \n\n";
		pid_t pd;
		for (i=0;i<3;i++){
			pd=wait(nullptr);
			if (pd!=0) {
				cout << "Child " << i << " terminated\n";
			}
		}
	}

	return 0;
}
