Operating Systems and RTOS Core Concepts

Operating System Fundamentals

Operating System: Types, Objectives, and Functions

Definition of Operating System

Software that manages hardware and software resources, acts as an interface between the user and hardware, and enables application execution.

Objectives of Operating Systems

  • Convenience: User-friendly interaction.
  • Efficiency: Optimum use of CPU, memory, and I/O devices.
  • Ability to Evolve: Integrate new system functions.
  • Security & Protection: Prevent unauthorized access and protect data.
  • Resource Allocation: Schedule and manage system resources.

Types of Operating Systems

  • Batch OS: Processes similar jobs in batches.
  • Time-Sharing OS: CPU allocated by time slicing to users.
  • Real-Time OS (RTOS): Guarantees response within a specified time.
  • Distributed OS: Manages resources across multiple machines.
  • Network OS: Manages users, security, and data over a network.

Functions of Operating Systems

  • Process Management: Creating, scheduling, and terminating processes.
  • Memory Management: Allocating and deallocating memory.
  • File System Management: Managing data storage and access.
  • Device Management: Controlling communication with I/O devices.
  • Security: Protecting data and access rights.
  • Job Scheduling: Managing multitasking and time-sharing.
  • Error Detection: Detecting and responding to errors.

Kernel Architecture and Functions

Kernel Definition

The core part of an Operating System that manages low-level tasks like process and hardware control, residing in main memory.

Kernel Functions

  • Process Management: Process creation, scheduling, and deletion.
  • Memory Management: RAM allocation and memory protection.
  • File System Management: Access, storage, and retrieval of files.
  • Device Management: Communicates with I/O devices via drivers.
  • Interrupt Handling: Handles internal and external interrupts.
  • I/O Communication: Manages input and output operations.

Process Management: States, PCB, and Operations

Process Definition

A program in execution containing its current activity, code, data, and resources.

Process Elements

  • Program Code: Executable instructions.
  • Data Section: Global/static variables.
  • Stack: Stores function calls and local variables.
  • Heap: Manages dynamic memory allocation.
  • Process Control Block (PCB): Contains metadata and management information.

Process Control Block (PCB) Details

  • Process ID: Unique identifier.
  • State: Current execution status.
  • Program Counter: Address of the next instruction.
  • Registers: Processor context and temporary data.
  • Memory Pointers: Base and limit registers.
  • I/O Info: Devices used, open files.
  • Accounting Info: Resource usage, priority level.

Process States: Five-State Model

  • New: Process being created.
  • Ready: Waiting to be assigned CPU.
  • Running: Currently executing on CPU.
  • Blocked: Waiting for an I/O event.
  • Exit: Process completed or terminated.

Process State Transitions

  • New → Ready: Admitted by OS.
  • Ready → Running: Scheduled to CPU.
  • Running → Blocked: Waiting for I/O/event.
  • Blocked → Ready: I/O completed.
  • Running → Ready: Preempted by scheduler.
  • Running → Exit: Normal completion/error termination.

Process Diagrams

  • Two-State Model: Running, Not Running.
  • Five-State Model: New, Ready, Running, Blocked, Exit (with transitions).

Process Operations

  • Creation: Initiated by OS, user, or parent process.
  • Spawning: Parent process creates child process.
  • Termination: Process ends normally, with error, or by OS intervention.

Process Scheduling and Thread Management

Process Scheduling Concepts

Levels of Scheduling

  • Long-Term Scheduling: Decides which jobs are admitted to the system.
  • Medium-Term Scheduling: Handles swapping processes between memory and disk.
  • Short-Term Scheduling (CPU Scheduling): Selects a ready process for CPU assignment.

Scheduling Criteria

  • CPU Utilization: Maximize CPU busy time.
  • Throughput: Number of processes completed per time unit.
  • Turnaround Time: Total time from submission to completion.
  • Waiting Time: Time spent in the ready queue.
  • Response Time: Time from request submission to first response.
  • Fairness & Starvation: Ensure fair CPU access, avoid indefinite waiting.

