Talha Irfan Talha Irfan - 1 month ago 6
Linux Question

Strange Parent ID

I am using two forks in my code:

int main()
{
cout<<"Main Process ID is: "<<getpid()<<endl;

int processID;

processID = fork();

cout<<"Part 1"<<endl;
cout<<"PID is: "<<getpid()<<" Child PID: "<<processID<<" Parent PID: "<<getppid()<<endl;

processID = fork();

cout<<"Part 2"<<endl;
cout<<"PID is: "<<getpid()<<" Child PID: "<<processID<<" Parent PID: "<<getppid()<<endl;

return 0;
}


But there are some strange "Parent ID" for "children". Why? (Please refer to screenshot below)

enter image description here

Thanks a lot in advance.

Answer

In your case the parent executed first, hence got eventually terminated. In this case child become orphan and hence it's parent id got replaced with the id of other process in same group or the child got re-parented.Hence, the parent id is different(strange).

Comments