Fork System Call MCQs: 12 Solved Questions with Explanations

Practise 12 published fork() MCQs, from basic return values to process trees, inherited descriptors and traced C output, with concise worked explanations.

KnowledgeGate Team

Exam prep & CS education

Updated 8 Sep 20268 min read56 views

One written fork() may be executed by several processes and return differently in the parent and child. Return values and process APIs establish the basics; memory, file-descriptor inheritance, process trees and printed-output traces demand careful execution. Threads and Process Creation MCQs pairs thread-model questions with a brief process-creation overview. The narrower fork path runs from return values through descriptor inheritance to complete process-tree and output traces.

Build the fork(), exec() and wait() map first

Use this map when tracing code:

Call or value

What it means

fork()

creates a child; both continue after the call

0

child's return

positive child PID

parent's return

-1

failure, no child created

exec()

replaces the process image

wait()/waitpid()

collects a child's status

Parent P0 (PID 4100) creates child 4101. P0 sees 4101; the child sees 0. Neither fixes scheduling. Three unconditional successful calls give 1 -> 2 -> 4 -> 8 total processes, so 8 - 1 = 7 new children. 2^n applies only if every process reaches every call. See fork() Questions in GATE.

Fork System Call MCQs 1-3: purpose and return values

Question 1

The fork() system call in Unix is used to:

  • (a) Terminate a process

  • (b) Suspend a process

  • (c) Create a new process

  • (d) Merge two processes

Choose.

Correct answer: (c) Create a new process.

fork() duplicates the caller to create a child; both continue at the next statement. exit() terminates, exec() replaces an image, and ordinary writable variables remain separate.

Question 2

What is the return value of the fork() system call in the child process?

  • (a) The PID of the child process

  • (b) The PID of the parent process

  • (c) 0

  • (d) -1

Choose.

Correct answer: (c) 0.

Child 4101 sees 0; parent P0=4100 sees 4101, the child's PID. -1 means failure and creates no child.

