Processes and Threads

GATE CS previous year questions · 5 questions

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;
}

Explanation

Answer: 21

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

GATE CS 2024 Set 1 | Question 24

MSQ · 1 mark

Which of the following statements about threads is/are TRUE?

Options for GATE CS 2024 Set 1 | Question 24

Explanation

Answer: (D)

  • A: false. Threads can also be implemented in user space by a thread library, with the kernel seeing only one process (the many-to-one model).
  • B: false. Open files belong to the process, so all its threads share one file descriptor table; a file opened by one thread can be used by the others.
  • C: false. Each thread runs its own chain of function calls, so it needs its own stack, along with its own registers and program counter.
  • D: true. The threads of a process share one address space, so any of them can read or overwrite the data of the others. The operating system puts no protection boundary between them; that is the price of cheap sharing.

Answer: D

GATE CS 2024 Set 1 | Question 25

MSQ · 1 mark

Which of the following process state transitions is/are NOT possible?

Options for GATE CS 2024 Set 1 | Question 25

Explanation

Answer: (B), (C)

In the process state diagram the moves are: Ready to Running (dispatch), Running to Ready (preemption, for example when the time slice ends), Running to Waiting (an I/O request or a wait for an event), Waiting to Ready (the event completes) and Running to Terminated (exit).

  • Waiting to Running is not possible: when its event completes, a waiting process joins the ready queue, and only the scheduler can dispatch it from there.
  • Ready to Waiting is not possible: a process blocks only by doing something, such as requesting I/O, and a ready process is not executing.

Running to Ready and Running to Terminated are normal transitions, so A and D are possible.

Answer: B, C

GATE CS 2024 Set 1 | Question 57

NAT · 2 marks

Consider the following code snippet using the fork() and wait() system calls. Assume that the code compiles and runs correctly, and that the system calls run successfully without any errors.

int x = 3;
while(x > 0) {
  fork();
  printf("hello");
  wait(NULL);
  x--;
}

The total number of times the printf statement is executed is _______

Explanation

Answer: 14

After each fork(), the parent and the new child both call printf once and then continue the loop with the same value of x. So every pass doubles the number of processes, and each of them prints once:

  • \(x = 3\): 2 processes print.
  • \(x = 2\): 4 processes print.
  • \(x = 1\): 8 processes print.

The total is \(2 + 4 + 8 = 14\). wait(NULL) only makes a parent wait for its children; it changes the order of execution, not the count. (More than 14 copies of "hello" can finally appear, because fork() copies unflushed output; the question counts executions of the statement.)

Answer: 14

GATE CS 2024 Set 2 | Question 25

MSQ · 1 mark

Consider a process P running on a CPU. Which one or more of the following events will always trigger a context switch by the OS that results in process P moving to a non-running state (e.g., ready, blocked)?

Options for GATE CS 2024 Set 2 | Question 25

Explanation

Answer: (A), (B)

The test is whether P could carry on running after the event.

  • A: a blocking read must wait for the disk, so P is moved to the blocked state and the OS switches to another process (or the idle task). Always.
  • B: the missing page has to be read in from swap space, which is slow disk I/O, so P blocks until the page arrives. Always.
  • C: the handler marks the other process ready, but P can simply resume afterwards; P is switched out only if the scheduler decides to preempt it. Not always.
  • D: the timer handler may find that P's time slice has not run out, or that no other process is ready, and return to P. Not always.

Answer: A, B