#include <iostream>
#include <unistd.h>
// #include <cstdlib>
// #include <sys/types.h>
// #include <sys/stat.h>

using namespace std;

int main(){
	pid_t pid = fork();

	if (pid<0){
		return 1;
	}
	if (pid > 0){
		// original parent
		cout << "Zombie factory start. PID = " << pid << endl;
		exit(0);
	}

	setsid();
	cout << "zombie factory running in the background" << endl;
	cout << "background PID: " << getpid() << endl;

	//create 5 zombies

	for  (int i=0; i<5;i++){
		pid_t child = fork();
		if (child == 0) {
			_exit(0);
		}
		sleep(2);
	}

	cout << "Created 5 zombies."<<endl;
	cout << "Background parent sleeping forever..." << endl;
	while (true)
		sleep(100);

	return 0;
}