Common Scheduling Algorithms

  • FCFS (First Come First Serve): Non-preemptive, processes jobs in order of arrival, simple but poor for short jobs behind long ones, not ideal for interactive systems.
  • SJF (Shortest Job First): Selects process with shortest burst time, can be preemptive (SRTF) or non-preemptive, optimal average waiting time, risk of starvation for longer jobs.
  • Priority Scheduling: Each process assigned a priority, higher priority executed first, can be preemptive or non-preemptive, aging used to prevent starvation.
  • Round Robin (RR): Preemptive, each process given a time quantum; if burst > quantum, added to end of ready queue, performance sensitive to quantum size, good for time-sharing systems.
  • Multilevel Queue Scheduling: Ready queue split into multiple queues based on priority/type, each queue has its own scheduling algorithm, no movement between queues.
  • Multilevel Feedback Queue Scheduling: Allows movement between queues, promotes fairness, long processes move to lower queues, aging brings old processes back up, complex to manage.

Preemptive vs. Non-Preemptive Scheduling

  • Preemptive: Interrupts a running process, better response time, higher overhead, good for real-time systems.
  • Non-Preemptive: Process runs until exit/wait, simpler implementation, less context switching.

Threads: Structure, Types, and Models

Process vs. Thread: Key Differences

  • Process: Independent execution, own PCB and address space, slow inter-process communication, high creation/termination overhead.
  • Thread: Lightweight, shares address space within a process, fast communication, low creation/termination overhead.

Thread States

  • Running: Currently executing.
  • Ready: Prepared to execute, waiting for CPU.
  • Blocked: Waiting for an event or resource.

Thread Operations

  • Spawn: Create a thread.
  • Block: Suspend thread waiting for event.
  • Unblock: Resume ready thread.
  • Finish: Thread execution completed.

Types of Threads: User and Kernel Level

  • User-Level Threads (ULT): Managed in user space, kernel unaware, faster, less control, limited by single-threaded kernel.
  • Kernel-Level Threads (KLT): Managed by kernel, true parallelism possible, slower context switches, greater overhead.

Multithreading Models

  • Many-to-One: Multiple user threads mapped to one kernel thread, simple but poor concurrency.
  • One-to-One: Each user thread mapped to one kernel thread, true parallelism, overhead for thread creation.
  • Many-to-Many: User threads mapped to many kernel threads, combines benefits of both models, scalable.

Multiprocessor Scheduling

Multiprocessor Systems

Multiple CPUs handle processes to improve performance.

Symmetric Multiprocessing (SMP)

All processors are peers, self-scheduling from a common ready queue, balancing load dynamically.

Asymmetric Multiprocessing (AMP)

One master processor schedules, others execute as assigned, simpler but less efficient.

Multiprocessor Scheduling Challenges

  • Load Balancing: Distribute work evenly among CPUs.
  • Resource Sharing: Manage access to shared data, avoid race conditions.
  • Processor Affinity: Bind a process to a specific CPU for better cache performance.

Context Switching Explained

Switching CPU from one process or thread to another, involves saving the current context and loading a new context, incurring overhead.

Real-Time Systems and Inter-Task Communication

Real-Time Operating Systems (RTOS)

RTOS Definition

An OS designed to process data and events within guaranteed time limits, ensuring task completion before deadlines.

RTOS Characteristics

  • Determinism: Predictable, bounded response time.
  • Responsiveness: Quick handling after interrupts.
  • User Control: Allows setting task priorities, memory access.
  • Reliability: System degrades gracefully on failure.
  • Fail-soft Operation: Critical tasks prioritized during system failure.

RTOS Structure

  • Two main parts: controlling system and controlled system.
  • Supports periodic communication (at intervals) and aperiodic communication (event-driven).
  • Kernel components: scheduler, kernel objects (tasks, semaphores), services (timing, interrupt handling).

RTOS Tasks and States

Task Definition in RTOS

A schedulable execution unit (thread) with its own ID, stack, priority, and execution routine.

RTOS Task States

  • Ready: Task ready for CPU, waiting in queue.
  • Running: Task actively executing on CPU.
  • Blocked: Task waiting for resource/event.
  • Suspended: Paused manually, typically for debugging.
  • Pended: Waiting for specific resource availability.
  • Delayed: Waiting for a timed delay to expire.

Task Synchronization: Semaphores and Mutexes

Semaphore Definition

A kernel object used to synchronize tasks or manage resource access.

Types of Semaphores

  • Binary Semaphore: Value 0 or 1, ensures mutual exclusion.
  • Counting Semaphore: Counts available resource instances.
  • Mutex (Mutual Exclusion Semaphore): Special binary semaphore with ownership, avoids priority inversion, allows recursive locking.

