0
votes

Why doesn't the fork() system call return the process ID of the parent to the child and return zero to parent?

pid_t pid;
pid = fork();

In this section, the return code for the fork() is zero for the child and the (non zero) process id of the child is the returned to the parent.

How can i find out the PID of the parent from the child?

2
stackoverflow.com/questions/46368813/… it is all very well explained over here - mariatsamp

2 Answers

0
votes

You could save it before doing the fork(). fork() is written to return the id that wouldn't be known until the fork had been done.

0
votes

You can always get the PID of the parent before the fork and keep it in a variable. It will be the same after the fork. Just run:

pid_t parent;
....
parent = getpid();
pid = fork(); //If pid is 0 your parent's pid is in parent