Skip to content
GATEiit

GATE CSE 2019 Q27: fork() inside loop for even i from 0 to 9: number of child processes

Syllabus topic: System calls, processes and threads

Question

The following C program is executed on a Unix/Linux system:

#include <unistd.h>
int main()
{
    int i;
    for (i=0; i<10; i++)
        if (i%2 == 0) fork();
    return 0;
}

The total number of child processes created is ________.

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 31 (official GATE 2019 final answer key)

Concept used: Each fork() doubles the number of processes, so k sequential forks create 2k−1 child processes.

Concept notes

Loading notes…

First step: Count how many times fork() is actually executed along one path (only for even i), then use 2k−1.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on System calls, processes and threads

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2019 CS question paper and final answer key, organised by IIT Madras. Question paper · Answer key