Video summary

L-1.9: Questions on Fork System Call With Explanation | Operating System

Main summary

Key takeaways

Educational

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)
  • Track which processes enter the if block and how many times print statements 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 the fork() 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 if condition that involves:
    • a first fork() producing one process split
    • then another fork() appearing after the &
  • The program prints "Hello" in the printf statements that follow the if condition(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 if block is skipped, and the code reaches printf("Hello") once (on that path).
  • Parent path:

    • First fork() returns positive
    • This influences the & expression, so execution proceeds to the second operand involving the next fork().

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 second fork:

    • Second fork() returns 0
    • So the & outcome is positive & 0 = 0 → false
    • Result: the nested if body is not executed, and it reaches printf("Hello") once.
  • When the second fork() is in the parent created by this second fork:

    • Second fork() returns positive
    • So the & outcome is positive & positive = positive → true
    • Result: the if body is executed
    • Inside that if body, another fork() occurs, leading to one additional child (as described).

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 if on the child return of the first fork
    • further splits from the second fork and the final if execution create additional processes that reach printf("Hello")
    • total prints across all relevant process paths = 4

Main points explicitly emphasized

  • Child fork() return value: always 0
  • Parent fork() return value: always positive
  • How many times fork() runs matters: each fork() increases the number of executing processes that may reach printf.
  • 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.

Speakers / sources featured

  • Speaker: “Gate Smashers” (the channel/creator introducing and explaining the fork() question). No individual name is provided in the subtitles.

Original video