Basic OS Review. At its core, the OS is a resource…

At its core, the OS is a resource manager and virtualization layer between hardware and user programs.

Key Topics:

  1. Processes and Threads:
    – Threads share address space; processes may use IPC for communication.
    – Both need synchronization for shared resources.
  2. Memory Management:
    – MMU translates virtual → physical addresses using the page table.
    – Page faults occur if a needed page is not in RAM.
    – OS handles loading pages from disk and manages page replacement.
  3. CPU Scheduling:
    – Scheduler chooses which process/thread to run next.
    – Context switch saves/restores process state.
    – Mode switch occurs during system calls to enter kernel mode.
  4. Concurrency & Synchronization:
    – Prevent data races using mutual exclusion (locks, semaphores).
    – Ensure event ordering among concurrent processes/threads.
Basic OS Review

Main Goals of OS

  1. Abstraction (Virtualization)
    Goal: Make hardware easier to use.
    – The OS hides the messy details of hardware (like disks, memory, CPUs) and gives each program a simple, consistent interface.
    – For example:
    – Instead of directly controlling a disk, we use files.
    – Instead of controlling the CPU, we run processes.
    – Instead of managing memory chips, we get virtual memory.
    Key idea: Each program feels like it has the entire machine to itself — this illusion is virtualization.
  2. Resource Management
    Goal: Share hardware efficiently and fairly among programs.
    – The OS decides who gets what and when.
    – It manages:
    – CPU time — via scheduling.
    – Memory — via allocation and protection.
    – Storage and I/O devices — via buffering, queues, and drivers.
    Example:
    – When we open multiple apps, the OS switches between them (CPU scheduling).
    – When one app runs out of memory, the OS ensures it doesn’t crash the whole system.
  3. Protection and Security
    Goal: Prevent processes from interfering with each other or the system.
    The OS enforces boundaries:
    – One app cannot overwrite another’s memory.
    – Unauthorized users can’t access protected files.
    – Uses user mode and kernel mode to separate privileges.
    Example:
    – When an app needs to read a file, it must ask the OS for permission.
    – The OS checks access rights before allowing the operation.
  4. Efficiency and Performance
    Goal: Use hardware resources optimally.
    – Minimize CPU idle time.
    – Optimize memory usage.
    – Reduce disk and I/O wait times.
    – Example:
    – If one process waits for I/O, the OS runs another process on the CPU to avoid wasting time.

System Call

  • Entry point to OS code.
  • Allow applications to request services from the operating system.
  • Provide controlled access to privileged kernel operations.
  • Language APIs or library functions typically act as interfaces to system calls.
  • High-level I/O functions map user parameters into system-call format.
  • In UNIX systems, fork() and execve() are wrapper functions that invoke corresponding system calls.
  • It serves as the boundary between user mode and kernel mode.
  • Triggered via a software interrupt or trap instruction.
  • Return the results back to the user programs after the OS completes the requested operation.
Some UNIX System Calls

Dual-Mode Execution (Execution Modes)

  • Provides a protection mechanism between user programs and the operating system.
  • The processor operates in two modes:
    User Mode — Restricted mode for executing user applications.
    Kernel (Supervisor) Mode — Privileged mode for executing OS code.
  • Purpose: Prevent user programs from performing critical or unsafe operations.
  • Critical operations (e.g., direct device access, modifying memory tables, disabling interrupts) are only allowed in kernel mode.
  • Mode Bit:
    Hardware flag that indicates the current mode of operation.
    0 → Kernel mode, 1 → User mode.
    – Set automatically by the processor when a system call or interrupt occurs.
  • Privileged Instructions:
    Instructions that can only execute in kernel mode.
    – Examples: I/O operations, interrupt control, and memory management setup.
  • Upon a violation (attempting to execute a privileged instruction in user mode), a trap is generated, and control is transferred to the OS.

Mode Switching

  • Occurs when control transitions between user mode and kernel mode.
  • Triggered by system calls, interrupts, or exceptions.
  • System calls provide the controlled mechanism for crossing the user–kernel boundary.
  • A special instruction (software interrupt or trap) invokes the kernel routine.
  • Hardware automatically switches the CPU to kernel mode during this process.
  • OS executes the requested kernel code, performs the operation, and prepares the result.
  • Upon completion, control returns to the user process, and the CPU switches back to user mode.
  • Ensures safe execution of privileged operations while maintaining process isolation.