Mutex Features

  • Recursive Locking: Task can reacquire its own lock.
  • Priority Inversion Handling: Via Priority Inheritance Protocol or Ceiling Protocol.
  • Task Deletion Safety: Prevents deletion of a mutex-owning task.

Inter-Task Communication (ITC)

Message Queues for ITC

Structured buffer for sending/receiving messages between tasks or ISRs.

  • Components: Queue Control Block (QCB), ID, buffer, waiting task list.
  • Features: Supports blocking/non-blocking operations, FIFO or priority-based task management.

Pipes for Inter-Task Communication

Unstructured byte stream, used for task communication.

  • FIFO order, blocking on full/empty, can be named (system-wide) or unnamed (local).

Message Queue vs. Pipe Comparison

  • Message Queue: Structured message passing, prioritization possible.
  • Pipe: Byte-stream, no message structure, simple FIFO.

Event Registers

Binary flags associated with a task, typically 8/16/32 bits.

  • Used to track events, supports logical AND/OR matching for event conditions.

Signals in RTOS

Software-generated asynchronous notifications to tasks.

  • Can be blocked, pending, or processed, triggered by system events.

Signal vs. Interrupt Comparison

  • Signal: Software-generated, task-level notification, limited control by task.
  • Interrupt: Hardware-generated, immediate system action, preempts task execution.

RTOS Task Constraints, Scheduling, and Kernel

Real-Time Task Constraints

Timing Constraints in RTOS

In real-time systems, task correctness depends on both result and timing; a task must complete before its deadline.

Task Classification by Deadline

  • Hard Real-Time: Missing deadline = system failure (e.g., pacemaker).
  • Firm Real-Time: Missing deadline = output useless but system continues (e.g., video streaming).
  • Soft Real-Time: Missing deadline = degraded performance but no failure (e.g., audio playback).

Periodic vs. Aperiodic Tasks

  • Periodic Tasks: Arrive at regular intervals (e.g., temperature monitoring).
  • Aperiodic Tasks: Arrive unpredictably (e.g., sensor-triggered events).
  • Sporadic Tasks: Aperiodic with a minimum inter-arrival time.

Aperiodic Task Scheduling Algorithms

Earliest Due Date (EDD) Scheduling

Schedules job with the nearest due date first, minimizes maximum lateness, ignores execution time, suited for soft/firm real-time systems.

Earliest Deadline First (EDF) Scheduling

Dynamic priority scheduling, process with the earliest absolute deadline scheduled first, preemptive, optimal for independent, preemptive tasks.

Latest Deadline First (LDF) Scheduling

Opposite of EDF, lower priority to earlier deadlines, used in less-critical scheduling scenarios.

EDF with Precedence Constraints

Tasks executed based on dependencies; predecessors must complete before successors, dependencies visualized via precedence graphs.

Periodic Task Scheduling Algorithms

Rate Monotonic Scheduling (RMS)

Static priority scheduling based on task period; shorter period = higher priority, assumes tasks are independent, periodic, preemptive, fixed execution times.

Deadline Monotonic Scheduling (DMS)

Static priority scheduling based on relative deadlines; shorter deadline = higher priority, handles constrained deadlines better than RMS.

RMS vs. DMS Comparison

  • RMS: Priority based on period, simpler, common, may fail if deadline < period.
  • DMS: Priority based on deadline, more flexible, handles deadline < period situations.

RTOS Schedulability Test

Utilization Bound (Liu and Layland Formula):

U = ∑(Ci/Ti) ≤ n(2^(1/n) – 1)

If total utilization satisfies this, the task set is schedulable.

Real-Time Kernel: Structure and Primitives

RTOS Kernel Structure

  • Task Control Blocks (TCBs): Stores task information.
  • Ready Queue: List of tasks ready for execution.
  • Scheduler: Decides the next task to execute.
  • Timer: Handles timeouts, delays.
  • Inter-Task Communication Objects: Semaphores, message queues, pipes.

Kernel State Transition Diagram

Depicts transitions between task states:

  • Ready → Running → Waiting → Delayed.
  • Transitions occur based on events like timeouts, I/O events, task preemption.

RTOS Kernel Primitives

  • CreateTask(): Initialize and activate a new task.
  • DelayTask(): Block a task for a specified duration.
  • DeleteTask(): Remove a task.
  • ResumeTask(): Move task from suspended to ready.
  • SuspendTask(): Move task from running/ready to suspended.