4.2.5 · HinglishOperating Systems

Process creation — fork(), exec(), wait(), exit()

2,381 words11 min readRead in English

4.2.5 · Coding › Operating Systems


YE CHAAR KYUN EXIST KARTE HAIN


fork() — do-return wali mystery

Kya share hota hai vs. kya copy hota hai?

Resource fork() ke baad
Memory (code/data/heap/stack) Logically copied (practically Copy-on-Write)
Open file descriptors Shared (same underlying file table entry, shared offset)
PID Different (child ko naya milta hai)
Pending signals Nahi milte (cleared)
CPU registers / program counter Copy hote hain → dono same instruction par resume karte hain

exec() — replace karo, return mat karo

execlp("ls", "ls", "-l", NULL);   // try to become 'ls'
perror("exec failed");            // ONLY reached if exec() failed
exit(1);

exit() aur wait() — handshake

Figure — Process creation — fork(), exec(), wait(), exit()

Scratch se derivation: canonical pattern


Common mistakes (Steel-manned)


Flashcards

fork() kaun si teen categories ki values return kar sakta hai aur kisko?
0 child ko, child ka PID (>0) parent ko, aur -1 failure par.
Child ko fork() se apna PID milne ki jagah 0 kyun milta hai?
Child apna PID getpid() se already le sakta hai; 0 ek free unambiguous "tum child ho" flag hai, aur parent ko actual PID ki zyada zarurat hoti hai.
Copy-on-Write kya hai aur ye fork() ko sasta kyun banata hai?
Pages read-only share hote hain aur sirf write hone par copy hote hain, isliye tum sirf usi memory ka cost dete ho jo actually modify hoti hai.
Ek successful exec() kabhi return kyun nahi karta?
Woh calling process ki memory image overwrite kar deta hai, isliye exec() ke baad wali instruction exist nahi karti; control naye program ke main ko jaata hai.
fork() ke baad, open file descriptors shared hote hain ya copied?
Shared — dono processes same open file table entry ko refer karte hain (aur file offset share karte hain).
Zombie process kya hoti hai?
Ek child jo terminate ho chuka ho lekin jiska exit status parent ne wait() ke zariye abhi collect nahi kiya.
Jab parent mar jaata hai toh orphaned children ka kya hota hai?
Unhe init (PID 1) ke paas re-parent kar diya jaata hai, jo unhe reap karta hai.
3 consecutive fork() calls ke baad kitne processes exist karte hain?
.
WEXITSTATUS(status) tumhe kya deta hai?
Woh value ke low 8 bits jo child ne exit() ko pass kiye the.
Shell ko exec() se pehle fork() kyun karna padta hai?
Taaki parent (shell) survive kare; exec() warna shell ko hi naye program se replace kar deta.
Fork fail hone ka return value pattern kya hai aur use kaise detect karo?
fork() -1 return karta hai aur koi child create nahi hota; if (pid < 0) check karo.

Recall Feynman: ek 12-saal ke bacche ko samjhao

Socho tumhare paas ek robot hai jo pehle se kaam karna jaanta hai. Bilkul naaya robot banana bahut mehnat ka kaam hai, isliye tum ek "copy" button (fork) dabaate ho aur ab do identical robots hain. Ek robot "boss" hai aur ek "helper" — woh bata sakte hain ki woh kaun hain kyunki copy button boss ko helper ka naam wali sticky note deta hai, aur helper ko blank note milti hai (woh 0). Helper phir ek naya instruction manual padh sakta hai aur bilkul alag robot ban sakta hai (exec) — same body, naya brain. Jab helper apna kaam khatam karta hai toh woh ek chota flag uthata hai jisme likha hota hai "kaam hua, yeh mera score hai" (exit). Boss us flag ka intezaar karta hai (wait), score padhta hai, aur tabhi helper ko hatane deta hai — warna khatam hua helper wahan frozen "zombie" banke khada rehta hai.


Connections

  • Processes vs Threads — threads by default memory share karte hain; fork alag (COW) memory deta hai.
  • Process Control Block (PCB)fork() ke dauran kya copy/create hota hai.
  • Zombie and Orphan Processeswait() missing hone ka seedha consequence.
  • Inter-Process Communication — fork ke baad shared FDs, pipes enable karte hain.
  • Copy-on-Write Memory — saste fork() ke peeche ka optimization.
  • The Shell — fork+exec+wait hi shell ka commands chalane ka dil hai.
  • Signals (SIGCHLD) — children reap karne ke liye blocking wait() ka alternative.

Concept Map

calls fork

returns 0

returns child PID

returns -1

Copy-on-Write

shared

calls exec

never returns on success

calls exit status

calls wait

status collected by

prevents

Parent process

fork clone

Child process

Failure, no child

Memory copied

File descriptors

exec replaces image

New program runs

exit terminates

wait suspends parent

Zombie processes