Understanding Processes and Process Management in Operating Systems
1. Introduction
A process is a running program. It should be understood as a program associated with an activity. A program is a list of instructions written in a file on disk (e.g., any executable program with the extension *.exe). When this program is executed, it is loaded into RAM, and the instructions are executed. Then we have a program plus activity, i.e., a process loaded into memory. We often use the terms ‘process’ and ‘task’ interchangeably. Example: Ctrl + Alt + Delete -> Task Manager -> Processes.
Context
PROCESS = PROGRAM + STATE
When multiple processes run concurrently in a system, the operating system needs to allocate the necessary resources (memory, virtual memory space, etc.). The operating system, with the Central Processing Unit (CPU), is responsible for synchronizing resources in a proper order.
The context or data structure of a process is defined as the information necessary to fully specify its current state. This includes the information to be saved when a process loses possession of the CPU and must be restored when it regains control of the processor (instructions and data). This context is unique to each process and identifies it within the system. This context or structure is called the Process Control Block (PCB) or process descriptor.
Process Control Block (PCB)
The Process Control Block (PCB) contains information that is always in main memory during the lifetime of the process. The PCB holds information the operating system needs to manage the process:
- Process Identifier (PID): Typically, a unique number (PID) is used, often within a 32-bit range. It also includes the identifier of the parent process (PPID). The init process (PID 1 on UNIX systems) is the first process and the ancestor of all others.
- Priority: A PCB may have one or more fields to indicate the execution priority. The priority is managed by the scheduler, which can follow different policies.
- Status: A process can have different states depending on its activity (ready, running, suspended). Schedulers may use models with three, four, or seven states. Lists of PCBs are often maintained for each state to facilitate transitions.
- Memory Management Information: This field stores information regarding the use of physical memory (RAM) and the location of instructions and data for the process.
- Resources Used: Each process requires certain hardware and software resources (printer, disk files, CD/DVD-ROM, etc.). These resources are allocated when the process begins execution. If a resource is unavailable during execution, the process enters the BLOCKED state.
Process States
The five states in a typical process state diagram are:
- Running: The process is currently executing.
- Ready: The process is ready to run, waiting for the scheduler to allocate the CPU.
- Blocked: The process cannot run until a specific event occurs (e.g., I/O operation).
- New: The process has been created but not yet accepted by the operating system. It is generally not loaded into main memory.
- Terminated: The process has been removed from the runnable processes, either due to completion or an error (e.g., protection fault, arithmetic error).
State Transitions
Creation
Preparation: When a process is created, it cannot immediately enter the Running state. It needs to be prepared for execution and enters the Ready queue.
Ready
To Execution: Once a process is ready, it waits for its turn. The CPU allocation is determined by the scheduling policy. When its turn arrives, the process takes control of the CPU and begins executing instructions.
Running
Preparation: A running process goes to the Ready state if it involuntarily leaves the CPU (e.g., time slice expires, higher-priority process preempts it).
Wait (Blocked): A running process goes to the Blocked state if it voluntarily relinquishes the CPU to wait for an external event.
Blocked
Prepared (Ready): From the Blocked state, a process cannot directly go to Running. When the external event completes, it must go through the Ready queue again.
Running
Termination: The process terminates after executing its last instruction, transitioning from the Running state to Terminated.
Process States (Detailed)
New and Terminated states are useful for process management. In this model, Blocked and Ready states have queues. When a new process is supported by the operating system, it is placed in the Ready queue. Waiting processes are suspended or held in a Blocked queue. When an event occurs, the corresponding processes are moved to the Ready queue.
Process Creation
The following steps occur during process creation:
- PCB is created:
- A process identifier (PID) is assigned.
- A priority is assigned.
- Necessary resources (memory, files, etc., except CPU) are allocated.
- PCB is inserted into the process table.
- The process is loaded into virtual memory.
- Once all resources (except CPU) are available, the PCB status is set to Ready.
Context Switching
In a multitasking system, when a thread (or process) gives way to another, a context switch occurs. If a process loses control of the CPU, it must be assigned to another process. This involves two steps:
- Select the next process (Scheduler).
- Perform the context switch (Dispatcher).
Context switching introduces overhead; the system does not perform useful work during the switch.
The scheduler selects a process from the Ready queue. The scheduler is responsible for deciding which process should run at each time instant. It must have mechanisms to access the data structures storing the PCBs. It makes decisions based on a scheduling policy:
- FCFS (First-Come, First-Served): The first process to arrive is the first served.
- Round Robin (RR): Each process is assigned a time slice. When the time expires, the process is preempted and returned to the waiting list.
Once the ID of the next process is known, control of the CPU must be transferred. Before this, a context switch occurs. The dispatcher saves the context of the process leaving the CPU and sets the context of the selected process.