Correct option is D
In a Unix-like operating system, the
fork() system call is used to create a new process by duplicating the calling process. The return value of
fork() is as follows:
·
0 to the child process: The newly created process receives 0 as its return value from the fork call.
·
A positive value (child's PID) to the parent process: The parent process receives the Process ID (PID) of the child.
·
A negative value if fork fails: If the fork system call fails (e.g., due to insufficient resources), it returns a negative value.
Thus, the correct answer is
(d) Both (a) and (c).
Important Key Points
1.
fork() Behavior:
· It creates a new process by duplicating the calling process.
· The child process is an almost identical copy of the parent, except for its PID and return value from fork().
2.
Return Values:
·
0: Returned to the child process.
·
Positive value (child’s PID): Returned to the parent process.
·
Negative value: Indicates fork failure, such as when the system is out of process table entries or memory.
3.
Practical Use: fork() is often used in multitasking and multiprocessing to spawn new processes.
Knowledge Booster
·
How fork() works:
· The child process gets its own copy of the parent’s memory, including stack and heap.
· Both processes continue executing from the instruction after the fork() call.
·
Example Code:
Output:
· Parent process: "This is the parent process, child PID: <child_pid>".
· Child process: "This is the child process".
Common Issues with fork():
· Overuse of fork() can lead to
fork bomb, a situation where too many processes exhaust system resources.
· fork() doesn't return to the child or parent in the same way in modern threading environments (use pthread or exec() family functions for more control).