COOLBEANS
COOLBEANS

Reputation: 757

Child process always zero and always unique after fork()?

I've read man and elsewhere, but I'm struggling with this concept. The child process is always unique, but in any example of forking I've found the child's pid must = 0. What if there aremany children, they can't all be zero or they wouldn't be unique?

Upvotes: 3

Views: 2842

Answers (1)

zwol
zwol

Reputation: 140540

The child process's pid is never zero. fork returns zero to the child to tell it that it is the child. The child process's pid, however, is the value that fork returns to the parent. (Remember that fork, assuming it succeeds, returns twice -- once in the child, once in the parent.) You can confirm this by writing a program that compares the result of getpid in the child to the value fork returns to the parent (with a little IPC).

Upvotes: 7

Related Questions