Processing a System Call

  • Switching between user mode and kernel mode introduces overhead.
  • Kernel actions during a system call:
    – Save CPU registers to preserve process state.
    – Verify system call type and parameters.
    – Check permissions of the calling process.
    – Execute the corresponding kernel function.
    – Restore saved registers after completion.
    – Return control to the user process in user mode.
  • Additional overhead may occur due to cache misses or pipeline flushes during the switch.

Process Definition

  • A process (or task) is an instance of a program in execution.
  • Represents both the static program (code) and the dynamic execution state.
  • Includes current values of registers, program counter, stack, and memory.
  • The process state evolves as execution proceeds; register contents, memory data, and the program counter are continuously modified.
  • Forms the fundamental unit of work managed by the operating system.

Process Image

  • Represents the current status of a process.
  • Components include:
    Executable code — instructions of the program.
    Static data area — global and static variables.
    Stack & Heap area — stack for function calls, heap for dynamic memory.
    Process Control Block (PCB) — data structure storing execution context/state (registers, program counter, scheduling info, etc.).
    Other management information — I/O status, open files, permissions, etc.

Process Execution States

Process State Transition Diagram

Basic States

  • Running — Process is currently executing on the CPU.
  • Ready — Process is prepared to run but waiting for CPU allocation.
  • Blocked (Sleeping) — Process is waiting for an event or I/O operation to complete.

Other States

  • New — Process is being created but not yet ready to execute.
  • Exit — Process has finished execution and is being terminated.
  • Suspended (Swapped) — Process is temporarily moved to secondary storage (disk) to free up memory.
  • Suspended Blocked — Process is blocked and swapped out of main memory.
  • Suspended Ready — Process is ready to run but swapped out of main memory.

Context Switch (Process Switch)

  • Involves two processes:
    – One leaves the Running state.
    – Another enters the Running state.
  • Operations performed:
    – Save the context (CPU registers, program counter, stack pointer, etc.) of the outgoing process.
    – Restore the context of the incoming process.
  • Note:
    – Different from a mode switch, which only changes CPU mode (user ↔ kernel) and affects a single process.
    – Purpose: Enables multitasking by allowing multiple processes to share the CPU.
Context Swith and Model Switch

Concurrent Processes

  • Two processes are concurrent if their executions overlap in time.
  • Uniprocessor systems: Multiprogramming provides concurrency by interleaving the execution of processes.
  • Multiprocessor / multicore systems: True parallel execution occurs, with multiple processes running simultaneously on different CPUs or cores.

Key idea: Concurrency ≠ parallelism; overlapping execution can be time-shared or simultaneous.

Concurrency → Multiple tasks make progress at the same time, but not necessarily executing simultaneously; they can take turns on one processor (time-sharing).

Parallelism → Multiple tasks actually run at the same time on multiple processors or cores.

Advantages of Concurrency

  • Improved CPU Utilization: CPU can work on other processes while some are waiting for I/O.
  • Better Responsiveness: Interactive processes can respond without waiting for long tasks to finish.
  • Resource Sharing: Multiple processes can share system resources efficiently.
  • Throughput Increase: More work completed in the same time period.
  • Supports Modular Design: The System can be designed as independent concurrent processes.

Challenges of Concurrency

  • Race Conditions: Multiple processes accessing shared data may cause inconsistencies.
  • Deadlocks: Processes may wait indefinitely for resources held by each other.
  • Complexity: Programming and debugging concurrent systems is harder.
  • Context Switching Overhead: Frequent switching reduces CPU efficiency.

Forms of Concurrency

1. Multiprogramming
Creates logical parallelism on a single CPU.
– Multiple processes/threads are kept in memory simultaneously.
– OS selects a ready process to execute.
– When a process waits (e.g., I/O), the CPU switches to another process.
Primary objective: Eliminate CPU idle time.

2. Multiprogramming with Time Sharing
Extension of multiprogramming.
– CPU switches between processes after a fixed time slice, regardless of waiting status.
– Switching occurs frequently, allowing user interaction with each program.
Primary objective: Fair distribution of CPU time among all users.