Question 3 (IBPS 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

Choose.

Correct answer: (d) Both first and third statements.

Statement (a) gives successful returns; (c) gives failure, conventionally -1. Both are true, so (d) is correct. Failure creates no child.

Fork System Call MCQs 4-6: fork, exec, wait and CreateProcess

Question 4

Consider a simple linux shell implementing the command sleep 100. Which of the following is an accurate ordered list of system calls invoked by the shell from the time the user enters this command to the time the shell comes back and asks the user for the next input?

  • (a) wait-exec-fork

  • (b) exec-wait-fork

  • (c) fork-exec-wait

  • (d) wait-fork-exec

Choose.

Correct answer: (c) fork-exec-wait.

The shell forks; the child uses exec to become sleep 100, while the parent waits. exec creates no process; wait collects status.

Question 5

What does the exec() system call do?

  • (a) Creates a new process

  • (b) Replaces the current process image with a new process image

  • (c) Terminates the current process

  • (d) Pauses the current process

Choose.

Correct answer: (b) Replaces the current process image with a new process image.

If child 4101 calls exec, its image changes but its PID remains 4101; no third process appears. Shells pair fork() with exec().

Question 6 (Beltron Programmer Shift-2 2025)

Which of the following best describes the major difference between UNIX fork() and Windows CreateProcess?

  • (a) CreateProcess creates a new thread in the same process

  • (b) Both are identical in behaviour

  • (c) fork() only runs in GUI environments

  • (d) fork() copies the current process; CreateProcess creates a new one from scratch

Choose.

Correct answer: (d) fork() copies the current process; CreateProcess creates a new one from scratch.

Option (d) is best. fork() duplicates the caller; CreateProcess starts a specified executable. Copy-on-write delays physical copying, but the address spaces remain logically separate.

Fork System Call MCQs 7-9: memory and descriptor inheritance

Question 7

When a process creates a new process using the fork() operation, which of the following is shared between the parent process and the child process?

  • (a) Page table

  • (b) Shared memory segments

  • (c) Stack

  • (d) Heap

Choose.

Correct answer: (b) Shared memory segments.

The child has its own logical stack, heap and page tables, though copy-on-write may reuse physical pages. Explicit shared-memory mappings remain shared; ordinary variables do not.

Question 8

Consider a parent process P that has forked a child process C in the program below.

c
int a = 5;
int fd = open(...);          //opening a file
int ret = fork();
if (ret > 0) {
    close(fd);
    a = 6;
    ...
} else if (ret == 0) {
    printf("a=%d\n", a);
    read(fd, something);
}

After the new process is forked, suppose that the parent process is scheduled first, before the child process. Once the parent resumes after fork, it closes the file descriptor and changes the value of a variable as shown above. Assume that the child process is scheduled for the first time only after the parent completes these two changes. What is the value of the variable a as printed in the child process, when it is scheduled next and will the attempt to read from the file descriptor succeed in the child?

  • (a) 5, Yes

  • (b) 6, Yes

  • (c) 5, No

  • (d) 6, No

Choose.

Correct answer: (a) 5, Yes.

The child copied a=5; the parent's a=6 affects only its copy. The duplicated child descriptor stays open when the parent closes its descriptor, so the read succeeds. Both refer to one open file description.

Question 9 (GATE 2005)

Consider the following code fragment:

c
if (fork() == 0)
{
    a = a + 5;
    printf("%d,%d\n", a, &a);
}
else
{
    a = a - 5;
    printf("%d, %d\n", a, &a);
}

Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?

  • (a) u = x + 10 and v = y

  • (b) u = x + 10 and v != y

  • (c) u + 10 = x and v = y

  • (d) u + 10 = x and v != y

Choose.

Correct answer: (c) u + 10 = x and v = y.

Take a=10: parent u=5, child x=15, hence u+10=x. Separate address spaces use the same virtual address for a, so v=y. Real C uses %p for addresses. The same GATE 2005 item in Threads and Process Creation MCQs compares thread and process memory. Its fork-specific lesson is narrower: copied values diverge, while the virtual address can remain equal.

Fork System Call MCQs 10-12: process counts and printed output

Question 10 (GATE Computer Science 2012; TPSC Senior Informatics Officer 2025)

A process executes the code

c
fork();
fork();
fork();

the total number of child processes created is

  • (a) 3

  • (b) 4

  • (c) 7

  • (d) 8

Choose.

Correct answer: (c) 7.

Start with 1 process. The calls produce 2, 4, then 8 because every process reaches the next call. Children created are 8-1=7, not 8. The same GATE 2012 item in Threads and Process Creation MCQs serves as a compact process-creation example. The fork-only treatment adds the complete tree and separates eight total processes from seven new children.

Process tree for three fork() calls, growing from one process to eight with seven children.

Question 11 (UGC NET June 2020)

Assuming that the system call fork() never fails, consider the following C program 𝑃1 and 𝑃2 executed on a UNIX/Linux system:

c
/* P1 */
int main() {
    fork();
    fork();
    fork();
    printf("Happy\n");
}

/* P2 */
int main() {
    fork();
    printf("Happy\n");
    fork();
    printf("Happy\n");
    fork();
    printf("Happy\n");
}

Statement 𝐼: 𝑃1 displays “Happy” 8 times

Statement 𝐼𝐼: 𝑃2 displays “Happy” 12 times

In the light of the above statements, choose the correct answer from the options given below

  • (a) Both Statement 𝐼 and Statement 𝐼𝐼 are true

  • (b) Both Statement 𝐼 and Statement 𝐼𝐼 are false

  • (c) Statement 𝐼 is correct but Statement 𝐼𝐼 is false

  • (d) Statement 𝐼 is incorrect but Statement 𝐼𝐼 is true

Choose.

Correct answer: (c) Statement 𝐼 is correct but Statement 𝐼𝐼 is false.

P1 calls fork() three times before printing, so 2^3=8 processes print. P2's sites run in 2, 4, 8 processes: 2+4+8=14, not 12. Only Statement I is true.

Question 12

Consider the following C program. Assume there are no syntax errors and the program executes correctly. Assume the fork system calls succeed. What is the output printed to the screen when we execute the below program? (MCQ)

c
void main(argc, argv) {
    for (int i = 0; i < 4; i++)
    {
        int ret = fork();
        if (ret == 0)
            printf("child %d\n", i);
    }
}
  • (a) The statement "child i" is printed exactly 4 times for each i from 0 to 3.

  • (b) The statement "child i" is printed 8 times for i=0, 16 times for i=1, and so on.

  • (c) The statement "child i" is printed 2^i times for i=0 to 3

  • (d) None of these

Choose.

Correct answer: (c) The statement "child i" is printed 2^i times for i=0 to 3.

Every process forks, but only new children have ret==0 and print. Counts are 2^0=1, 2^1=2, 2^2=4, 2^3=8: 1+2+4+8=15 lines, unordered.

The fork() traps to check before choosing an option

For Question 11, place 2, 4, 8 beside P2's print sites: 14, not 12. For Question 12, write new-child counts 1, 2, 4, 8, not totals.

Diagnose the score and choose the next step

Classify each miss by trace type: Questions 1-3 test return values, Questions 4-6 test process APIs, Questions 7-9 test inheritance, and Questions 10-12 test process counts and output. For a miss in Questions 10-12, redraw the process population after every fork() and label each print site with the number of processes that reach it. For a miss in Questions 7-9, write separate parent and child state columns before applying any updates. Retry only the missed questions without options, then repeat all 12 the next day.

Continue with GATE Guidance by Sanchit Sir or CS Fundamentals for Placements by Sanchit Sir. GATE CS Exam Preparation covers the wider subject.