Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 2.2: Operations on processes: creation, termination and cooperation

How processes are born with fork(), transformed with exec(), collected with wait() and ended with exit() — plus zombies, orphans and why processes cooperate.

Module 2 1.5 hours Theory CO2
← Before
2.1 Process concept
States, PCB, queues
You are here
2.2 Operations on processes: creation, termination and cooperation
Module 2 · Process, Scheduling, Threads and Deadlocks
Next →
2.3 Threads
Lightweight processes

Learning objectives

By the end of this session you should be able to:

  • Describe the process tree, PIDs and the design choices in process creation.
  • Predict the output of programs that use fork(), exec(), wait() and exit().
  • Count the processes created by a sequence of fork() calls.
  • Explain how zombie and orphan processes arise and how the OS deals with them.
  • Give four reasons for cooperating processes and connect them to IPC.

Lecture notes

1. Process creation and the process tree

A process (the parent) can create new processes (children), which can create more — forming a process tree rooted at PID 1 (systemd). Each process has a unique integer PID and knows its parent's PID (PPID). Try pstree -p on Linux to see the tree.

When a parent creates a child, the designer has to decide:

Resource sharing

Parent and child share all, some, or none of the resources.

Execution

Parent continues concurrently with the child, or waits until the child terminates.

Address space

Child is a duplicate of the parent (UNIX fork), or loads a new program (exec / Windows CreateProcess).

2. fork(), exec(), wait(), exit() — the UNIX way

  • fork() creates a child that is an almost exact copy of the parent. It is called once but returns twice: 0 in the child, the child's PID (> 0) in the parent, and -1 on failure.
  • exec() (e.g. execlp) replaces the calling process's memory with a new program. It does not return on success.
  • wait() blocks the parent until a child terminates and collects its exit status.
  • exit(status) terminates the calling process and returns status to the parent.
fork_exec.c — the shell pattern: fork, exec in the child, wait in the parent
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
    pid_t pid = fork();                 /* one call, two returns */

    if (pid < 0) {                      /* error */
        perror("fork");
        exit(1);
    } else if (pid == 0) {              /* child */
        printf("Child  PID=%d, parent=%d\n", getpid(), getppid());
        execlp("ls", "ls", "-l", NULL); /* replace child with ls */
        perror("exec");                 /* only reached if exec fails */
        exit(1);
    } else {                            /* parent */
        int status;
        wait(&status);                  /* block until child ends */
        printf("Parent PID=%d: child %d finished with status %d\n",
               getpid(), pid, WEXITSTATUS(status));
    }
    return 0;
}

One process (the shell, PID 100) is running main().

PID 100

The kernel creates PID 101: a copy of the address space, open files and registers. Both processes now continue from the line after fork().

PID 100 · pid=101→PID 101 · pid=0

The return value is different, so the if sends the child into the pid == 0 branch and the parent into the else branch.

The child calls execlp("ls", …): its code, data, heap and stack are replaced by the ls program. PID stays 101.

PID 100 (waiting)→PID 101 now running ls

The parent is in the Waiting state inside wait() until the child terminates.

ls finishes and exits. The kernel records its status; the parent wakes up, collects the status, and the child's PCB is finally removed.

How many processes does this create?
fork();
fork();
fork();
printf("hello\n");

Each fork() doubles the number of processes: 1 → 2 → 4 → 8. So 8 processes print hello (2n for n forks), of which 7 are new.

3. Process termination

A process terminates when it executes its last statement or calls exit(). The OS deallocates its memory, open files and buffers. A parent may also terminate a child (kill()) if the child exceeds its resources, its task is no longer needed, or the parent itself is exiting. Some systems use cascading termination: if a parent ends, all its children are terminated too.

Zombie and orphan processes

Zombie

A child that has terminated but whose parent has not yet called wait(). Its memory is freed, but its PCB entry (PID + exit status) remains so the parent can collect it. Shown as <defunct>, state Z in ps.

Too many zombies can exhaust the process table.

Orphan

A child whose parent terminated first, without waiting. The orphan keeps running and is re-parented to init/systemd (PID 1) or a subreaper, which calls wait() on it when it ends.

Daemons are often deliberately orphaned.

You will create both deliberately in Lab 3 and watch them with ps -el.

4. Cooperating processes

Processes are independent if they cannot affect or be affected by others, and cooperating if they can. Reasons to cooperate:

  • Information sharing — several applications need the same data (copy-paste, a shared file).
  • Computation speed-up — split a task into subtasks that run in parallel on multiple cores.
  • Modularity — build a system as separate cooperating processes (e.g. Chrome's browser, renderer and plug-in processes).
  • Convenience — a user edits, compiles and prints at the same time.

Cooperation requires inter-process communication (IPC) — shared memory or message passing — and, because shared data can be corrupted by concurrent access, synchronization. Both are the subject of Module 3.

VideoBytes

fork(), exec(), wait() and exit()

Video coming soon

fork(), exec(), wait() and exit()

Recorded by Dr. Krithika L.B. Slot m2-fork-exec

Zombie and orphan processes

Video coming soon

Zombie and orphan processes

Recorded by Dr. Krithika L.B. Slot m2-zombie-orphan

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
What does fork() return, and to whom?

0 to the child, the child's PID to the parent, −1 to the parent if creation failed.

How many times is "hi" printed by for (i = 0; i < 3; i++) fork(); printf("hi\n");?

23 = 8 times — every process existing after the loop prints once.

What happens to the code after a successful exec() call?

It never runs: exec replaces the whole memory image, so control never returns to the old program.

How does a zombie differ from an orphan?

A zombie has finished but its parent hasn't waited for it (only a PCB entry remains). An orphan is still running but its parent has exited; it is adopted by init/systemd.

Why do cooperating processes need synchronization?

If two processes update shared data at the same time, the result can depend on the order of their instructions (a race condition) — Module 3.

Key takeaways

  • Processes form a tree; fork() duplicates the caller and returns twice (0 to child, PID to parent).
  • exec() replaces the program; wait() collects a child's status; exit() terminates.
  • Zombie = dead child not yet waited for; orphan = live child whose parent died (adopted by PID 1).
  • Cooperating processes need IPC and synchronization — Module 3.

Where this leads

Creating a whole process for every task is expensive — a full copy of the address space, a new PCB, a context switch to reach it. Can several flows of control share one process? Yes: threads.

Continue to 2.3 — Threads and multithreading models →