Video summary
L-1.9: Questions on Fork System Call With Explanation | Operating System
Main summary
Key takeaways
Main ideas / lesson conveyed
fork() behavior (core concept)
- When
fork()is called, it creates a child process in addition to the existing parent process. - The child process receives a return value of 0.
- The parent process receives a positive integer (often thought of as +1, but any positive value works).
- Parent and child then execute concurrently from the point of the
fork()call.
How to solve fork()-based questions
- Split the execution into two cases each time you see a conditional involving
fork():- one case where the current path is in the child (
fork()returns 0) - another case where the current path is in the parent (
fork()returns positive)
- one case where the current path is in the child (
- Track which processes enter the
ifblock and how many timesprintstatements run.
Key operator logic used in the problem
- The conditional uses the bitwise AND (
&) operator, not OR. - With
&:positive & 0→0(false)positive & positive→positive(true)
- Unlike
||, the&operator requires evaluating based on the relevant operand results (so you must account for thefork()return values according to how the expression is structured).
Detailed execution logic (as explained in the subtitles)
Setup
- A C program contains
main(). - There is an
ifcondition that involves:- a first
fork()producing one process split - then another
fork()appearing after the&
- a first
- The program prints
"Hello"in theprintfstatements that follow theifcondition(s). - The explanation labels processes as parent/child and counts child instances created by each
fork().
First fork() (inside the if)
-
Child path:
- First
fork()returns 0 - Therefore the expression value makes the condition evaluate to false
- Result: the first
ifblock is skipped, and the code reachesprintf("Hello")once (on that path).
- First
-
Parent path:
- First
fork()returns positive - This influences the
&expression, so execution proceeds to the second operand involving the nextfork().
- First
Second fork() (the operand after &)
This second fork() runs only in the scenario where the first part of the & expression leads execution to evaluate the second part.
-
When the second
fork()is in the child created by this secondfork:- Second
fork()returns 0 - So the
&outcome ispositive & 0 = 0→ false - Result: the nested
ifbody is not executed, and it reachesprintf("Hello")once.
- Second
-
When the second
fork()is in the parent created by this secondfork:- Second
fork()returns positive - So the
&outcome ispositive & positive = positive→ true - Result: the
ifbody is executed - Inside that
ifbody, anotherfork()occurs, leading to one additional child (as described).
- Second
Final counting of "Hello" outputs (speaker’s conclusion)
- The speaker concludes there are 4 total prints of
"Hello". - Main reasoning used for the count:
- one process prints when it skips the
ifon the child return of the firstfork - further splits from the second
forkand the finalifexecution create additional processes that reachprintf("Hello") - total prints across all relevant process paths = 4
- one process prints when it skips the
Main points explicitly emphasized
- Child
fork()return value: always 0 - Parent
fork()return value: always positive - How many times
fork()runs matters: eachfork()increases the number of executing processes that may reachprintf. - The same type of problem can be solved by understanding these rules and noting the operator used:
- the explanation suggests that replacing
&with OR (|or||as intended) would change evaluation, but the general approach remains counting child/parent paths.
- the explanation suggests that replacing
Speakers / sources featured
- Speaker: “Gate Smashers” (the channel/creator introducing and explaining the
fork()question). No individual name is provided in the subtitles.