Summary
Full Transcript
Process Management in an Operating System 1. What is a Process? A process is a running program—essentially a program in execution. The OS treats each process as an independent unit with: Its own memory space CPU state (registers, program counter) Resources (files, I/O handles) 2. Process Life Cycle Processes typically go through these states: New → Created by the OS Ready → Waiting to run Running → Currently using the CPU Blocked/Waiting → Waiting for I/O, such as disk or keyboard Terminated → Finished execution The OS maintains a Process Control Block (PCB) for each process, storing: Process ID CPU register values Scheduling information Memory limits I/O status 3. CPU Scheduling Since many processes compete for the CPU, the OS uses a scheduler to decide what runs next. Goals of Scheduling Maximize CPU utilization Minimize waiting time Ensure fairness Support real-time deadlines if needed Common Scheduling Algorithms First-Come, First-Served (FCFS) – simple but may cause long wait times. Shortest Job First (SJF) – minimizes average waiting time. Round Robin (RR) – each process gets a fixed time slice; good for multitasking. Priority Scheduling – higher priority processes run first. Multilevel Queues – different queues for interactive, batch, or real-time tasks. 4. Context Switching A context switch occurs when the OS switches the CPU from one process to another. How it works OS saves the current process’s CPU state (registers, program counter) into its PCB. OS loads the next process’s CPU state from its PCB. CPU resumes execution of the new process. Efficient context switching Modern OSes optimize switching by: Minimizing state that must be saved Using hardware support (e.g., memory protection, register sets) Running kernel code that quickly updates PCBs Even though context switching has overhead, fast switching allows multitasking to feel seamless. 5. Multitasking and Concurrency Modern OSes allow multiple processes to appear to run simultaneously through: Time slicing (rapid context switching) Preemptive scheduling (OS can interrupt tasks) Kernel threads and user threads Some systems also use multi-core CPUs, enabling true parallel execution of multiple threads or processes. 6. Synchronization and Communication When processes run concurrently, the OS manages: Locks, semaphores, monitors → to avoid race conditions Inter-process communication (IPC) using pipes, sockets, shared memory This ensures processes coordinate safely while sharing resources. 7. Process Creation and Termination Creation Processes are typically created using: fork() (UNIX-like OS) – clones the parent process exec() – replaces process memory with a new program CreateProcess() (Windows API) Termination Processes end when: They finish execution They encounter an error The OS or user terminates them The OS then: Frees memory Releases resources Updates process tables In Summary The OS handles running programs through a tightly coordinated system of: Process creation and tracking (PCBs) Scheduling algorithms for fairness and efficiency Context switching for multitasking Synchronization to avoid conflicts Resource management to ensure smooth operation This combination ensures that computers can run multiple programs at once—efficiently, safely, and without interference.
