Processes, Threads, System Calls and Context Switching Notes for Gate Cs
Processes, Threads, System Calls and Context Switching notes for GATE CS: 53 study cards covering concepts, formulas, shortcuts and exam traps, plus solved pr
Processes Threads System Calls and Context Switching notes
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.
The Five-State Process Model
Five States of a Process
Every process transitions through these five states during its lifetime:
1. New
The process is being created. PCB is being allocated and initialized. Memory is being allocated. Short-lived transitional state.
2. Ready
Process is loaded in memory and waiting for CPU assignment. All resources except CPU are available. Multiple processes can be in Ready state simultaneously. Organized in ready queues.
3. Running
Instructions are being executed. Only ONE process can be Running on a single CPU at any instant. Process has been assigned the CPU by the scheduler. Continues until it terminates, requests I/O, is preempted, or an event occurs.
4. Blocked (Waiting)
Process cannot continue until some external event occurs. Waiting for I/O completion, signal, message, or resource. Multiple processes can be Blocked simultaneously. Organized in device queues or event queues.
5. Terminated (Exit)
Process has finished execution. OS deallocates resources and removes PCB. Short-lived transitional state before complete removal.
Key Observations
A process is active only in Running state
A process is eligible to run in Ready state
A process is ineligible to run in Blocked state (even if CPU is free)
The transition from Blocked to Ready happens when the awaited event occurs
The transition from Ready to Running happens through CPU scheduling
50 more cards in this chapter
Try a question
Answer it here to see how it works. Nothing is recorded until you sign in.
Question 1
Level 1: Warm-up
In the standard five-state process model, a process is currently in the 'Running' state. How many distinct valid state transitions are possible directly from the 'Running' state?
Question 2
Level 1: Warm-up
Which of the following process state transitions is/are strictly IMPOSSIBLE in the standard five-state process model, ignoring any non-standard OS optimizations?
Question 3
Level 1: Warm-up
Consider three scenarios starting with a single initial process. Which of the following statements regarding the total number of processes (parent + children) existing after the operations complete is/are TRUE?
(1) The process executes fork() exactly once.
(2) The process executes fork() sequentially exactly twice.
(3) The process executes fork() sequentially exactly three times.
Question 4
Level 1: Warm-up
Consider a program that starts with a single initial process. It executes a loop that calls the fork() system call exactly k times sequentially, with no conditional breaks or wait() calls. Assuming all fork() calls are successful, if the total number of child processes created is 31, what is the value of k?
Question 5
Level 1: Warm-up
In a single-CPU system with no I/O devices, what is the maximum number of processes that can be simultaneously in the 'Running' state?
Question 6
Level 1: Warm-up
Assertion (A): Context switching is considered pure overhead in an operating system.
Reason (R): During a context switch, the CPU executes instructions that do not advance the execution of any user process.
Question 7
Level 1: Warm-up
Consider a process that starts execution, performs 3 scanf operations, 2 printf operations, and 1 fopen operation before terminating normally. Assuming the process enters the ready queue initially and after every I/O operation completion, how many times does this process enter the ready queue during its entire lifetime?
Question 8
Level 1: Warm-up
Which of the following events will ALWAYS trigger a context switch that results in the currently running process moving to a non-running state?
Question 9
Level 1: Warm-up
In a multithreaded process, which of the following resources is strictly private to each individual thread and is NOT shared among threads of the same process?
Question 10
Level 1: Warm-up
During a context switch between two threads of the SAME process, which of the following components is saved and restored by the operating system?
Free preview ends here
Login to view the complete notes
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 Notes for GATE CS
Processes, Threads, System Calls and Context Switching notes for GATE CS: 53 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.
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.
The Five-State Process Model
Five States of a Process
Every process transitions through these five states during its lifetime:
1. New
The process is being created. PCB is being allocated and initialized. Memory is being allocated. Short-lived transitional state.
2. Ready
Process is loaded in memory and waiting for CPU assignment. All resources except CPU are available. Multiple processes can be in Ready state simultaneously. Organized in ready queues.
3. Running
Instructions are being executed. Only ONE process can be Running on a single CPU at any instant. Process has been assigned the CPU by the scheduler. Continues until it terminates, requests I/O, is preempted, or an event occurs.
4. Blocked (Waiting)
Process cannot continue until some external event occurs. Waiting for I/O completion, signal, message, or resource. Multiple processes can be Blocked simultaneously. Organized in device queues or event queues.
5. Terminated (Exit)
Process has finished execution. OS deallocates resources and removes PCB. Short-lived transitional state before complete removal.
Key Observations
A process is active only in Running state
A process is eligible to run in Ready state
A process is ineligible to run in Blocked state (even if CPU is free)
The transition from Blocked to Ready happens when the awaited event occurs
The transition from Ready to Running happens through CPU scheduling
State Transition Diagram: All Possible Moves
State Transition Diagram
Transition Details
Transition
Trigger
Description
New -> Ready
Admit
OS creates process and loads it into memory
Ready -> Running
Dispatch
Scheduler assigns CPU to the process
Running -> Ready
Preempt
Time slice expires or higher priority process arrives
Running -> Blocked
Wait
Process requests I/O or waits for event
Blocked -> Ready
Wake up
Awaited event occurs (I/O complete, signal received)
Running -> Terminated
Exit
Process completes execution or is killed
Impossible Transitions
Blocked -> Running: must go through Ready first
Blocked -> Terminated: must wake up to Ready, then run, then terminate
Ready -> Blocked: only Running processes can block
New -> Running: must be admitted to Ready queue first
Terminated -> any state: process is done
Critical insight: A Blocked process cannot run even if the CPU is idle. It must first be moved to Ready when its awaited event occurs.
Processes, Threads, System Calls and Context Switching: Solved Questions with Step-by-Step Explanations (10 Problems)
Question 1 · Operating SystemNAT
In the standard five-state process model, a process is currently in the 'Running' state. How many distinct valid state transitions are possible directly from the 'Running' state?
Correct Answer:
3.00
Step-by-Step Solution
Key idea: This is a state transition diagram recall question, recognizable because it asks for valid moves from a specific state.
Step 1: Identify the current state: Running.
Step 2: List all valid outgoing transitions from the Running state:
Running → Ready (due to preemption or time quantum expiry)
Running → Blocked (due to I/O request or waiting for an event)
Running → Terminated (due to process completion or exit)
Step 3: Count the valid transitions. There are exactly 3.
Answer: 3.00
Question 2 · Operating SystemMSQ
Which of the following process state transitions is/are strictly IMPOSSIBLE in the standard five-state process model, ignoring any non-standard OS optimizations?
A.
New to Ready
B.
Ready to Blocked
C.
Blocked to Running
D.
Blocked to Terminated
Correct Answer:
["B","C","D"]
Step-by-Step Solution
Insight: The standard five-state model has strict rules: only Running processes can block or terminate, and Blocked processes must wake up to Ready first.
Exam route: Evaluate each transition. New → Ready is allowed. Ready → Blocked is impossible (must be Running). Blocked → Running is impossible (must go through Ready). Blocked → Terminated is impossible (must wake up first).
Learning route: The OS enforces state boundaries. A process in the Ready state is waiting for the CPU, so it cannot block (it is not executing anything to request I/O). A process in the Blocked state is waiting for an event; it cannot run or terminate until that event occurs and the OS moves it to the Ready queue. Thus, B, C, and D are impossible.
Wrong path: Assuming a process can terminate directly from Blocked (ignoring the constraint that it must be cleaned up after running or being explicitly killed from Ready/Running), leading to missing D.
Question 3 · Operating SystemMSQ
Consider three scenarios starting with a single initial process. Which of the following statements regarding the total number of processes (parent + children) existing after the operations complete is/are TRUE?
(1) The process executes fork() exactly once.
(2) The process executes fork() sequentially exactly twice.
(3) The process executes fork() sequentially exactly three times.
A.
The total number of processes in Scenario (1) is strictly less than in Scenario (2).
B.
The total number of processes in Scenario (2) is strictly less than in Scenario (3).
C.
The total number of processes in Scenario (1) is greater than in Scenario (2).
D.
The total number of processes in Scenario (3) is exactly 6.
Correct Answer:
["A","B"]
Step-by-Step Solution
Key idea: Each sequential fork() call doubles the total number of processes. The formula for n sequential forks is 2n total processes.
Step 1: Calculate total processes for Scenario (1). n=1, so total processes = 21=2.
Step 2: Calculate total processes for Scenario (2). n=2, so total processes = 22=4.
Step 3: Calculate total processes for Scenario (3). n=3, so total processes = 23=8.
Step 4: Evaluate Option A. Is 2<4? Yes. (True)
Step 5: Evaluate Option B. Is 4<8? Yes. (True)
Step 6: Evaluate Option C. Is 2>4? No. (False)
Step 7: Evaluate Option D. Is the total for Scenario (3) equal to 6? No, it is 8. The value 6 comes from the incorrect linear formula 2×3. (False)
Answer: Options A and B are true.
Question 4 · Operating SystemNAT
Consider a program that starts with a single initial process. It executes a loop that calls the fork() system call exactly k times sequentially, with no conditional breaks or wait() calls. Assuming all fork() calls are successful, if the total number of child processes created is 31, what is the value of k?
Correct Answer:
5.00
Step-by-Step Solution
Key idea: Use the exponential formula for sequential fork() calls to relate the number of calls to the total number of processes.
Step 1: Recall the formula for k sequential fork() calls starting from 1 process.
Step 2: Total processes = 2k.
Step 3: Total child processes = Total processes − Initial process = 2k−1.
Step 4: Set up the equation based on the given value: 2k−1=31.
Step 5: Solve for 2k: 2k=32.
Step 6: Solve for k: Since 25=32, k=5.
Answer: 5.00
Question 5 · Operating SystemMCQ
In a single-CPU system with no I/O devices, what is the maximum number of processes that can be simultaneously in the 'Running' state?
A.
0
B.
1
C.
2
D.
Depends on the scheduling algorithm
Correct Answer:
B
Step-by-Step Solution
Key idea: This tests the fundamental constraint of the 'Running' state in the five-state process model.
Step 1: Recall the definition of the 'Running' state: instructions are being executed.
Step 2: A single-CPU system has exactly one processing unit.
Step 3: Therefore, only ONE process can be actively executing instructions on that single CPU at any given instant.
Answer: B
Question 6 · Operating SystemMCQ
Assertion (A): Context switching is considered pure overhead in an operating system.
Reason (R): During a context switch, the CPU executes instructions that do not advance the execution of any user process.
A.
Both A and R are true, and R is the correct explanation of A.
B.
Both A and R are true, but R is NOT the correct explanation of A.
C.
A is true, but R is false.
D.
A is false, but R is true.
Correct Answer:
A
Step-by-Step Solution
Key idea: This tests the conceptual understanding of context switching and its impact on system performance.
Step 1: Evaluate Assertion (A): Context switching involves saving and loading state, which consumes CPU cycles without doing useful work for the user. Thus, it is pure overhead. (True)
Step 2: Evaluate Reason (R): During this save/load phase, the CPU is executing OS kernel instructions, not the instructions of any user process. (True)
Step 3: Check the link: R directly explains why A is true. The lack of progress for user processes is the very definition of this overhead.
Answer: A
Question 7 · Operating SystemMCQ
Consider a process that starts execution, performs 3 scanf operations, 2 printf operations, and 1 fopen operation before terminating normally. Assuming the process enters the ready queue initially and after every I/O operation completion, how many times does this process enter the ready queue during its entire lifetime?
A.
5
B.
6
C.
7
D.
8
Correct Answer:
C
Step-by-Step Solution
Key idea: This tests the formula for counting ready queue entries based on I/O operations.
Step 1: Identify the initial entry: The process enters the ready queue once when it is first created/admitted. (Count = 1)
Which of the following events will ALWAYS trigger a context switch that results in the currently running process moving to a non-running state?
A.
The process executes a simple arithmetic instruction
B.
A timer interrupt occurs
C.
The process allocates memory from the heap
D.
The process reads a variable from its own stack
Correct Answer:
B
Step-by-Step Solution
Key idea: This tests the specific events that force the OS to perform a context switch.
Step 1: Evaluate option A: Executing an arithmetic instruction is a normal user-mode action and does not trap to the OS. (False)
Step 2: Evaluate option B: A timer interrupt forces the OS to regain control and evaluate if the current process should be preempted, always triggering a context switch or preemption check. (True)
Step 3: Evaluate option C: Heap allocation is typically handled in user space and does not "always" trigger a switch. (False)
Step 4: Evaluate option D: Reading from the stack is a standard user-mode instruction. (False)
Answer: B
Question 9 · Operating SystemMCQ
In a multithreaded process, which of the following resources is strictly private to each individual thread and is NOT shared among threads of the same process?
A.
Heap memory
B.
Open file descriptors
C.
Program counter and stack
D.
Global variables
Correct Answer:
C
Step-by-Step Solution
Key idea: This tests the distinction between shared process resources and private thread state.
Step 1: Recall that threads within the same process share the process's address space and resources.
Step 2: Identify the shared resources: Heap memory, open file descriptors, and global variables (data segment) are all shared.
Step 3: Identify the private resources: Each thread must maintain its own independent execution state, which includes its own Program Counter (PC), CPU registers, and Stack.
Answer: C
Question 10 · Operating SystemMCQ
During a context switch between two threads of the SAME process, which of the following components is saved and restored by the operating system?
A.
Page table base register
B.
Heap memory segment
C.
Program counter
D.
Open file descriptor table
Correct Answer:
C
Step-by-Step Solution
Key idea: During a thread context switch, only the private state of the thread is saved. Shared process resources are untouched.
Step 1: Identify the components listed.
Step 2: Classify each as private (PC, registers, stack) or shared (page table, heap, open files).