Resource Management (HL Extension) Study Notes

Hello Computer Scientists! Welcome to Topic 6: Resource Management. This chapter is vital for understanding how the Operating System (OS) truly works—it’s the brain behind the machine, deciding who gets what, and when.

Think of the OS as the manager of a very busy factory (the computer). It has limited machines (resources) and many workers (processes) demanding them. If the manager doesn't allocate resources efficiently and fairly, chaos ensues!

We will break down how the OS manages the CPU (scheduling), memory (RAM), and prevents nasty issues like deadlocks. Don't worry if this seems tricky at first; we’ll use clear analogies to make sense of these complex systems.


1. The Role of the Operating System (OS) in Resource Management

A resource is any hardware or software component required by a process for execution. Since systems usually run multiple processes concurrently, the OS must manage these resources to ensure stability, efficiency, and fairness.

Types of Resources
  • Processor (CPU): The central computation unit.
  • Memory (RAM): Where programs and data are stored while running.
  • I/O Devices: Printers, hard drives, network cards, screens.
  • Files/Data: Shared information or database access.
Goals of Resource Management

The OS tries to achieve three main goals simultaneously:

  1. Efficiency: Keeping resources busy (e.g., keeping the CPU utilized).
  2. Fairness: Ensuring every process gets a reasonable share of time and resources.
  3. Security/Protection: Preventing one process from interfering with another (e.g., reading or writing to another process's memory space).
Quick Review: The OS is the central traffic controller, aiming for high utilization without crashes or starvation.

2. Processor Management: Scheduling Algorithms

CPU Scheduling is the function that determines which process in the ready queue gets allocated the CPU next and for how long. The main objective is to optimize system performance metrics like throughput (jobs completed per unit time) and response time (how quickly a system reacts to input).

Key Scheduling Algorithms

We categorize scheduling into two types:

  • Non-preemptive: Once a process starts running, it continues until it finishes or voluntarily yields the CPU (like a presentation that can't be interrupted).
  • Preemptive: The OS can interrupt a running process and assign the CPU to a higher-priority task (like a supervisor interrupting a worker).
a) First Come, First Served (FCFS)

This is the simplest non-preemptive algorithm.

  • How it works: Processes are served strictly in the order they arrive. It’s a basic queue (FIFO).
  • Analogy: A simple queue at a supermarket checkout line.
  • Drawback: It suffers from the Convoy Effect. If a very long job arrives first, all subsequent short jobs have to wait a very long time, leading to poor overall efficiency and high average waiting time.
b) Shortest Job Next/First (SJN/SJF)

This is an optimal algorithm for minimizing the average waiting time.

  • How it works: The scheduler picks the job that requires the least estimated execution time (burst time). Can be non-preemptive or preemptive (known as Shortest Remaining Time First - SRTF).
  • Analogy: Clearing all your quick 5-minute tasks before starting on the massive 3-hour project.
  • Drawback: It requires predicting the future (knowing the exact execution time), which is impossible in real systems. It also risks starvation (a very long job might never run if small jobs keep arriving).
c) Priority Scheduling

Jobs are executed based on an assigned importance level.

  • How it works: Each process is assigned a priority number (lower number often means higher priority). The CPU goes to the highest priority process ready to run.
  • The Danger: Starvation. Low-priority processes may wait indefinitely if high-priority jobs continually arrive.
  • Solution: Aging. This technique gradually increases the priority of processes that have been waiting in the queue for a long time. It's like giving an "express pass" to someone who has been waiting in line for hours.
d) Round Robin (RR)

This is a preemptive algorithm designed specifically for time-sharing systems to provide fast response times.

  • How it works: Each process is given a small unit of CPU time, called a time quantum or time slice (e.g., 10 milliseconds). If the process doesn't finish within that time, it is preempted and moved to the back of the ready queue.
  • Analogy: A high-speed traffic light cycling quickly between lanes. Everyone gets a turn, even if brief.
  • Key Consideration: The size of the time quantum is crucial. If too large, RR behaves like FCFS. If too small, too much time is wasted switching between processes (context switching).
Key Takeaway on Scheduling: FCFS is simple but inefficient. SJF is optimal but impractical. RR provides fairness and fast response time using time quanta.

3. Memory Management Techniques

The OS is responsible for managing the primary memory (RAM). It must track which parts of memory are being used, allocate space to processes, and protect one process’s memory from another.

a) Paging

