Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 77 ยท C, C++ and Systems Interview Questions

Operating Systems: PCB, Process States, IPC, Pipes and Zombies

Answer operating system interview questions on the PCB, process states, the dispatcher, pipes, shared memory, signals and zombies with real C and runnable scheduling.

The first operating systems lesson (Processes, Threads and Deadlocks) covers context switches, the basic scheduling policies, deadlock and paging. This one covers the rest of the process chapter that interviews love: what the OS stores about a process, how a process moves between states, how processes talk to each other (IPC), what a zombie is, and how to compute waiting and response times for a preemptive scheduler.

Unlike most lessons in this track, the systems examples here are real C programs that run on a Linux sandbox through the in-page runner, with fork, pipe, mmap and signals. Related reading: the Linux tutorial and the C tutorial.

A process in five states

New Being created.
Ready Waiting for a CPU.
Running On the CPU now.
Waiting Blocked on I/O or a lock.

Terminated is the fifth state. A zombie is terminated but not yet reaped.

1 / 2

What is a process control block (PCB)?

Short answer: the PCB (called task_struct in Linux) is the kernel's record of one process. Everything the OS needs to stop the process and resume it later is stored there.

Typical contents:

  • Identity: process ID (PID), parent PID, user and group IDs.
  • State: new, ready, running, waiting or terminated.
  • CPU context: the program counter and registers, saved at a context switch.
  • Scheduling data: priority, time used, which queue it is in.
  • Memory management data: page table pointer, segment limits.
  • I/O and files: the table of open file descriptors, current directory.
  • Accounting and signals: CPU time used, pending signals.

A context switch saves the running process's registers in its PCB and loads the next process's registers from its PCB. The dispatcher is the kernel code that performs that last step: it hands the CPU to the process the scheduler selected, switches to user mode and jumps to the saved program counter. Dispatch latency is the time it takes. The scheduler decides who; the dispatcher does the switching.

What are the process states?

A process starts new, becomes ready when it is admitted, is running when the scheduler picks it, and returns to ready if its time slice ends (preemption). If it must wait for a disk read, a network packet or a lock, it becomes waiting (blocked) until the event completes, then goes back to ready, not straight to running. When it ends it is terminated.

stateDiagram-v2
    [*] --> New
    New --> Ready: admitted
    Ready --> Running: scheduler dispatch
    Running --> Ready: time slice over
    Running --> Waiting: I/O or lock
    Waiting --> Ready: event complete
    Running --> Terminated: exit

Every blocked process wakes into the ready queue, never directly onto the CPU.

How does fork create a process, and how do parent and child talk?

On Unix, fork() creates a copy of the calling process. It returns twice: 0 in the child and the child's PID in the parent. The copy starts with the same memory contents (copy-on-write, so pages are shared until one side writes) and duplicated file descriptors. exec() then replaces the child's program, and wait/waitpid lets the parent collect the child's exit status.

Because the child has its own memory, assigning to an ordinary variable in the child does not change the parent's variable. To communicate you need an IPC mechanism.

What is IPC, and what are the main mechanisms?

Inter-process communication (IPC) is any way for separate processes to exchange data or coordinate. There are two models:

  • Shared memory: the kernel maps one region into both address spaces. After setup, communication needs no system calls and no copying, so it is the fastest, but the processes must synchronize themselves (semaphores, mutexes in the shared region) or they race.
  • Message passing: processes send and receive messages through the kernel (pipes, message queues, sockets). It is slower because the kernel copies the data, but safer, simpler to reason about, and it works across machines (sockets).
Mechanism Direction Related processes only? Notes
Unnamed pipe One way Yes (inherited through fork) Byte stream, pipe()
Named pipe (FIFO) One way No Has a name in the file system, mkfifo
Message queue Messages No Kernel-held queue, preserves boundaries
Shared memory Any No Fastest, needs synchronization
Socket Two way No, even across hosts Network or Unix-domain
Signal One way, tiny No A number, no payload; handler runs asynchronously

Pipes

A pipe is a kernel buffer with two file descriptors: one to read, one to write. Reading from an empty pipe blocks; writing to a full pipe blocks; reading after every write end is closed returns end-of-file. That last rule is why each side should close the end it does not use. The shell's ls | wc -l is two processes connected by a pipe.

C Real C: parent reads what the child writes into a pipe

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Shared memory versus a private copy

The next program shows the difference that matters: a normal variable is copied by fork, while a MAP_SHARED mapping is the same physical memory for both processes. Real programs use shm_open or mmap with a name; an anonymous shared mapping works between a parent and the children it forks.

