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

Lab 3: Process creation using fork(); orphan and zombie processes

Theory link: 2.1 Process concept · 2.2 Operations on processes

Experiment 3 of 15 Module 2 2 hours

Video walkthrough

Lab 3 walkthrough — Process creation using fork(); orphan and zombie processes

Video coming soon

Lab 3 walkthrough — Process creation using fork(); orphan and zombie processes

Recorded by Dr. Krithika L.B. Slot lab03-walkthrough

Aim

Aim. To create child processes with fork(), distinguish parent and child using the return value and process IDs, and demonstrate zombie and orphan processes.

Requirements: Linux/WSL, gcc.

Explanation

fork() creates a new process by duplicating the caller (Session 2.2). It returns 0 in the child and the child's PID in the parent. getpid() gives a process's own PID, getppid() its parent's.

Zombie

Child has exited, parent is alive but has not called wait(). Visible in ps with state Z / <defunct>. Disappears once the parent waits or exits.

Orphan

Parent exits first; the still-running child is adopted by PID 1 (systemd) or a subreaper, so getppid() changes.

Environment

These programs use POSIX system calls (fork, pthread, shm, semaphore) and must be compiled and run on Linux or WSL — not with Windows MinGW/Turbo C.

Code

Program 1 · Parent and child

fork_basic.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
    pid_t pid = fork();

    if (pid < 0) {
        printf("fork failed\n");
        exit(1);
    } else if (pid == 0) {
        printf("Child : PID = %d, parent PID = %d\n", getpid(), getppid());
    } else {
        wait(NULL);                       /* wait for the child to finish */
        printf("Parent: PID = %d, child PID = %d\n", getpid(), pid);
    }
    return 0;
}

Program 2 · Zombie process

zombie.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t pid = fork();

    if (pid == 0) {
        printf("Child %d exiting now\n", getpid());
        exit(0);                          /* child dies immediately */
    } else {
        printf("Parent %d sleeping 30 s without calling wait()\n", getpid());
        sleep(30);                        /* child is a zombie meanwhile */
        printf("Parent exiting\n");
    }
    return 0;
}

Program 3 · Orphan process

orphan.c
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    pid_t pid = fork();

    if (pid == 0) {
        printf("Child : my parent is %d\n", getppid());
        sleep(5);                         /* parent exits during this sleep */
        printf("Child : now my parent is %d (adopted)\n", getppid());
    } else {
        printf("Parent %d exiting immediately\n", getpid());
    }
    return 0;
}

How to run

  1. Compile: gcc fork_basic.c -o fork_basic (same for zombie.c and orphan.c).
  2. Run ./fork_basic.
  3. Run ./zombie & (in the background), then within 30 s run ps -el | grep defunct or ps -o pid,ppid,stat,cmd — note state Z.
  4. Run ./orphan; wait 5 s for the child's second line (press Enter to get the prompt back).

Sample input & output

fork_basic

Input
(none)
Output
Output
$ ./fork_basic
Child : PID = 5122, parent PID = 5121
Parent: PID = 5121, child PID = 5122

zombie + ps

Input
./zombie & then ps
Output
Output
$ ./zombie &
Parent 5130 sleeping 30 s without calling wait()
Child 5131 exiting now
$ ps -o pid,ppid,stat,cmd
  PID  PPID STAT CMD
 4870  4869 Ss   bash
 5130  4870 S    ./zombie
 5131  5130 Z    [zombie] <defunct>
 5132  4870 R+   ps -o pid,ppid,stat,cmd

orphan

Input
(none)
Output
Output
$ ./orphan
Parent 5140 exiting immediately
Child : my parent is 5140
$ Child : now my parent is 1 (adopted)

Try it yourself

  1. Create a child that prints the numbers 1–5 while the parent prints 6–10; use wait() so the parent prints after the child.
  2. Modify fork_basic.c so the child runs ls -l using execlp().
  3. Write a program that creates exactly 3 children of the same parent (not a chain).
  4. On a desktop Linux, the orphan may be adopted by a PID other than 1. Find out which process it is with ps -p <ppid>.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
What does fork() return in the parent and in the child?

Child's PID (> 0) in the parent, 0 in the child, −1 on failure.

Why does a zombie still appear in ps if it is dead?

Its PCB entry is kept so that the parent can read its exit status with wait().

How do you remove a zombie?

The parent calls wait()/waitpid(); or kill the parent so the zombie is adopted by init, which reaps it.

Who adopts an orphan?

init/systemd (PID 1) or a designated subreaper process.

Do parent and child share variables after fork()?

No — each has its own copy of the address space (copy-on-write).