Paging is a memory management scheme that avoids external fragmentation (wasted space between allocated memory blocks).

  • How it works: Physical memory (RAM) is divided into fixed-size blocks called frames. Logical memory (the process address space) is divided into same-size blocks called pages.
  • When a process runs, its pages are mapped into available frames in RAM. Crucially, these frames do not need to be contiguous (next to each other).
  • Address Translation: The OS uses a Page Table to translate the process's logical address (Page Number + Offset) into the physical address (Frame Number + Offset).
b) Segmentation

Segmentation is a memory management scheme that maps memory based on a program's logical structure.

  • How it works: The program is divided into variable-sized logical units (segments), corresponding to components like the main code, data area, stack, or subroutines.
  • This is easier for programmers to manage but can lead to external fragmentation because segments are different sizes, creating uneven holes in memory when they finish.
c) Virtual Memory

Virtual memory is a revolutionary technique that allows a program to run even if only part of it is loaded into physical memory. It creates the illusion that the computer has more RAM than it physically possesses.

  • How it works: It uses secondary storage (disk space) to hold parts of the program that are not currently in use. This storage area on the disk is often called the swap space.
  • Swapping: When a process requests a page/segment that is currently on the disk, the OS triggers a page fault, moves an unused page from RAM back to the disk, and loads the requested page into RAM. This process is called swapping.
  • Analogy: Your physical desk (RAM) can only hold so many books (pages). Virtual memory is like having a huge filing cabinet (disk) next to you, from which you swap books as needed. It feels like you have infinite desk space, but swapping is slow!
  • Did You Know? Excessive swapping, where the system spends more time moving pages than actually executing instructions, is called thrashing, and it dramatically slows down the computer.
Key Takeaway on Memory: Paging and Segmentation are methods for allocation. Virtual Memory uses swapping between RAM and disk space to increase the apparent size of physical memory.

4. Concurrency and Deadlock

When multiple processes are running simultaneously (concurrency), they often need to share resources. This shared access creates potential problems that the OS must manage.

a) Race Conditions and Critical Sections

A race condition occurs when the outcome of a process execution depends on the sequence or timing of access to shared resources by two or more independent processes.

  • Example: Two processes, A and B, simultaneously try to increment a shared variable $X$. If the sequence is A reads, B reads, A increments, B increments, the final result will be wrong (only incremented once instead of twice).
  • To prevent race conditions, the shared segment of code where resources are accessed must be treated as a critical section.

Mutual exclusion is the solution to race conditions. It ensures that if one process is executing in its critical section, no other process can be executing in its critical section that uses the same shared resource.

b) Deadlock

A deadlock is a permanent waiting state where two or more competing processes are waiting for resources that are currently held by the other waiting processes. None can proceed.

  • Analogy: The classic "traffic gridlock" where cars are stuck in a loop, each waiting for the car ahead to move.
The Four Necessary Conditions for Deadlock

Deadlock can only occur if all four of these conditions hold true simultaneously. Removing just one condition prevents deadlock.

Memory Aid (M-H-N-C):

  1. M: Mutual Exclusion: Resources involved must be non-sharable (only one process can use it at a time). (e.g., a printer, not read-only data).
  2. H: Hold and Wait: A process must be holding at least one resource and simultaneously waiting to acquire additional resources held by other processes.
  3. N: No Preemption: Resources cannot be forcibly taken away from the process holding them; they must be released voluntarily.
  4. C: Circular Wait: There must exist a set of processes \(\{P_0, P_1, ..., P_n\}\) such that \(P_0\) is waiting for a resource held by \(P_1\), \(P_1\) is waiting for a resource held by \(P_2\), and so on, with \(P_n\) waiting for a resource held by \(P_0\).
Deadlock Handling Strategies

The OS uses different approaches to manage deadlocks:

  1. Deadlock Prevention: Structurally ensuring that at least one of the four necessary conditions can never hold. (Example: Requiring processes to request all needed resources at once, thus breaking the Hold and Wait condition.)
  2. Deadlock Avoidance: The OS dynamically checks the resource allocation state to ensure that a safe state is maintained (a state where the system can allocate resources to each process in some order without causing a deadlock). (The most famous algorithm for this is the Banker’s Algorithm.)
  3. Deadlock Detection and Recovery: Allowing deadlocks to occur, detecting them (using resource allocation graphs), and then recovering. Recovery typically involves preemption (taking a resource away) or process termination (killing one or more processes to break the cycle).
Key Takeaway on Concurrency: Race conditions are solved by mutual exclusion. Deadlocks require the simultaneous fulfillment of four specific conditions (M-H-N-C) and are handled by prevention, avoidance, or detection/recovery.

You’ve made it through Resource Management! Understanding these concepts is essential for truly appreciating the complexity and robustness of modern operating systems. Keep practicing those scheduling algorithms and memorizing the four deadlock conditions!