Reputation: 283
It's a simple one, but i'm not sure of the solution
int main() {
c = fork();
fork();
fork();
}
I think 8...also is the value of c=0 ? If so, why?
Upvotes: 0
Views: 127
Reputation: 2897
The answer is: 7 processes are spawn by the fork.
First you have the main processes. It forks itself, creating a new process. Let's call it child1.
Now, main and child1 both fork themselves, creating child2 and child3.
Now, main, child1, child2 and child3 fork again.. creating child4, child5, child6 and child7.
c is equal to 0, only in child1 process if the first fork succeeds.
(from man fork):
RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child.
Keep in mind that each of these processes has its own PID, which is different from the PID of any other existing process. After each fork, the parent knows the PID of the spawned child.
Upvotes: 2
Reputation: 283
In which process do you want to know that value ? The son process will have c = 0, and the father process will have its son's pid value in c
Upvotes: 0