Question

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;
}
Official answer and solution
Official answer
21
From
Official GATE 2025 answer key

Solution

Count the total number of I/O operations (scanf and printf) executed by the program; each completion transitions the process from the I/O queue back to the ready queue.

  1. The statement scanf("%d", &x); executes 1 I/O operation.
  2. The for loop runs for i = 0 to 19 (2020 iterations), and each iteration executes printf("%d\n", x);, performing 20 I/O operations.
  3. In total, the process performs 1+20=211 + 20 = 21 I/O operations. Each time an I/O operation is requested, the process moves from Running to the I/O (waiting) queue, and upon returning from that I/O operation, it enters the ready queue.

Excluding the initial entry into the ready queue when the process is created, the process enters the ready queue 2121 times.

Final answer

21

Common trap

Forgetting to count the initial scanf call before the for loop (which would give 20).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2025 Computer Science and Information Technology - Session 1 (CS1)
Question
29 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.