3. Multiprocessing
Multiple processors/cores on a single system run processes simultaneously.
– Creates true physical parallelism.
– Increases throughput and execution speed for CPU-bound tasks.

Process Types

1. Batch Processes
– Execution based on pre-provided input files; output is written to files.
– No user interaction during execution.
– Suitable for basic multiprogramming systems where the CPU is kept busy.
– Common in large-scale computations, payroll, or automated data processing.

2. Interactive / Time-Sharing Processes
– User interacts with the process while it runs.
– Programs prompt for input and provide immediate feedback.
– Requires an interactive/time-sharing OS to allow responsive CPU allocation.
– Common in text editors, shells, GUIs, and online systems.

Batch → runs in background, system schedules CPU efficiently.
Interactive → requires frequent CPU access for responsiveness.

Protection

  • Necessary when multiple processes/threads execute concurrently.
  • Prevents mutual interference between processes.
  • Memory Protection / Isolation: Ensures one process cannot access another process’s physical address space.
  • Techniques for memory protection:
    – Base and Limit Registers:
    Define the allowed memory range for a process.
    Virtual Memory: Each process gets its own logical address space, mapped to physical memory by the OS.
  • Purpose: Maintain process isolation, system stability, and security.

Processes and Threads

  • Traditional (Single-threaded) Processes:
    Can execute only one task at a time.
  • Multithreaded Processes:
    Can perform multiple tasks concurrently within the same process.
    – Conceptually, several threads run simultaneously.
  • Thread:
    A separately schedulable execution context within a process.
    – Shares process resources (memory, files) but has its own program counter, stack, and registers.

Key Idea: Threads allow concurrency within a single process, improving responsiveness and resource utilization.

Threads

  • Multiple threads share the address space and resources (e.g., open files) of a single process.
  • Each thread has its own stack, program counter (PC), and Thread Control Block (TCB).
  • Threads execute separate sections of code and maintain private data.
  • All threads can access the global data of the parent process.
  • Purpose: Enable concurrent execution within a process while sharing resources efficiently.
Thread vs Process

Threads vs Processes (Shared Data & Overhead)

  • Processes:
    – Sharing data between processes requires OS intervention (IPC, pipes, shared memory).
    – Overhead: system calls, mode switches, context switches → extra execution time.
  • Threads:
    Threads within the same process share global data automatically.
    – Sharing is as simple as calling functions within the same process.
    – Lower overhead compared to processes.

Key Idea: Threads are lighter and faster for shared-data operations than separate processes.

Process (Thread) Scheduling

  • Definition: Deciding which process or thread runs next on the CPU.
  • Context: In a multiprogrammed system, multiple processes compete for a single CPU.
  • Types of Scheduling:
    Preemptive Scheduling: A running process can be interrupted before completion. Triggered by timer expiration or arrival of a higher-priority process.
    Non-preemptive Scheduling (implicit): Process runs until it completes or blocks; not interrupted by the OS.

Key Idea: Scheduling ensures fair and efficient CPU utilization among competing processes/threads.

Scheduling Algorithms

1. FCFS (First-Come, First-Served)
– Type: Non-preemptive.
– Behavior: Process runs until completion or blocks for an event.
– Queue: Processes served in arrival order.
– Use case: Simple, suitable for batch systems.

2. RR (Round Robin)
– Type: Preemptive FCFS variant.
– Time Slice / Quantum: Maximum CPU time a process can run before being preempted.
– Behavior: Process preempted at the end of the time slice → returns to Ready queue tail. Ensures fair CPU distribution among processes.
– Use case: Time-sharing / interactive systems.

Scheduling Goals

  • Optimize Turnaround Time: Minimize total time from process submission to completion.
  • Optimize Response Time: Minimize time from process request to first response (important for interactive systems).
  • Optimize Throughput: Maximize the number of processes completed per unit time.
  • Avoid Starvation / Ensure Fairness: Prevent some processes from waiting indefinitely.
  • Respect Priorities: Higher-priority processes may run before lower-priority ones.
  • Static vs Dynamic Scheduling:
    – Static: Decisions made before execution; priorities fixed.
    – Dynamic: Decisions made during execution; priorities may change.

Interprocess Communication (IPC)

Allows cooperating processes or threads to exchange information.

