Welcome to Module 11 of your GATE preparation. In the previous module on Compiler Design, we explored how a high-level program is translated, structured, and optimized into a machine-understandable format. Now, we shift our focus from how a program is built to how it is managed and executed by the computer's core software: the Operating System.
This lesson marks the beginning of our deep dive into the OS. We will dissect the fundamental concept of a process, which is essentially a program in execution. You will learn to trace the lifecycle of a process through its various states—from creation to termination—using the process state transition diagram, a cornerstone of OS theory. We will then examine the critical data structure the OS uses to keep track of every process: the Process Control Block (PCB). Finally, we will connect these two concepts by detailing the mechanics of a context switch, the procedure that allows the OS to juggle multiple processes, creating the illusion of parallel execution.
Mastering these foundational concepts is non-negotiable for GATE, as they form the bedrock for nearly every other topic in Operating Systems, including CPU scheduling, synchronization, and memory management.
The Lifecycle of a Process: States and Transitions
When you run a program, the operating system loads it into memory and creates an active entity called a process. Unlike a program file sitting passively on your disk, a process is dynamic, changing its state as it executes and interacts with the system. The OS tracks these changes using a model called the process state diagram.
At any given time, a process is in one of several states. Let's explore the most fundamental states and the events that cause transitions between them.
OS | Process Management | Process State Transition Diagram and various Schedulers | RBR
To begin, let's watch a segment from a lecture by Prof. Ravindrababu Ravula, a well-known authority for GATE preparation. This will introduce the core states and transitions in a process's life.
First, watch the explanation of the "happy path" of a process: from creation to termination. Focus on the distinction between the New, Ready, Running, and Terminated states from this section. Next, see what happens when a running process needs to perform an I/O operation. Watch the segment on the Waiting/Blocked state from this part.
As you saw in the video, the journey of a process involves several key stages:
- New: The process is being created. The OS has not yet admitted it to the pool of executable processes.
- Ready: The process is loaded into main memory and is waiting to be assigned to a CPU for execution. There can be many processes in the Ready state, typically managed in a queue. The decision to move a process from secondary memory to the Ready state is handled by the Long-Term Scheduler, which controls the degree of multiprogramming (the number of processes in memory).
- Running: The process's instructions are being executed by a CPU core. The transition from Ready to Running is performed by the Short-Term Scheduler (or Dispatcher). On a single-core system, only one process can be in the Running state at a time.
- Waiting (or Blocked): The process is waiting for some event to occur, such as the completion of an I/O operation (e.g., reading from a file) or receiving a signal. A process in this state cannot proceed even if the CPU is free.
- Terminated: The process has finished execution. Its resources are being deallocated by the OS.
The transitions discussed so far are:
- New → Ready: The process is admitted by the Long-Term Scheduler.
- Ready → Running: The Short-Term Scheduler selects the process for execution.
- Running → Terminated: The process completes its execution normally.
- Running → Waiting: The process requests an I/O operation or needs to wait for another event.
- Waiting → Ready: The event the process was waiting for has occurred (e.g., I/O is complete).
Preemption and Suspended States
The model becomes more complete when we consider multiprogramming in a time-sharing environment. An OS can't let one process hog the CPU forever. This introduces preemption and the possibility of moving processes out of main memory.
OS | Process Management | Process State Transition Diagram and various Schedulers | RBR
Let's continue with Prof. Ravula's lecture to understand two more critical transitions: preemption and suspension.
Watch the segment from this clip. Pay close attention to the event that causes a process to move from Running back to Ready, and the reason for introducing the "Suspend" states.
This adds several more transitions to our model, managed by the Medium-Term Scheduler:
- Running → Ready (Preemption): This is a forced transition. It occurs when a higher-priority process becomes ready or when the currently running process has exhausted its assigned time slice (timer interrupt). This ensures fairness and responsiveness.
- Suspended States: Sometimes, main memory becomes full. To make room for a new process or a higher-priority process, the OS can move a lower-priority process from main memory to disk. This is called swapping.
- Ready → Suspend Ready: A ready process is swapped out to disk.
- Waiting → Suspend Wait: A waiting process is swapped out to disk. It is still waiting for its event (e.g., I/O), but from disk.
- Suspend Wait → Suspend Ready: The event that the process was waiting for occurs while it is swapped out. It is now ready to run, but still on disk.
- Suspend Ready → Ready: The Medium-Term Scheduler brings the process back into main memory when space is available.
A comprehensive 7-state process model including New, Ready, Running, Waiting/Blocked, Terminated, and the two suspended states. This is a crucial diagram for GATE.
The Process Control Block (PCB)
The OS needs to manage a vast amount of information for every single process. To do this, it uses a data structure called the Process Control Block (PCB), also known as a Task Control Block. The PCB is the manifestation of a process inside the OS; if the PCB is gone, the process ceases to exist.
Each process is represented by its own PCB, which stores all the context necessary to manage and execute it.
The following video from Neso Academy provides a clear breakdown of the essential components stored within a PCB.
Watch the video from the beginning, focusing on what each piece of information represents. The key segments are: What is a PCB? Components of a PCB, including Process ID, Process State, Program Counter, CPU Registers, Scheduling information, and more.
To summarize, the PCB contains all the vital information about a process, including:
- Process ID (PID): A unique integer that identifies the process.
- Process State: The current state of the process (e.g., Ready, Running, Waiting).
- Program Counter (PC): The address of the next instruction to be executed for this process. This is absolutely critical for resuming a process after it has been interrupted.
- CPU Registers: The contents of all processor registers (e.g., accumulators, index registers, stack pointers) used by the process. Like the PC, these must be saved when the process is interrupted and restored when it runs again.
- CPU Scheduling Information: The process's priority, pointers to scheduling queues, and other parameters used by the scheduler.
- Memory Management Information: Information about the memory allocated to the process, such as base and limit registers or pointers to page tables.
- Accounting Information: CPU time used, time limits, account numbers, etc.
- I/O Status Information: A list of I/O devices allocated to the process, open files, etc.
The PCB is the repository for any information that may vary from process to process.
Context Switching: The Engine of Multitasking
Now, let's connect the state model and the PCB. A context switch is the mechanism the OS uses to switch the CPU from executing one process to executing another. This is how multitasking is achieved. The PCB is the central tool that makes this switch possible.
When a context switch occurs, the OS must save the context of the old process and load the context of the new process. This "context" is precisely the information stored in the PCB.
OS | Process Management | Process State Transition Diagram and various Schedulers | RBR
Let's conclude with Prof. Ravula's explanation of context switching. He highlights its role and performance implications.
Please watch the segment from this discussion on context switching.
The process of a context switch can be broken down into these steps:
- Trigger: An interrupt occurs (e.g., a timer interrupt signals the end of a time slice, or an I/O completion interrupt arrives).
- Mode Switch: The CPU switches from user mode to kernel mode to allow the OS to handle the interrupt.
- Save Context: The OS saves the state of the currently running process (let's call it P1) into its corresponding PCB (PCB1). This involves storing the Program Counter, all CPU registers, and updating the process state (e.g., to Ready or Waiting).
- Schedule: The OS executes its short-term scheduling algorithm to select the next process to run (let's call it P2) from the ready queue.
- Restore Context: The OS loads the saved context of the new process (P2) from its PCB (PCB2) into the CPU's registers.
- Mode Switch and Resume: The process state in PCB2 is updated to Running. The OS switches the CPU back to user mode and execution of P2 begins at the instruction pointed to by its restored Program Counter.
A context switch is pure overhead; the system does no useful application-level work during the switch. The time it takes depends on the amount of information in the PCB and hardware support. For GATE, remember that frequent context switching can degrade system performance, a trade-off for achieving multitasking and responsiveness.
Conclusion
In this lesson, we laid the essential groundwork for our study of Operating Systems. You are now equipped with a solid understanding of the life of a process and how the OS manages it.
Here are your key takeaways:
- A process moves through a series of states: New, Ready, Running, Waiting, and Terminated. Preemption and memory pressure introduce additional states like Suspend Ready and Suspend Wait.
- The Process Control Block (PCB) is the data structure that represents a process within the OS, storing its entire context, including its state, Program Counter, and CPU registers.
- A Context Switch is the mechanism used to switch the CPU between processes. It involves the OS saving the context of the outgoing process into its PCB and restoring the context of the incoming process from its PCB.
We have established what a process is. In our next lesson, we will explore an even finer level of concurrency. What if we want multiple threads of execution within the same process? This will lead us to the topic of Threads, where we'll compare user-level and kernel-level threads and analyze their impact on performance and programming models.