Processes, Threads, System Calls and Context Switching Pyqs for Gate Cs
Solve 9+ Processes, Threads, System Calls and Context Switching previous year questions for GATE CS with answers and detailed solutions. Free sample questions
Try a question
Answer it here to see how it works. Nothing is recorded until you sign in.
Question 1
2026 Slot Set1 PYQ
Level 3: Exam Standard
Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always successful in creating a process.
int main () { int i; for (i = 0; i < 3; i++){ if (fork() == 0){ continue; } break; } printf("Hello!"); return 0; }
The total number of times that the printf statement gets executed is ________. (answer in integer)
Question 2
2025 Slot Set1 PYQ
Level 3: Exam Standard
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; }
Question 3
2024 Slot Set2 PYQ
Level 3: Exam Standard
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)?
Question 4
2024 Slot Set1 PYQ
Level 3: Exam Standard
Which of the following statements about threads is/are TRUE?
Question 5
2024 Slot Set1 PYQ
Level 3: Exam Standard
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 _________
Question 6
2024 Slot Set1 PYQ
Level 3: Exam Standard
Which of the following process state transitions is/are NOT possible?
Question 7
2023 PYQ
Level 3: Exam Standard
Which one or more of the following options guarantee that a computer system will transition from user mode to kernel mode?
Question 8
2023 PYQ
Level 3: Exam Standard
Which one or more of the following need to be saved on a context switch from one thread (T1) of a process to another thread (T2) of the same process?
Question 9
2021 Slot Set1 PYQ
Level 3: Exam Standard
Which of the following standard C library functions will <i>always</i> invoke a system call when executed from a single-threaded process in a UNIX/Linux operating system?
Free preview ends here
Login to view the complete previous-year questions and solutions
Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.
Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.
Built around you, not around a syllabus PDF
Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.
Revision that hits your weak spots
We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.
Questions calibrated to the real exam
Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.
Notes written for recall, not for volume
Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.
One place for everything
Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.
Honest progress
No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.
Unlock the whole course
Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.
Processes, Threads, System Calls and Context Switching PYQs for GATE CS
Solve 9+ Processes, Threads, System Calls and Context Switching previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.
Chapter Roadmap: Processes, Threads, System Calls and Context Switching
Chapter Journey: 4 Topics, One Clear Path
This chapter builds your understanding of how the operating system manages execution. Here is what you will master:
Topic 1: Process States, Ready Queues and Context Switching (Current)
Understand the lifecycle of a process: new, ready, running, blocked, terminated
Learn how the OS maintains ready queues and decides which process runs next
Master context switching: what gets saved, what gets loaded, and the overhead involved
Foundation for everything else in this chapter
Topic 2: Threads and Thread Context
Threads as lightweight units of execution within a process
What threads share versus what they keep private
Thread context switching versus process context switching
Topic 3: System Calls and User-Kernel Mode Transitions
The boundary between user mode and kernel mode
How programs request OS services through system calls
The mechanism of mode transition and its triggers
Topic 4: Process Creation using Fork and Wait
The fork system call: creating child processes
Understanding parent-child relationships
The wait system call: synchronization between processes
What You Will Achieve:
By the end of this chapter, you will be able to trace process state transitions, calculate context switch overhead, distinguish between process and thread behavior, predict the number of processes created by fork loops, and identify which operations trigger user-to-kernel mode transitions.
What is a Process? The Unit of Execution
Process = Program in Execution
A process is a program in execution. It is the fundamental unit of work in a modern operating system.
Program versus Process
Aspect
Program
Process
Nature
Passive entity
Active entity
Storage
Stored on disk
Loaded in memory
Analogy
Recipe in a cookbook
The act of cooking
Lifetime
Permanent file
Temporary execution
What Makes Up a Process?
A process in memory contains:
Text section: The compiled program code
Program counter: Points to the next instruction to execute
Stack: Temporary data, function parameters, return addresses, local variables
Data section: Global variables
Heap: Dynamically allocated memory (grows during execution)
Process Control Block (PCB)
The operating system tracks each process using a Process Control Block (PCB). Think of it as the process identity card. It contains:
Process state: current state (new, ready, running, blocked, terminated)
Program counter: address of next instruction
CPU registers: contents of all registers when process was last running
CPU scheduling information: priority, pointers to scheduling queues
Memory management information: base and limit registers, page tables
Accounting information: CPU time used, time limits, process number
I/O status information: list of I/O devices allocated, open files
The PCB is the key data structure that allows the operating system to support multiple processes simultaneously.
Processes, Threads, System Calls and Context Switching: Solved Questions with Step-by-Step Explanations (9 Problems)
Question 1 · Operating System · 2026_Set1NAT
Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always successful in creating a process.
int main () { int i; for (i = 0; i < 3; i++){ if (fork() == 0){ continue; } break; } printf("Hello!"); return 0; }
The total number of times that the printf statement gets executed is ________. (answer in integer)
Correct Answer:
4.00
Step-by-Step Solution
Insight: The continue statement sends the child process to the next loop iteration, while the break statement causes the parent process to exit the loop immediately. This creates a linear chain of processes.
Exam route: Trace the loop iteration by iteration.
i=0: P0 forks P1. P1 (child) continues to i=1. P0 (parent) breaks and prints. (1 print)
i=1: P1 forks P2. P2 (child) continues to i=2. P1 (parent) breaks and prints. (1 print)
i=2: P2 forks P3. P3 (child) continues to i=3. P2 (parent) breaks and prints. (1 print)
Iteration i=3 (executed by P3): Loop condition i < 3 is false. P3 exits loop, executes printf. (Count = 4)
Total number of times printf is executed is 4.
Question 2 · Operating System · 2025_Set1NAT
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; }
Correct Answer:
21.00
Step-by-Step Solution
Insight: The problem explicitly overrides real-world buffering by stating that every I/O request and return causes a context switch. We simply count the number of I/O operations that return.
Exam route: Identify all I/O operations. scanf is 1 I/O operation. The for loop runs 20 times, and each iteration contains a printf, which is 1 I/O operation. Total I/O operations = 1 + 20 = 21. Each return from I/O transitions the process from Blocked to Ready.
Learning route:
Initial state: New → Ready. The problem states: "not counting the time the process enters the ready queue when it is run initially". So, count = 0.
Process runs, hits scanf. Transitions: Running → Blocked.
This pattern repeats for all 20 iterations of the loop.
Total entries to Ready queue from Blocked state = 1 (from scanf) + 20 (from printf) = 21.
Question 3 · Operating System · 2024_Set2MSQ
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)?
A.
P makes a blocking system call to read a block of data from the disk
B.
P tries to access a page that is in the swap space, triggering a page fault
C.
An interrupt is raised by the disk to deliver data requested by some other process
D.
A timer interrupt is raised by the hardware
Correct Answer:
["A","B","D"]
Step-by-Step Solution
Insight: A context switch away from the running process P occurs if P is forced to yield the CPU. This happens if P voluntarily blocks, encounters a fault requiring OS intervention, or is preempted by a hardware timer.
Exam route: Evaluate each event for whether it forces P out of the Running state.
A: Blocking syscall → P must wait for I/O → moves to Blocked. (Yes)
B: Page fault → P cannot execute the faulting instruction until the page is loaded from disk → moves to Blocked. (Yes)
C: Interrupt for another process → OS handles it, but P may remain Running if it has higher priority or time slice remains. (No, not always)
D: Timer interrupt → Designed specifically to preempt the current process and invoke the scheduler → P moves to Ready. (Yes)
Learning route:
A blocking system call explicitly surrenders the CPU, moving P to the Blocked state.
A page fault is a synchronous exception. The hardware traps to the OS, which must perform disk I/O to fetch the page. P cannot proceed and is placed in a wait queue (Blocked).
An external interrupt (like disk completion for another process) transfers control to the kernel, but the scheduler may decide to resume P immediately if P's priority is highest. It does not always remove P from Running.
A timer interrupt is the mechanism for time-sharing. It forces the scheduler to run, which will move P to the Ready queue and dispatch another process (preemption).
Question 4 · Operating System · 2024_Set1MSQ
Which of the following statements about threads is/are TRUE?
A.
Threads can only be implemented in kernel space
B.
Each thread has its own file descriptor table for open files
C.
All the threads belonging to a process share a common stack
D.
Threads belonging to a process are by default not protected from each other
Correct Answer:
["D"]
Step-by-Step Solution
Insight: Threads within the same process share the process-level resources (like address space and file descriptors) but maintain private execution state (like stack and registers).
Exam route: Evaluate each option against the shared vs. private state model. Threads can be user-level (not just kernel). File descriptors are per-process (shared). Stacks are per-thread (private). Address space is per-process, meaning threads are not protected from each other's memory writes.
Learning route:
Implementation: Threads can be User-Level Threads (ULT) managed by a library, or Kernel-Level Threads (KLT) managed by the OS. Thus, "only in kernel space" is false.
File Descriptors: The open file table is a process-level resource. All threads share it.
Stack: Each thread must have its own call stack to maintain independent function calls and local variables. They do not share a common stack.
Protection: Since all threads share the same virtual address space, a bug in one thread (like a wild pointer) can corrupt the memory of another thread in the same process. They are not protected from each other by the hardware MMU.
Question 5 · Operating System · 2024_Set1NAT
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 _________
Correct Answer:
14.00
Step-by-Step Solution
Insight: Each iteration of the loop doubles the number of active processes, and the wait(NULL) ensures a parent waits for its child to finish the current iteration before proceeding, creating a predictable tree of execution.
Exam route: Let f(k) be the number of prints generated by a process entering the loop with x=k. In each step, fork() creates a child. Both parent and child print (2 prints). Both call wait(). The child's wait() returns immediately (no children), it decrements x, and generates f(k−1) more prints. The parent waits for the child, then decrements x, and generates f(k−1) more prints. Thus, f(k)=2+2f(k−1).
Learning route:
Base case: f(0)=0 (loop condition x>0 fails).
For x=1: fork() creates 2 processes. Both print (2 prints). Child wait returns, x→0, loop ends. Parent wait returns, x→0, loop ends. Total f(1)=2.
For x=2: f(2)=2+2f(1)=2+2(2)=6.
For x=3: f(3)=2+2f(2)=2+2(6)=14.
The total number of times printf is executed is 14.
Question 6 · Operating System · 2024_Set1MSQ
Which of the following process state transitions is/are NOT possible?
A.
Running to Ready
B.
Waiting to Running
C.
Ready to Waiting
D.
Running to Terminated
Correct Answer:
["B","C"]
Step-by-Step Solution
Insight: A process can only transition to Blocked (Waiting) if it is currently Running and actively requests an event. It can only transition to Running from the Ready state, never directly from Blocked.
Exam route: Draw the standard 5-state process diagram. Check each option against the directed edges. Running → Ready (preemption, valid). Waiting → Running (invalid, must go through Ready). Ready → Waiting (invalid, only Running processes can block). Running → Terminated (valid, normal exit).
Learning route:
The OS state machine strictly enforces that a process must be eligible for the CPU (Ready) before it can be dispatched (Running).
A process in the Waiting (Blocked) state is ineligible for the CPU, even if the CPU is idle. When its awaited event occurs, the OS moves it to the Ready queue.
A process in the Ready state is passively waiting for the CPU; it cannot execute instructions to request I/O, so it cannot transition to Waiting.
Therefore, "Waiting to Running" and "Ready to Waiting" are impossible transitions.
Question 7 · Operating System · 2023MSQ
Which one or more of the following options guarantee that a computer system will transition from user mode to kernel mode?
A.
Function Call
B.
malloc Call
C.
Page Fault
D.
System Call
Correct Answer:
["C","D"]
Step-by-Step Solution
Insight: A transition to kernel mode is guaranteed only when an event strictly requires privileged OS intervention, such as a hardware exception or an explicit software trap.
Exam route: Evaluate each option for "guaranteed" mode transition. Function calls are pure user space. malloc is a user-space library function that may not invoke a syscall. Page faults are hardware exceptions that force kernel mode. System calls are explicit software traps that force kernel mode.
Learning route:
Function Call: A standard procedure call (e.g., int x = add(a, b);) executes entirely in user mode. No mode transition occurs.
malloc Call: This is a C library function. It manages a user-space heap. It only invokes a system call (like brk or mmap) if the user-space heap is exhausted. Therefore, it does not guarantee a mode transition.
Page Fault: When a process accesses a valid virtual address that is not currently mapped to physical memory, the hardware triggers a trap. This forces an immediate transition to kernel mode so the OS can fetch the page from disk. This is guaranteed.
System Call: This is a deliberate software instruction (e.g., syscall or int 0x80) designed specifically to cross the user-kernel boundary and request OS services. This is guaranteed.
Question 8 · Operating System · 2023MSQ
Which one or more of the following need to be saved on a context switch from one thread (T1) of a process to another thread (T2) of the same process?
A.
Page table base register
B.
Stack pointer
C.
Program counter
D.
General purpose registers
Correct Answer:
["B","C","D"]
Step-by-Step Solution
Insight: A context switch between threads of the same process only requires saving the private execution state, because the process-level resources (like the address space) remain unchanged.
Exam route: Apply the decision rule: "Is this state private to a thread or shared by the whole process?" Private state must be saved; shared state must not. Stack pointer, Program counter, and General purpose registers are private. The Page table base register is shared.
Learning route:
Page table base register: Points to the process's page directory. Since all threads of a process share the same virtual address space, they use the same page table. It does not change during a thread switch.
Stack pointer: Points to the top of the thread's private call stack. This must be saved to resume the thread's function calls correctly.
Program counter: Holds the address of the next instruction to execute for this specific thread. This must be saved.
General purpose registers: Hold intermediate computation results for this specific thread. These must be saved.
Question 9 · Operating System · 2021_Set1MSQ
Which of the following standard C library functions will <i>always</i> invoke a system call when executed from a single-threaded process in a UNIX/Linux operating system?
A.
exit
B.
malloc
C.
sleep
D.
strlen
Correct Answer:
["A"]
Step-by-Step Solution
Insight: Process termination fundamentally requires kernel intervention to reclaim resources (PCB, file descriptors), making exit the only function that unconditionally crosses the user-kernel boundary.
Exam route: Evaluate each option for "always". strlen is pure user-space math. malloc manages a user-space heap and only calls the OS (brk/mmap) on exhaustion. sleep can sometimes be optimized or interrupted, but exit must always invoke the kernel to destroy the process.
Learning route:
strlen: Operates entirely on user-space memory. No syscall.
malloc: Uses pre-allocated user-space heap. Only invokes a syscall if the heap is exhausted. Not always.
sleep: While it often invokes nanosleep, certain implementations or edge cases (like sleep(0)) might return without a full context switch, or it's simply less absolute than process termination.
exit: A user process cannot destroy itself or free kernel-managed resources. It must always invoke the exit or exit_group syscall to hand control to the OS for cleanup. This is unconditional.