GATE CS 2025 Set 1 | Question 29

NAT · 1 mark

Suppose in a multiprogramming environment, the following C program segment is executed. A process goes into I/O queue whenever an I/O related operation is performed. Assume that there will always be a context switch whenever a process requests for an I/O, and also whenever the process returns from an I/O. The number of times the process will enter the ready queue during its lifetime (not counting the time the process enters the ready queue when it is run initially) is _______. (Answer in integer)

int main()
{
    int x=0,i=0;
    scanf("%d",&x);
    for(i=0; i<20; i++)
    {
        x = x+20;
        printf("%d\n",x);
    }
    return 0;
}

Answer

21

Explanation

Every I/O call moves the process from the CPU to the I/O queue, and when that I/O completes the process re-enters the ready queue. So we only need to count the I/O operations.

The program performs one scanf and 20 printf calls (one in each iteration, \(i = 0\) to \(19\)); the addition and return 0 need no I/O.

Entries into the ready queue \(= 1 + 20 = 21\).

Answer: 21