Processes:

  • Use IPC mechanisms because each process has a separate memory space.
    Two main approaches:
    – Shared Memory — explicit setup of common memory; efficient but needs synchronization.
    – Message Passing — send/receive messages; simpler but slower due to copying.

Threads:

  • Communicate directly via shared memory of the same process.
  • No need for traditional IPC unless threads are in different processes.

Key Idea:

  • IPC is essential for process coordination.
  • Threads naturally share data, so communication is simpler and faster within the same process.

Process / Thread Synchronization

  • Definition: Coordinating concurrent processes or threads to ensure correct execution order.
  • Reason: Concurrent processes are asynchronous; the order of events cannot be predicted.
  • Purpose: If processes or threads share resources or exchange information, synchronization is necessary to:
    – Avoid race conditions
    – Ensure data consistency
    – Maintain correct program behavior

Key Idea: Synchronization controls access to shared resources and ensures cooperative execution in concurrent systems.

Process Synchronization

Synchronization is essential whenever multiple execution contexts (processes or threads) share resources or interact. It ensures the correct order of execution among concurrent processes.

  • Event Ordering:
    Controls the sequence of events between concurrent processes.
    – Example: Ensure event 2 in process A occurs before event 4 in process B.
  • Mutual Exclusion (Critical Section Problem):
    When a process is using a shared resource, no other process can access it simultaneously.
    – Prevents data inconsistency and race conditions.
    – Critical Section (CS) is a code segment that accesses shared data or resources.
    – Only one process at a time can execute its CS (“lock” the critical section).
    – Separate shared resources can be accessed concurrently if they don’t overlap.

Key Idea: Synchronization ensures the correct order of execution and safe access to shared resources in concurrent systems.

Data Race

A data race occurs in concurrent programming when two or more threads access the same shared memory location simultaneously, and at least one of the accesses is a write operation, without proper synchronization mechanisms in place. This leads to undefined or unpredictable behavior, as the outcome depends on the timing and order of thread execution, which can vary between runs.

Semaphores

A semaphore is an integer used to control access to shared resources. Its value indicates how many processes can access the resource. Operations on a semaphore are atomic (cannot be interrupted).

  • Definition: Integer variable S used for synchronization, accessed only via atomic operations:
    – Initialize(S) — set initial value
    – P(S) / wait(S) — decrement S; may block process if S < 0
    – V(S) / signal(S) — increment S; may wake a blocked process
  • Requirement: All operations must be indivisible (atomic); no other access to S allowed during operation.
  • Purpose / Why Needed:
    Enforce mutual exclusion in critical sections
    – Coordinate event ordering among processes/threads
    – Prevent race conditions and ensure correct concurrent execution

Key Idea: Semaphores are OS-provided synchronization tools to safely manage shared resources and execution order in concurrent systems.
Semaphore keeps track of resource availability and controls process access to avoid conflicts.
Works as a lock (binary semaphore) or counter for multiple resources (counting semaphore).

Other Mechanisms for Mutual Exclusion

  1. Spinlocks
    – Busy-waiting solution: process continuously tests a lock variable until the critical section is free.
    – Implemented with atomic machine-level instructions.
    – Suitable for short critical sections to avoid context switch overhead.
  2. Disable Interrupts
    – Interrupts are disabled before entering the critical section and enabled after leaving.
    – Ensures the current process is not preempted while in CS.
    – Mainly used in uniprocessor systems.

Key Idea: These mechanisms ensure mutual exclusion but have trade-offs in efficiency, scalability, and applicability.

Deadlock

  • Definition: A set of processes is deadlocked when each process is blocked, waiting for a resource held by another process in the set.
  • Key Point: Deadlocks cannot be resolved by the processes themselves; external intervention is required.
  • Consequence: All involved processes cannot proceed, halting part of the system.

Key Idea: Deadlock occurs due to circular wait for resources, and must be handled using prevention, avoidance, or detection mechanisms.

Deadlock vs Starvation

Key Idea:
Deadlock = circular wait; total halt.
Starvation = unfair scheduling; delayed progress.

