Learning Objectives
By the end of this lesson, you will be able to:
- Show understanding of how an OS maximizes the use of computer resources
- Describe ways in which the user interface hides hardware complexities from users
- Show understanding of process management including multitasking and process states
- Explain different scheduling routines (FCFS, SJF, Round Robin, SRTF) and their benefits
- Understand how the OS kernel acts as an interrupt handler
- Explain virtual memory, paging, segmentation, and disk thrashing in memory management
- Differentiate between paging and segmentation techniques
Key Terms
Operating System (OS)
A program that acts as an interface between user and computer hardware, controlling execution of all programs.
Kernel
The central component of OS responsible for communication between hardware, software and memory.
Process
A program that has started to be executed. Each process shares common hardware resources.
Multitasking
Allows computers to carry out more than one task (process) at a time by sharing hardware resources.
Scheduling
The process of deciding which processes should be carried out to ensure best use of computer resources.
Virtual Memory
Using secondary storage (disk) to extend RAM so CPU appears to access more memory than available RAM.
Paging
Memory management technique where process is divided into equal-sized pages and memory into same-sized frames.
Segmentation
Memory management technique where memory is divided into variable length blocks called segments.
Interrupt
A signal sent to processor by hardware or software indicating they require processor attention.
Disk Thrashing
Occurs when system spends more time swapping pages between memory and disk than executing processes.
Introduction to Operating Systems
An Operating System (OS) is a program that acts as an interface between the user and computer hardware, controlling the execution of all kinds of programs. When you turn on your computer, the OS is what makes it possible to run applications like web browsers, games, and word processors.
Real-life Example: The School Office
Think of an OS like a school office. Students (users) don't interact directly with the principal, teachers, or maintenance staff (hardware). Instead, they go through the office staff (OS) who:
- Schedule appointments (process scheduling)
- Manage resources like classrooms and equipment (resource management)
- Handle emergencies like fire drills (interrupt handling)
- Simplify complex procedures for students (hiding hardware complexity)
How an OS Starts: The Bootstrap Process
When you first switch on a computer, there's no operating system in memory. Here's what happens:
- The computer has a Basic Input/Output System (BIOS) stored in ROM
- BIOS starts a Bootstrap Program
- The bootstrap program loads the operating system from disk into memory
- The OS is set running and takes control of the computer
Hiding Hardware Complexities
The OS makes computers user-friendly by hiding complex hardware details. Imagine if you had to manually control every electronic component in your phone!
How OS Simplifies Things
- GUI Interfaces: Using graphical interfaces instead of command-line interfaces
- Device Drivers: Special programs that simplify hardware interfaces
- File Management: Simplifying saving and retrieving data from storage
- Background Utilities: Running tasks like virus scanning without user intervention
Resource Management
One key OS task is to maximize utilization of computer resources, which are managed in three areas:
Activity 1: OS Analogy
Scenario: You're the manager of a busy coffee shop with 3 coffee machines (CPUs), limited counter space (Memory), and 2 cash registers (I/O devices). Customers (processes) arrive with different orders.
Task: Explain how you would manage resources to serve customers efficiently. Draw parallels to OS resource management for CPU, Memory, and I/O.
Solution:
- CPU (Coffee Machines): Schedule which customer's order gets processed next. Some orders are quick (espresso), others take longer (complicated latte). Use scheduling algorithms to maximize machine usage.
- Memory (Counter Space): Only keep current orders on the counter. Completed orders go to pickup area. If counter gets full, move some orders temporarily elsewhere (like virtual memory).
- I/O (Cash Registers): While customers pay (I/O operation), baristas can continue making other orders (CPU doesn't wait idle). Have a queue system for payments.
- Overall: Like an OS, you'd multitask - taking new orders while processing current ones, using all resources efficiently without any sitting idle.
Check Your Understanding
1. What is the main purpose of an operating system? [2 marks]
Answer
[1 mark] To act as an interface between the user and computer hardware
[1 mark] To control the execution of all kinds of programs and maximize resource utilization
2. Describe the bootstrap process when a computer is switched on. [3 marks]
Answer
- [1 mark] BIOS (Basic Input/Output System) stored in ROM starts
- [1 mark] BIOS starts a bootstrap program
- [1 mark] Bootstrap program loads the OS from disk into memory and sets it running
Process Management and Multitasking
A process is a program that has started to be executed. Multitasking allows computers to carry out more than one task (process) at a time. Each process shares common hardware resources like CPU, memory, and I/O devices.
Process State Diagram
How it works: Processes move between different states. Only one process can be in Running state at a time. Processes wait in Ready state for CPU attention. If a process needs I/O, it moves to Blocked state.
Process States
A process can be in one of three main states. Understanding these states helps explain how multitasking works.
Running State
- Process is being executed by processor
- Currently using allocated processor time
- Only one process can be running at a time
- If requires I/O, moves to Blocked state
Ready State
- Processes waiting for processor's attention
- Not being executed but ready to run
- In a queue waiting for their turn
- New processes always start in Ready state
Blocked State
- Process waiting for an event (e.g., I/O)
- Cannot be executed at the moment
- Also called Waiting or Suspended state
- Returns to Ready when I/O completes
Process Control Block (PCB)
Each process has a Process Control Block (PCB) - a data structure containing all information needed for a process to run. Think of it as a process's ID card and medical record combined.
Real-life analogy: When a doctor sees multiple patients, each has a medical record (PCB) with their history, current condition, test results, and treatment plan. When switching patients, the doctor refers to the record to continue treatment.
Activity 2: Process State Transitions
Scenario: A computer is running three processes:
- P1: Word processor (currently typing)
- P2: Web browser downloading a file
- P3: Calculator app waiting to be used
Task: For each event below, identify which process changes state and what the new state is:
- User starts printing a document from the word processor
- File download completes in the web browser
- Operating system timer interrupts the word processor
- User switches from word processor to calculator
Solution:
- P1 (Word processor): Running → Blocked (waiting for printer I/O)
- P2 (Web browser): Blocked → Ready (I/O operation completed)
- P1 (Word processor): Running → Ready (interrupted by timer)
- P3 (Calculator): Ready → Running (given CPU time)
P1 (Word processor): Running → Ready (switched out)
Check Your Understanding
3. What is the difference between a program and a process? [2 marks]
Answer
[1 mark] A program is passive code stored on disk
[1 mark] A process is a program that has started execution and has its own PCB, memory space, and resources
4. Why can only one process be in the Running state at a time on a single-core CPU? [2 marks]
Answer
[1 mark] A single-core CPU has only one processing unit that can execute instructions
[1 mark] Therefore, only one process's instructions can be executed at any given moment
5. What information is stored in a Process Control Block (PCB)? [3 marks]
Answer
[1 mark each for any three]
- Current process state (ready, running, blocked)
- Process privileges (which resources it can access)
- Register values (PC, MAR, MDR, ACC)
- Process priority and scheduling information
- Amount of CPU time needed to complete
- Process ID for unique identification
Scheduling Algorithms
Scheduling decides which processes should be carried out to ensure the best use of computer resources. Different algorithms have different benefits and are used in different situations.
Scheduling Algorithm Comparison
Observe: Different scheduling algorithms handle the same processes differently. Notice how waiting times and throughput vary between FCFS, SJF, and Round Robin.
Types of Scheduling Algorithms
| Algorithm | Type | How It Works & Characteristics |
|---|---|---|
| First Come First Served (FCFS) | Non-Preemptive | Jobs executed in order of arrival (like a FIFO queue). Simple but poor performance with long average wait time. |
| Shortest Job First (SJF) | Non-Preemptive | Executes processes in ascending order of CPU time required. Minimizes waiting time but requires knowing burst time in advance. |
| Shortest Remaining Time First (SRTF) | Preemptive | Like SJF but preemptive. If a new process arrives with shorter burst time, current process is interrupted. |
| Round Robin | Preemptive | Each process gets a fixed time slice (quantum). Processes are cycled through in order. Prevents starvation. |
Real-life Example: Supermarket Checkouts
Different scheduling algorithms are like different checkout strategies:
- FCFS: Single queue - customers served in arrival order (fair but slow if someone has a full cart)
- SJF: Express lane - customers with few items go first (fast overall but unfair to those with full carts)
- Round Robin: Each customer gets 2 minutes, then moves to back of queue (fair but inefficient)
- SRTF: Express lane that interrupts current customer if someone with just 1 item arrives
Check Your Understanding
6. Why is starvation impossible in Round Robin scheduling? [2 marks]
Answer
[1 mark] Each process gets a fixed time slice (quantum) during each cycle
[1 mark] Every process is guaranteed to get CPU time regularly, so no process waits indefinitely
7. What is the main disadvantage of SJF scheduling? [2 marks]
Answer
[1 mark] Requires knowing the burst time (CPU time needed) in advance
[1 mark] This is impossible in interactive systems where required CPU time is not known beforehand
Memory Management
Memory management determines which processes should be in main memory and where they should be stored. When memory gets crowded, the OS uses techniques like virtual memory, paging, and segmentation.
Virtual Memory and Paging
Virtual memory uses secondary storage (disk) to extend RAM. The CPU appears to access more memory than physically available. Paging divides processes into equal-sized pages and memory into same-sized frames.
Paging Visualization
How paging works: Each colored block represents a page from a different process. Pages can be scattered throughout available frames. When a needed page isn't in memory (page fault), it's loaded from disk, possibly replacing another page if memory is full.
Paging Characteristics
- Process divided into equal-sized pages
- Memory divided into equal-sized frames
- Not all pages need to be loaded to start execution
- Each process has a page table mapping pages to frames
- Pages can be scattered throughout available frames
- When memory full, pages are swapped to/from disk
Disk Thrashing
Disk thrashing occurs when the system spends more time swapping pages between memory and disk than executing processes. This happens when:
- Too many processes compete for limited memory
- A process constantly needs pages that aren't in memory
- System performance degrades dramatically
Analogy: Like a librarian constantly fetching books from storage instead of helping readers
Segmentation
Segmentation is an alternative memory management technique where memory is divided into variable-length blocks called segments. Each segment contains a logical unit like a function, array, or stack.
| Paging | Segmentation |
|---|---|
| Fixed-size blocks (pages) | Variable-size blocks (segments) |
| Can lead to internal fragmentation (unused space in pages) | Reduces internal fragmentation but increases external fragmentation |
| Hardware decides page size | User supplies segment number and size |
| Uses page table to map logical to physical addresses | Uses segment map table with segment number and offset |
| Invisible to user/programmer | Visible to user/programmer |
Check Your Understanding
8. What is virtual memory and why is it used? [3 marks]
Answer
[1 mark] Virtual memory uses secondary storage (disk) to extend RAM
[1 mark] Makes CPU appear to access more memory than physically available
[1 mark] Allows running larger programs or more programs than would fit in physical RAM
9. What causes disk thrashing and how does it affect system performance? [3 marks]
Answer
[1 mark] Caused by excessive paging when processes constantly need pages not in memory
[1 mark] System spends more time swapping pages than executing processes
[1 mark] Dramatically degrades performance - computer becomes very slow and unresponsive
10. State two differences between paging and segmentation. [2 marks]
Answer
[1 mark each for any two]
- Paging uses fixed-size blocks; segmentation uses variable-size blocks
- Paging can cause internal fragmentation; segmentation reduces internal fragmentation but increases external fragmentation
- Paging is invisible to user/programmer; segmentation is visible to user/programmer
- Paging uses page tables; segmentation uses segment map tables
Key Takeaways
- An Operating System (OS) acts as an interface between users and hardware, maximizing resource utilization
- The OS hides hardware complexities through GUIs, device drivers, and simplified file management
- Multitasking allows multiple processes to share CPU time through careful scheduling
- Processes exist in one of three states: Running (executing), Ready (waiting), or Blocked (waiting for I/O)
- Scheduling algorithms (FCFS, SJF, Round Robin, SRTF) determine which process gets CPU time next
- Round Robin prevents starvation by giving each process a fixed time slice
- SJF minimizes waiting time but requires knowing process duration in advance
- Virtual memory uses disk space to extend available RAM through paging
- Paging divides memory into fixed-size frames and processes into same-sized pages
- Segmentation uses variable-sized segments, reducing internal fragmentation but increasing external fragmentation
- Disk thrashing occurs when excessive paging slows down system performance
- The kernel is the core OS component handling process management, memory management, and interrupt handling
Question Bank
1. Explain how an operating system hides hardware complexities from users. [4 marks]
Marking Scheme & Answer
- [1 mark] Using GUI interfaces rather than command-line interfaces
- [1 mark] Using device drivers which simplify complexity of hardware interfaces
- [1 mark] Simplifying saving and retrieving of data from memory and storage devices
- [1 mark] Carrying out background utilities, such as virus scanning, without user intervention
2. Describe the three main process states and explain when a process moves between them. [6 marks]
Marking Scheme & Answer
- [2 marks each for describing states]
- Running: Process is being executed by processor; only one process can be in this state at a time
- Ready: Process is in queue waiting for processor's attention; ready to run but not currently executing
- Blocked/Waiting: Process is waiting for an event (e.g., I/O) and cannot be executed at the moment
- [Transition examples]
- Ready → Running: When scheduler selects process for execution
- Running → Blocked: When process requests I/O operation
- Blocked → Ready: When I/O operation completes
- Running → Ready: When time slice expires or higher priority process arrives
3. Compare and contrast First Come First Served (FCFS) and Shortest Job First (SJF) scheduling algorithms. [5 marks]
Marking Scheme & Answer
FCFS (First Come First Served):
- Non-preemptive algorithm
- Uses FIFO queue principle
- Simple to understand and implement
- Poor performance with high average wait time
- No starvation - every process eventually runs
SJF (Shortest Job First):
- Non-preemptive algorithm
- Executes processes in ascending order of CPU time required
- Minimizes average waiting time
- Requires knowing burst time in advance
- Impossible in interactive systems
- Can cause starvation for long processes
4. What is the purpose of a Process Control Block (PCB) and what information does it store? [5 marks]
Marking Scheme & Answer
[1 mark] Purpose: PCB is a data structure that contains all data needed for a process to run, allowing the OS to save and restore process state when switching between processes.
[4 marks] Information stored (1 mark each for any four):
- Current process state (ready, running, blocked)
- Process privileges (which resources it can access)
- Register values (PC, MAR, MDR, ACC)
- Process priority and scheduling information
- Amount of CPU time needed to complete
- Process ID for unique identification
5. Explain how paging works in virtual memory management. Include the terms: pages, frames, page table, and page fault. [6 marks]
Marking Scheme & Answer
- [1 mark] Process is divided into equal-sized pages
- [1 mark] Memory is divided into equal-sized frames of same size as pages
- [1 mark] Not all pages need to be loaded into memory to start execution
- [1 mark] Each process has a page table that maps pages to frames in memory
- [1 mark] When process needs a page not in memory, a page fault occurs
- [1 mark] Required page is loaded from disk, possibly replacing another page if memory is full
- [Additional] Pages can be scattered throughout available frames; page table tracks their locations
6. What are interrupts and how does the OS handle them? Give examples of different types of interrupts. [6 marks]
Marking Scheme & Answer
[2 marks] Definition: An interrupt is a signal sent to processor by hardware or software indicating they require processor attention.
[2 marks] Handling: OS kernel invokes interrupt-handling routine; current register values are saved in PCB; after handling, process can continue execution.
[2 marks] Examples (½ mark each for any four):
- Hardware interrupts: Printer out of paper, keyboard data entered, mouse click
- Software interrupts: Division by zero, file not found
- Clock interrupts: Generated by internal clock for scheduling
- I/O interrupts: Generated by input/output devices
7. What is the difference between preemptive and non-preemptive multitasking? Give examples of each. [4 marks]
Marking Scheme & Answer
Preemptive Multitasking:
- Resources are allocated to a process for limited time
- Process can be interrupted while running
- More flexible form of scheduling
- Example: Round Robin, SRTF scheduling
Non-Preemptive Multitasking:
- Once resources allocated, process retains them until completion
- Process cannot be interrupted while running
- More rigid form of scheduling
- Example: FCFS, SJF scheduling
8. Explain the objectives of process scheduling in an operating system. [5 marks]
Marking Scheme & Answer
- [1 mark each for any five]
- To keep CPU busy all the time to maximize throughput
- To give each process a fair share of CPU time, being fair to all users
- To allow all processes to complete in a reasonable amount of time
- To maximize use of peripherals
- To prevent deadlock by resolving conflicts between processes requiring same resources
- To allow multiprogramming
- To allow highest priority jobs to be executed first
- To service largest possible number of jobs in given amount of time
- To minimize amount of time users must wait for their results
9. What is Direct Memory Access (DMA) and how does it improve system performance? [4 marks]
Marking Scheme & Answer
[2 marks] DMA (Direct Memory Access) is a feature that allows hardware to access main memory independently of the CPU, using a DMA controller.
[2 marks] How it improves performance:
- Frees up CPU to carry out other tasks while slower I/O operations are taking place
- Prevents CPU from remaining idle while waiting for I/O operations to complete
- Allows direct data transfer between memory and I/O devices without CPU intervention
- Improves overall system throughput by parallelizing CPU and I/O operations
10. Describe how the operating system handles a page fault condition in virtual memory management. [4 marks]
Marking Scheme & Answer
- [1 mark] When a process tries to access a page that is not currently in main memory (as indicated by the page table), a page fault occurs
- [1 mark] The OS identifies which page is needed from the page table information
- [1 mark] If main memory is full, the OS selects a page to remove using a page replacement algorithm (e.g., FIFO, LRU)
- [1 mark] The required page is loaded from secondary storage (disk) into the freed frame in main memory
- [Additional] The page table is updated to reflect the new location of the page
- [Additional] The interrupted process can then continue execution from the point of the page fault