#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
using namespace std;

int main(void){
	int k = 3;
	int  status;
	pid_t pid;
	pid=fork();
	//int k=2;
	if (pid<0){
		cout << "fork() error !" << endl;
		_exit(1);
	}
	if (pid == 0){
		k = 2;
		//child process
		cout << "Child process PID = " << getpid() << endl;
		cout << "Parent process PID = " << getppid() << endl;
		sleep(5);
		cout << "Child k=" << k <<endl;
		_exit(-10);
	} else {
		//parent process
		cout << "Parent process PID = " << getpid() << endl;
		cout << "Parent of parent PID = " << getppid() << endl;
		pid = wait(&status);
		if (WIFEXITED(status)) {
			cout << "Process PID = " << pid << "finished with code:" << WEXITSTATUS(status) << "from process with PID:" << getpid() <<endl;
		}
		cout << "Parent k = " << k << endl;
	}
	return 0;
}
