Suppose in a multiprogramming environment, the following C program segment is…
2025
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;
}
Attempted by 278 students.
Show answer & explanation
Correct answer: 21
Explanation: Each I/O operation causes the process to request I/O (causing a context switch) and when that I/O completes the process returns and enters the ready queue. We do not count the initial placement of the process on the ready queue when it first starts.
The program performs one input operation using scanf. After the scanf completes the process returns from I/O and enters the ready queue once: +1
The printf statement is executed 20 times inside the loop. Each printf causes an I/O request and later a return to the ready queue, contributing 20 entries: +20
Total: 1 + 20 = 21. Therefore the process enters the ready queue 21 times (not counting the initial run).
A video solution is available for this question — log in and enroll to watch it.