Consider the following segment of codes related to process creation. How many…

2017

Consider the following segment of codes related to process creation. How many times the message “child process created” will be printed?

#include <stdio.h>

void main()

{

fork();
fork();
fork();

printf("child process created");

}

  1. A.

    9

  2. B.

    7

  3. C.

    8

  4. D.

    3

Attempted by 180 students.

Show answer & explanation

Correct answer: C

Each fork() system call duplicates the calling process, doubling the total number of running processes. If a program calls fork() consecutively n times, it generates a total of 2n processes. Here, n = 3, creating 23 = 8 concurrent processes (1 parent + 7 children). Each process executes the printf statement once.

Thus, the correct option is C.

Explore the full course: Niacl Ao It Specialist