IAE Academy

GATE 2025 CS1 Q.29: solution and answer

GATE 2025 · CS1Q.29 · 1 mark · NAT

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;
}
See the question in the paper

Answer and solution

ANSWER

The process enters the ready queue 21 times: once after the scanf and once after each of the 20 printf calls.

Official answer: 21Source: Official answer key, GATE 2025 CS1 (IIT Roorkee)

  1. Each I/O request moves the process to the I/O queue. When that I/O completes, the process moves from the I/O queue back to the ready queue. So the process enters the ready queue once per completed I/O operation.

  2. The call scanf("%d",&x) reads from the keyboard. That is 1 I/O operation.

  3. The loop for(i=0; i<20; i++) runs for , which is 20 iterations. Each iteration calls printf, which writes to the output device. That is 20 I/O operations. The statement x = x+20 is pure computation and causes no I/O.

  4. The declarations and return 0 cause no I/O.

  5. Total I/O operations .

  6. Each of the 21 I/O completions puts the process back in the ready queue once. The question does not count the first entry to the ready queue, when the process is run initially.

  7. Number of ready-queue entries .

KEY IDEA

Count the I/O operations. Every I/O request sends the process to the I/O queue, and every I/O completion brings it back to the ready queue, so each I/O call gives exactly one ready-queue entry.

WATCH OUT FOR

Counting only the 20 printf calls and forgetting the scanf (giving 20), or adding the initial entry to the ready queue (giving 22). The arithmetic statement x = x+20 is not an I/O operation.

From the GATE 2025 CS1 paper. Every question of this paper · Download the full paper · All 9 Processes & threads questions, with notes

More Processes & threads questions