The following C program main() { fork() ; fork() ; printf ("yes"); } If we…

2018

The following C program

main()
{
    fork() ; fork() ; printf ("yes");
}

If we execute this core segment, how many times the string yes will be printed ?

  1. A.

    Only once

  2. B.

    2 times

  3. C.

    4 times

  4. D.

    8 times

Attempted by 181 students.

Show answer & explanation

Correct answer: C

The C program uses the fork() system call twice sequentially. The first fork creates one child process, resulting in two active processes (parent and child). The second fork is executed by both existing processes, doubling the count to four total processes. Since all four processes reach and execute the printf statement, the string is printed exactly 4 times.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Isro