Which of the following statements correctly describe the return values of the…
2023
Which of the following statements correctly describe the return values of the fork() system call in a Unix-like operating system?
- A.
0 to the child process and a positive value (child's PID) to the parent process
- B.
1 to the child process and 0 to the parent process
- C.
A negative value if the fork fails
- D.
Both first and third statements
- E.
None of the above
Attempted by 31 students.
Show answer & explanation
Correct answer: D
Concept
The fork() system call creates a new process by duplicating the calling (parent) process. It is called once but returns twice — once in the parent and once in the newly created child. The single return value is what lets each process discover which role it is playing after the duplication.
Return-value rule
On a successful fork(), the kernel hands back three distinct outcomes:
In the child process the return value is 0. The child has no need to learn its own PID this way — it can always obtain it with getpid().
In the parent process the return value is the PID of the newly created child, which is a positive integer. This lets the parent track or wait for that specific child.
If fork() fails (for example, the process table is full), no child is created and the call returns a negative value, conventionally -1, in the parent, with errno set.
Applying it here
The statement "0 to the child and a positive value (the child's PID) to the parent" matches the success behaviour exactly, so it is correct. The statement "a negative value if the fork fails" matches the failure behaviour exactly, so it is also correct. The statement "1 to the child and 0 to the parent" inverts and misstates the values — the child receives 0, not 1, and the parent receives a positive PID, not 0 — so it is wrong. Because two of the listed statements are individually correct, the choice that affirms both of those statements together is the right answer.
Cross-check
A standard idiom confirms this: pid = fork(); if (pid < 0) handle_error(); else if (pid == 0) child_code(); else parent_code();. The three branches — negative, zero, positive — correspond exactly to failure, child, and parent, which is precisely what the two correct statements together describe.