#include <iostream>
using namespace std;

#include <stdlib.h> 
#include <sys/types.h> 
#include <unistd.h> 

/* getpid() and fork() are system calls declared in unistd.h.  They return */
/* values of type pid_t.  This pid_t is a special type for process ids. */
/* It's equivalent to int. */

int main(int argc, char *argv[])
{ 
	pid_t childpid;
	int count;

	if (argc != 2)
	{
		cerr << "Usage:  forkbomb <int>" << endl;
		exit(1);
	}

	count = atoi(argv[1]);
	if (count > 4)
	{
		cerr << "Integer parameter has been reduced to 4" << endl; 
		count = 4;
	}

	for (int i = 1;  i <= count;  i++) {
/*
		cout 	<< "- Before fork, i = " << i << ", process " 
			<< getpid() 
			<< ", with parent " 
			<< getppid() 
			<< endl;
*/
		childpid = fork();
/*
		cout 	<< " --- After fork, i = " << i << ", process " 
			<< getpid() 
			<< ", with parent " 
			<< getppid() 
			<< endl << endl;
*/
	}

	cout 	<< "---- Terminating process " << getpid() 
		<< endl;

	return 0;
}