Deadlock Management Strategies

  1. Prevention
    Design the system so that at least one of the four necessary conditions for deadlock cannot occur.
    – Example: eliminate circular wait by imposing a resource ordering.
  2. Avoidance
    Allocate resources carefully, ensuring enough are available for all processes to complete.
    – Example: Banker’s Algorithm.
  3. Detection
    Periodically check for deadlocks in the system.
    – If detected, abort one or more processes or take corrective action to resolve.

Key Idea: Choose a strategy based on system type, performance needs, and risk tolerance.

Analysis of Deadlock Management

  • Practical Use: Most systems do not implement general deadlock management due to cost and complexity.
  • Reasons:
    – Time-consuming: Detecting or avoiding deadlocks frequently adds overhead.
    – Restrictive: Prevention strategies may limit resource utilization.
  • Exceptions:
    – Transaction systems: Use rollback to recover from deadlocks.
    – Lock ordering techniques: Control the sequence of resource acquisition to reduce deadlock risk.

Key Idea: Deadlock management is often avoided in general-purpose systems; handled selectively in specialized environments.

Memory Management

  • Primary Memory Sharing: Memory is shared between the OS and user processes.
  • Protection:
    – OS must protect itself from user processes.
    – Each user process must be isolated from others.
  • Efficiency: OS manages memory allocation and deallocation to ensure processes can execute efficiently.

Key Idea: Memory management balances protection, isolation, and efficient utilization of physical memory.

Memory Allocation — Single Process

  • Early Approach:
    OS reserved a protected portion of memory; the rest belonged to one process at a time.
  • Process Ownership:
    The process controlled all resources during execution.
    – Execution continued until the process completed.

Key Idea: Early systems used simple, single-process memory allocation, with minimal protection and no multitasking.

Memory Allocation — Multiple Processes (Contiguous)

  • Multiprogramming / Time-Sharing: Multiple processes reside in memory simultaneously.
  • Contiguous Allocation: Each process image is stored in a single, contiguous memory block.
  • Drawbacks:
    A limited number of processes can fit in memory.
    – Memory fragmentation reduces efficiency.

Motivation for Virtual Memory

  • Reduce fragmentation and better utilize memory.
  • Increase concurrency: More processes can execute simultaneously.
  • Method:
    Allow non-contiguous loading of process segments.
    – Allow execution even if the process is partially in memory.

Key Idea: Virtual memory solves fragmentation and concurrency limits inherent in contiguous allocation.

Virtual Memory — Paging

Virtual Memory: Paging
  • Concept: Program’s address space divided into pages (contiguous blocks of memory).
  • Page Size: Typically a power of 2, often 4 KB or larger.
  • Physical Memory: Divided into page frames of the same size as a page.
  • Mapping: Any program page can be loaded into any available frame in memory.
  • Advantage: No memory space is wasted; it eliminates external fragmentation.

General Idea

  • Load only the pages a process needs currently (saves space).
  • Principle of Locality: Programs tend to use data and instructions near recently used ones.
  • Result: More processes or threads can reside in memory simultaneously.

Problems in Paging

  • Which pages are needed?
  • Where are the pages located?
  • How to translate virtual addresses to physical?

Solutions

  • Demand Paging: Load pages only when required.
  • Page Table: Keeps track of where each page resides.
  • MMU (Memory Management Unit): Hardware translates virtual addresses to physical addresses (RAM) using the page table.
    – If the physical page is in RAM: Access proceeds normally.
    – If the physical page is not in RAM: A page fault occurs → OS loads the page from disk into RAM.

OS Responsibilities

  • Maintain page tables.
  • Manage page replacement when memory is full.

Key Idea: Paging allows efficient memory use, supports more concurrency, and enables virtual memory execution.
If the page is not present in physical memory (RAM), a page fault occurs.
OS then loads the page from disk (backing store) into RAM before the process can continue.

File Systems

  • OS Role: Maintains a shared file system for all users and processes.
  • Single-User Systems: Provide data protection, efficient lookup, and reliability.
  • Multi-User Systems: Require additional access control to protect files from unauthorized access.

Disk Management

  • Responsibilities:
  • Allocate disk space efficiently.
  • Keep track of file locations on disk.
  • Challenges:
  • Faces issues similar to main memory management, including fragmentation.

Key Idea: The file system ensures data organization, protection, and efficient storage access for users and processes.

Learn more about Basic OS Review. At its core, the OS is a resource…

Leave a Reply