Lab 3 walkthrough — Process creation using fork(); orphan and zombie processes
Lab 3 walkthrough — Process creation using fork(); orphan and zombie processes
Recorded by Dr. Krithika L.B. Slot lab03-walkthrough
Theory link: 2.1 Process concept · 2.2 Operations on processes
Lab 3 walkthrough — Process creation using fork(); orphan and zombie processes
Recorded by Dr. Krithika L.B. Slot lab03-walkthrough
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.
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.
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.
Parent exits first; the still-running child is adopted by PID 1 (systemd) or a subreaper, so getppid() changes.
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.
#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;
}#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;
}#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;
}gcc fork_basic.c -o fork_basic (same for zombie.c and orphan.c)../fork_basic../zombie & (in the background), then within 30 s run ps -el | grep defunct or ps -o pid,ppid,stat,cmd — note state Z../orphan; wait 5 s for the child's second line (press Enter to get the prompt back).$ ./fork_basic
Child : PID = 5122, parent PID = 5121
Parent: PID = 5121, child PID = 5122$ ./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
Parent 5140 exiting immediately
Child : my parent is 5140
$ Child : now my parent is 1 (adopted)wait() so the parent prints after the child.ls -l using execlp().ps -p <ppid>.Prepare these for the lab evaluation.
Revealed 0Child's PID (> 0) in the parent, 0 in the child, −1 on failure.
Its PCB entry is kept so that the parent can read its exit status with wait().
The parent calls wait()/waitpid(); or kill the parent so the zombie is adopted by init, which reaps it.
init/systemd (PID 1) or a designated subreaper process.
No — each has its own copy of the address space (copy-on-write).