LearnFrenzy
Sign in Take a free test

Operating Systems · Topic 2 of 11

Processes, Threads and fork(): questions with answers

Process states and the PCB, fork(), exec() and wait() output puzzles, zombies and orphans, threads vs processes, IPC, schedulers and copy-on-write.

Questions
45
Timed sets
3
Answers run or computed
17
Your accuracy
–

What this topic covers

9 subtopics
  • fork()17
  • Threads7
  • exec() and wait()5
  • Process basics5
  • IPC3
  • Zombies and orphans3
  • Process states2
  • Schedulers2
  • Signals1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

"How many times does this print?" fork() puzzles are a favourite of placement tests and GATE. Each puzzle here was compiled and run on a real Unix system with terminal-style output buffering, so the counts are what you would see on screen. Around them: process states and the PCB, exec() and wait(), zombies and orphans, threads versus processes and inter-process communication.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Process states Beginner Practice

In the five-state model (new, ready, running, waiting, terminated), which transition is not possible?

Waiting → Ready
Running → Waiting
Running → Ready
Waiting → Running
Show answer and explanation
Correct answer D

Explanation

When its I/O completes, a waiting process becomes ready; only the scheduler moves a ready process to running. Running → waiting happens on an I/O request, running → ready on a timer interrupt.

Why the other options are wrong

  • AHappens when the I/O completes.
  • BHappens on an I/O request.
  • CHappens on preemption.
Question 2 fork() Intermediate Practice Answer computed

How many times is x printed?

C main.c
#include <stdio.h>
#include <unistd.h>

int main(void) {
    if (fork() && fork())
        fork();
    printf("x\n");
    return 0;
}

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 4

Explanation

The first fork gives a parent (non-zero) and a child (0). && short-circuits in the child. The parent runs the second fork: now 3 processes. Only the original parent sees both values non-zero, so it forks once more: 4 processes.

Question 3 fork() Advanced Practice Answer computed

There is no newline now. How many dashes appear on the terminal?

C main.c
#include <stdio.h>
#include <unistd.h>

int main(void) {
    for (int i = 0; i < 2; i++) {
        fork();
        printf("-");
    }
    return 0;
}

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 8

Explanation

Without a newline the dashes stay in each process's stdio buffer, and fork() copies the buffer. After the loop there are 4 processes; each holds "--" (one dash from each iteration, inherited or its own), and flushes it at exit: 4 × 2 = 8. With "-\n" it would be 6.

42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

Operating Systems

All 11 topics

Topics, levels and full mocks
  1. 01 OS Basics and System Calls45 questions · 3 levels
  2. 02 Processes, Threads and fork()45 questions · 3 levels
  3. 03 CPU Scheduling Algorithms45 questions · 3 levels
  4. 04 Process Synchronization and Semaphores45 questions · 3 levels
  5. 05 Deadlocks and Banker's Algorithm45 questions · 3 levels
  6. 06 Memory Management and Paging45 questions · 3 levels
  7. 07 Virtual Memory and Page Replacement45 questions · 3 levels
  8. 08 File Systems and Disk Scheduling45 questions · 3 levels
  9. 09 Threads and Concurrency in Code45 questions · 3 levels
  10. 10 Linux Commands and Shell45 questions · 3 levels
  11. 11 GATE-Style OS Questions (MSQ & NAT)45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links