C Real C: a shared mapping versus an ordinary variable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

If both processes wrote the shared integer at the same time you would have a race condition, exactly as with threads. That is the cost of the speed.

Signals

A signal is a small asynchronous notification: SIGINT (Ctrl+C), SIGTERM (polite stop), SIGKILL (cannot be caught), SIGSEGV (bad memory access), SIGCHLD (a child ended), SIGUSR1 (yours to define). A handler runs on the process's own stack when the signal arrives, so it must do very little (set a flag, write); most library calls are not async-signal-safe.

C Real C: install a handler and send yourself SIGUSR1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

What are zombie and orphan processes?

A zombie is a process that has finished (it called exit) but whose parent has not yet called wait to read its exit status. The process is gone, but its PCB entry stays in the process table so the status can still be collected. It uses no CPU or memory beyond that entry, but each zombie holds a PID, and a parent that never reaps can exhaust the PID table. You cannot kill a zombie, because it is already dead; you make the parent call wait, handle SIGCHLD, or kill the parent so init (PID 1) adopts and reaps the zombies.

An orphan is the opposite: the parent ended first while the child still runs. The kernel re-parents the child to init (or a subreaper), which reaps it when it ends, so orphans are harmless.

C Real C: watch a child become a zombie, then get reaped

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Z means zombie; after waitpid the entry is gone (?). In a shell, a zombie shows up as <defunct> in ps.

How do you compute waiting, turnaround and response time?

Interview questions often hand you a table of arrival and burst times and ask for averages. Memorize the three formulas:

  • Turnaround time = completion time - arrival time.
  • Waiting time = turnaround time - burst time (time spent in the ready queue).
  • Response time = first time on the CPU - arrival time.

The basic FCFS, SJF and round-robin policies are in the first OS lesson. Here is the preemptive version of SJF, shortest remaining time first (SRTF), next to round robin with a quantum of 2, on the classic input P1(0, 7), P2(2, 4), P3(4, 1), P4(5, 4). SRTF preempts whenever a job arrives with less time remaining than the running job.

Python SRTF versus round robin with arrival times

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

SRTF gives the smallest average waiting time (3.0 here) but makes P1, the long job, wait until 16. Round robin is fairer and has better response, at the price of a higher average wait. Round-robin results depend on one convention: whether a job that arrives during a quantum queues ahead of the preempted job (as above) or behind it. State your convention in an exam or interview.

Multilevel feedback queue (MLFQ) is the scheduler real systems approximate: several queues with different priorities and quanta; a new job starts in the top queue, a job that uses its whole quantum moves down (it looks CPU-bound), and a job that blocks early stays high (it looks interactive). Periodic aging or priority boosts stop starvation.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the process control block store?

    The PCB is the kernel's per-process record. At a context switch the registers go into the old PCB and come out of the new one.
  2. 2. A process blocked on disk I/O finishes waiting. Which state does it enter next?

    A woken process must wait for the scheduler to pick it again, so it joins the ready queue rather than going straight to the CPU.
  3. 3. Why is shared memory the fastest IPC mechanism, and what does it cost?

    After the mapping exists, processes read and write directly. There is no per-message system call or copy, so races are your responsibility.
  4. 4. A zombie process is...

    The process has exited but its table entry remains until the parent calls wait. Killing the parent lets init adopt and reap the zombie.
  5. 5. A job has arrival time 3, burst time 5 and completes at time 14. What are its turnaround and waiting times?

    Turnaround = completion - arrival = 14 - 3 = 11. Waiting = turnaround - burst = 11 - 5 = 6.

Summary

  • The PCB stores a process's identity, state, saved registers, memory and file information; a context switch saves one PCB's context and loads another's, and the dispatcher hands over the CPU.
  • Processes move new, ready, running, waiting, terminated; a woken process returns to ready.
  • fork copies a process; communication needs IPC: pipes, queues, sockets, signals (message passing) or shared memory (fast, needs synchronization).
  • A zombie is a dead process not yet reaped by its parent; an orphan is a live process whose parent died and is adopted by init.
  • Turnaround = completion - arrival; waiting = turnaround - burst; response = first run - arrival. SRTF minimizes average waiting but can starve long jobs; MLFQ approximates it without knowing burst times.

Next: Go and Rust Interview Questions. For deadlock avoidance (Banker's algorithm), Peterson's solution, page tables and RAID, see the second half of Processes, Threads and Deadlocks.