arrow
arrow
arrow
What does the fork() command return in a Unix-like operating system?
Question



What does the fork() command return 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 (a) and (c)

E.

None of the above

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).

Free Tests

Free
Must Attempt
Video Solutions

OICL AO Prelims Full Mock Test - 01

languageIcon English
  • pdpQsnIcon100 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English
Free
Must Attempt

OICL AO Pre 2025 : Quant Section Test 01

languageIcon English
  • pdpQsnIcon35 Questions
  • pdpsheetsIcon35 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt
Video Solutions

OICL AO Pre 2025 : Reasoning Section Test 01

languageIcon English
  • pdpQsnIcon35 Questions
  • pdpsheetsIcon35 Marks
  • timerIcon20 Mins
languageIcon English
test-prime-package

Access ‘IBPS SO IT Officer’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
319k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow