You are on page 1of 2

#include<iostream>

#include<unistd.h>
#include<wait.h>
using namespace std;
int main()
{
pid_t pid1,pid2,pid3;
pid1=fork();
pid2=fork();
pid3=fork();
cout<<endl<<endl;
if(pid1<0||pid2<0||pid3<0)
{
cout<<"FORK FAILED"<<endl;
return-1;
}
else if(pid1==0&&pid2>0&&pid3>0)
{
cout<<"child1 process id:\t"<<getpid()<<endl;
cout<<"child1 parent process id:\t"<<getppid()<<endl<<endl;
}
else if(pid1==0&&pid2==0&&pid3==0)
{
cout<<"child1_child3_chil2 process id:\t"<<getpid()<<endl;
cout<<"child1_child3_chil2 parent process id:\t"<<getppid()<<endl<<endl;

}
else if(pid1==0&&pid2==0&&pid3>0)
{
cout<<"child1_child2 process id:\t"<<getpid()<<endl;
cout<<"child1_child2 parent process id:\t"<<getppid()<<endl<<endl;

}
else if(pid1>0&&pid2==0 && pid3>0)
{
cout<<"child2 process id:\t"<<getpid()<<endl;
cout<<"child2 parent process id:\t"<<getppid()<<endl<<endl;

}
else if(pid1>0 && pid2==0 && pid3==0)
{
cout<<"child2_child3 process id:\t"<<getpid()<<endl;
cout<<"child2_child3 parent process id:\t"<<getppid()<<endl<<endl;

}
else if(pid1>0 &&pid2>0&& pid3==0)
{
cout<<"child3 process id:\t"<<getpid()<<endl;
cout<<"child3 parent process id:\t"<<getppid()<<endl<<endl;

}
else if(pid1==0&&pid2>0&&pid3==0)
{
cout<<"child1_child3 process id:\t"<<getpid()<<endl;
cout<<"child1_child3 parent process id:\t"<<getppid()<<endl<<endl;
}
else if(pid1>0 && pid2>0 && pid3>0)
{
cout<<"in parent process id:\t"<<getpid()<<endl;
cout<<"in parent parent's process id:\t"<<getppid()<<endl<<endl;

pid_t pid;
int status;
pid=wait(&status);
cout<<"in parent: child process with id =\t"<<pid<<endl;
cout<<" has terminated with status=\t"<<status<<endl;

pid=wait(&status);
cout<<"in parent: child process with id =\t"<<pid<<endl;
cout<<" has terminated with status=\t"<<status<<endl;

pid=wait(&status);
cout<<"in parent: child process with id =\t"<<pid<<endl;
cout<<" has terminated with status=\t"<<status<<endl;

return 0;
}

You might also like