Operating Systems
Introduction
An Operating System (OS) is the most important software that runs on a computer. It acts as an intermediary between the computer hardware and the end-user, managing everything from your keyboard and mouse to the CPU and memory. Without an OS, a computer is just a collection of electronic parts. Popular examples include Windows, macOS, Linux, Android, and iOS. Understanding how an OS works is crucial for writing efficient, reliable, and secure applications.
Core Concepts
Core Concepts
1. Process Management
- Process vs. Program: A program is a passive set of instructions on disk. A process is a program in execution.
- Process State: The current state of a process (e.g., new, running, waiting, ready, terminated).
- CPU Scheduling: The OS must decide which process gets to use the CPU and for how long.
- Scheduling Algorithms: First-Come, First-Served (FCFS), Shortest-Job-Next (SJN), Round Robin.
- Inter-Process Communication (IPC): Mechanisms for processes to communicate with each other (e.g., pipes, shared memory).
2. Memory Management The OS is responsible for allocating and deallocating memory space to programs that need it.
- Virtual Memory: A technique that gives a process the illusion that it has its own contiguous private memory space, which can be larger than the physical RAM.
- Paging: Dividing memory into fixed-size blocks called pages.
- Segmentation: Dividing memory into logical segments of varying sizes (e.g., code segment, data segment).
- Page Replacement Algorithms: When memory is full, the OS must decide which page to swap out (e.g., FIFO, LRU - Least Recently Used).
3. File System Management The OS provides a structured way to store and access files on storage devices.
- Files and Directories: The OS organizes data into files and folders (directories) in a hierarchical structure.
- File Operations: Creating, reading, writing, deleting files.
- Access Control: Managing permissions to determine who can read, write, or execute files.
- Common File Systems: NTFS (Windows), APFS (macOS), EXT4 (Linux).
4. Concurrency and Synchronization Managing multiple processes or threads that execute concurrently.
- Threads: The smallest unit of processing that can be performed in an OS. A process can have multiple threads.
- Race Condition: A situation where the result of an operation depends on the unpredictable sequence or timing of other events.
- Deadlock: A state in which two or more processes are blocked forever, each waiting for the other to release a resource.
- Synchronization Primitives: Tools to prevent race conditions and deadlocks.
- Mutexes (Mutual Exclusion): Ensures that only one thread can access a critical section of code at a time.
- Semaphores: A more general synchronization tool that can allow a certain number of threads to access a resource.
5. I/O Management The OS manages communication with hardware devices like disks, keyboards, printers, and network cards through device drivers.
Examples & Use Cases
Examples & Use Cases
Analogy for a Process Imagine cooking a recipe.
- The Program: The written recipe in a cookbook.
- The Process: You, the chef, actually gathering ingredients and following the steps.
- The CPU: The countertop space where you are actively working.
- I/O Waiting: Waiting for the oven to preheat.
Mutex in Pseudocode
// Both Thread A and Thread B want to increment a shared counter
shared_variable counter = 0;
mutex lock;
function increment_counter() {
lock.acquire(); // Acquire the lock
// --- Critical Section Start ---
int temp = counter;
temp = temp + 1;
counter = temp;
// --- Critical Section End ---
lock.release(); // Release the lock
}
Without the mutex, a race condition could occur where both threads read the same value of counter
and only one increment is saved.
Practice Questions
Practice Questions
1. MCQ: What is the primary purpose of virtual memory? A) To make the computer run faster. B) To allow a program to use more memory than is physically available. C) To store files permanently. D) To manage CPU scheduling.
2. Short Answer: What is a deadlock? Give a simple real-world analogy.
3. Problem: Consider three processes with the following CPU burst times: P1 (24ms), P2 (3ms), P3 (3ms). If they arrive in the order P1, P2, P3, what is the average waiting time using the First-Come, First-Served (FCFS) scheduling algorithm?
Quick Summary
Quick Summary
- The Operating System is the core software that manages a computer's hardware and software resources.
- Process Management deals with executing programs and scheduling CPU time.
- Memory Management handles the allocation of RAM and the use of virtual memory.
- File System Management provides a structured way to handle data on storage devices.
- Concurrency and Synchronization are crucial for managing multiple tasks that run at the same time, preventing issues like deadlocks and race conditions.
Further Reading
Operating System Concepts by Silberschatz, Galvin, and Gagne (The "Dinosaur Book")
(Book (Classic Textbook))
View Resource