BIT204 Operating System

Operating SystemUnit 1016 min read

System Architecture & I/O Systems: Hardware-Layer OS

Unit 10 of Operating System: Explores how OS interacts with hardware (CPU, memory, I/O), layered system models, I/O device management, and system architecture (uniprocessor vs. multiprocessor) with real-world examples like NTC’s network routers and eSewa’s transaction queues.

TAKEAWAYS:

  • The OS sits between hardware and applications, abstracting hardware details via layers (e.g., hardware → device drivers → OS → user programs).
  • I/O systems handle devices (keyboard, disk, network) using buffers, spooling, and interrupts to manage data flow efficiently.
  • System architecture classifies OS designs into uniprocessor, multiprocessor, and distributed systems, each with trade-offs in scalability and fault tolerance.
  • Interrupts and DMA (Direct Memory Access) optimize I/O performance by reducing CPU overhead.
  • Real-world examples: NTC’s routers use layered models for network traffic, Daraz’s order queues rely on I/O buffers for inventory management.
  • Exam tip: Focus on layered diagrams, I/O scheduling algorithms, and interrupt handling—these are high-weightage topics with direct question patterns.

1. System Architecture: Hardware-Layer OS

The OS acts as an intermediary between hardware and user applications. Its architecture defines how it interacts with the CPU, memory, and I/O devices. Below are the key components:

1.1 Layers of System Architecture

The OS is often modeled as a stack of layers, each providing abstraction to the layer above. This modularity simplifies design and maintenance.

flowchart TD
    A["User Programs"] --> B["Application Interface"]
    B --> C["System Calls"]
    C --> D["Kernel Services"]
    D --> E["Device Drivers"]
    E --> F["Hardware"]

Key Layers:

Layer Description Example in OS
User Programs Applications written in high-level languages (C, Python, Java). eSewa mobile app
System Calls Interface between user programs and OS (e.g., open(), read(), write()). fork() in Linux
Kernel Services Core OS functions (process management, memory, I/O). Scheduler in Windows
Device Drivers Software that controls hardware (keyboard, disk, network). NTC’s router firmware
Hardware Physical components (CPU, RAM, storage, I/O devices). NTC’s fiber-optic backbone

Why Layers Matter:

  • Abstraction: Users don’t need to know how a disk works to store a file.
  • Modularity: Changing one layer (e.g., replacing a disk driver) doesn’t break the entire system.
  • Security: User programs run in a restricted environment (user mode), while critical OS functions run in kernel mode.

1.2 System Architecture Classifications

OS architectures are classified based on hardware configuration:

Type Description Example Pros Cons
Uniprocessor Single CPU core handles all tasks. Old desktop OS (Windows XP). Simple, low cost. Bottleneck under heavy load.
Multiprocessor Multiple CPUs share the workload. Modern servers, supercomputers. High performance, scalability. Complex synchronization.
Distributed Multiple machines work together as a single system. Cloud computing (AWS, Google Cloud). Fault tolerance, global access. Network latency, complexity.
Old desktop OS (e.g., Windows XP)Single CPU, single threadUniprocessorModern servers, supercomputersMultiple CPUs with shared memoryMultiprocessorCloud computing (AWS, Google Cloud)Multiple machines as a single systemDistributedSystem Architecture
A multiprocessor system with shared memory and I/O buses. (Image: Ferry24.Milan, CC BY-SA 4.0, via Wikimedia Commons)

1.3 Uniprocessor vs. Multiprocessor Systems

Uniprocessor:

  • All processes compete for the single CPU.
  • Uses time-sharing to switch between tasks (e.g., keyboard input while running an app).
  • Example: A single-core Raspberry Pi running Linux.
User ProgramsSystem CallsKernel ServicesDevice DriversHardware
Layered architecture showing where uniprocessor vs. multiprocessor differences occur

Multiprocessor:

  • Multiple CPUs can execute tasks in parallel.
  • Symmetric Multiprocessing (SMP): All CPUs share the same OS and memory.
    • Example: A web server with 8 CPU cores handling multiple requests simultaneously.
  • Asymmetric Multiprocessing (AMP): Each CPU has a dedicated role (e.g., one handles I/O, another runs the scheduler).
    • Example: Some embedded systems.

Worked Example: NTC’s Network Routers NTC uses multiprocessor systems to handle millions of concurrent connections. Each CPU core processes a subset of traffic, reducing latency. If one core fails, others take over (fault tolerance).


2. I/O Systems: Managing Peripheral Devices

I/O (Input/Output) systems manage interactions between the CPU and peripheral devices (keyboard, disk, network). Inefficient I/O can cripple system performance.

2.1 I/O Device Classification

Devices are classified based on data transfer rate and access method:

Type Description Example Transfer Rate
Storage Devices High-capacity, slow access. HDD, SSD, USB drive. KB/s to MB/s
Network Devices Connects to other computers (wired/wireless). Ethernet card, Wi-Fi adapter. Mbps to Gbps
Human Interface Keyboard, mouse, monitor. Keyboard, touchscreen. Slow (characters/sec)
Real-time Devices Must respond within strict time limits (e.g., sensors). Thermostat, industrial robots. Varies (ms to µs)

2.2 I/O Buffering and Spooling

To optimize I/O performance, the OS uses buffers and spooling:

  • Buffering:

    • Temporary storage area for data in transit (e.g., keyboard input or disk reads).
    • Reduces CPU overhead by allowing devices to operate at their own pace.
    • Example: When you type on a keyboard, characters are stored in a buffer before being passed to the OS.
  • Spooling (Simultaneous Peripheral Operation Online):

    • Used for slow devices (e.g., printers).
    • Output is sent to a spool file (temporary storage) while the CPU continues processing.
    • Example: In a university lab, print jobs are spooled so students can keep working while their documents print.

Mermaid Diagram: Buffering in I/O

sequenceDiagram
    participant User
    participant Keyboard
    participant OS
    participant Printer

    User->>Keyboard: Types "Hello"
    Keyboard->>OS: Sends "Hello" to buffer
    OS->>OS: Processes buffer (e.g., saves to file)
    OS->>Printer: Sends data from spool file

2.3 Interrupts and DMA

The CPU cannot wait for slow I/O devices (e.g., disk reads). Two mechanisms handle this:

  1. Interrupts:

    • A signal sent to the CPU when an I/O device needs attention (e.g., keyboard press, disk completion).
    • Types of Interrupts:
      • Hardware Interrupt: Triggered by hardware (e.g., mouse click).
      • Software Interrupt: Triggered by software (e.g., system call like read()).
    • Interrupt Handling Flow:
      1. Device generates an interrupt.
      2. CPU saves current state (context switch).
      3. OS executes Interrupt Service Routine (ISR).
      4. CPU resumes normal operation.

    Mermaid Diagram: Interrupt Handling

stateDiagram-v2
	[*] --> CPU: Running user process
	CPU --> Device: Device triggers interrupt (e.g., disk ready)
	Device --> CPU: Sends interrupt signal
	CPU --> OS: Save state, jump to ISR
	OS --> Device: Handle interrupt (e.g., read data)
	OS --> CPU: Restore state, resume process
	CPU --> [*]
  1. Direct Memory Access (DMA):
    • Allows I/O devices to transfer data directly to/from memory without CPU intervention.
    • Reduces CPU load (critical for high-speed devices like network cards).
    • Example: When downloading a file from the internet, the network card uses DMA to write data to RAM while the CPU runs other tasks.

Worked Example: Downloading a File on Your Laptop

  • Your browser requests a file from the internet.
  • The network card uses DMA to transfer data chunks directly to RAM.
  • The CPU only intervenes when the transfer is complete (via interrupt).
  • Without DMA, the CPU would have to handle every byte, slowing down the entire system.

2.4 I/O Scheduling Algorithms

The OS schedules I/O requests to optimize device performance. Common algorithms:

Algorithm Description Pros Cons Example Use Case
FCFS (First-Come, First-Served) Processes requests in arrival order. Fair, simple. Poor for bursty I/O (e.g., many small requests). Printer queue in a lab.
SSTF (Shortest Seek Time First) Serves the request with the shortest disk arm movement. Reduces disk seek time. Can starve requests at the edges. Database indexing.
SCAN (Elevator Algorithm) Arm moves in one direction, serving all requests until it reaches the end. Balances disk usage. May still have long waits. File servers.
C-SCAN (Circular SCAN) Similar to SCAN but wraps around to the start after reaching the end. More balanced than SCAN. Still not perfect for all cases. RAID arrays.

Mermaid Diagram: Disk Scheduling (SCAN)

Disk head startsat position 100Serves requests: 5→ 20 → 50 → 80 → 90 → Reaches end, wrapsto start: 100 → 5

Worked Example: Daraz’s Order Fulfillment Queue

  • Daraz uses FCFS for order processing: orders are fulfilled in the order they arrive.
  • However, for warehouse inventory management, SSTF is used to minimize the movement of robotic arms (similar to disk scheduling).
  • If a warehouse uses SCAN, the robotic arm would move in one direction, reducing back-and-forth trips.

3. System Calls and Kernel Interaction

System calls are the only way user programs interact with the OS kernel. They enable privileged operations (e.g., file access, process creation).

3.1 System Call Process

  1. User Program invokes a system call (e.g., open("file.txt")).
  2. Trap Mechanism: The CPU switches from user mode to kernel mode (via a trap or interrupt).
  3. Kernel Executes: The OS performs the requested operation (e.g., opens the file).
  4. Return to User Mode: Control returns to the user program with results.

Mermaid Diagram: System Call Flow

sequenceDiagram
    participant User
    participant CPU
    participant Kernel

    User->>CPU: Invokes system call (e.g., open())
    CPU->>CPU: Trap to kernel mode
    CPU->>Kernel: Execute system call
    Kernel-->>CPU: Return result
    CPU->>User: Resume user program

Why Kernel Mode?

  • User programs cannot directly access hardware (e.g., disk, memory).
  • The kernel enforces security and stability by controlling access.

3.2 Common System Calls

Category System Call Description Example
Process Control fork(), exec(), exit() Create, replace, or terminate processes. Spawning a new shell (/bin/bash).
File Management open(), read(), write(), close() Manage files and I/O. Saving a file in Notepad++.
Device Management ioctl() Control special devices (e.g., printers, sensors). Configuring a Wi-Fi adapter.
Information getpid(), time() Retrieve system information. Checking process ID (getpid()).
Communication pipe(), socket() Enable inter-process communication. WhatsApp sending a message.
08162431System Call Number16 bitsParameters16 bits
Example of a system call instruction format (e.g., Linux x86 syscall)

Worked Example: eSewa Transaction

  1. User opens eSewa app → invokes open() system call to load the app.
  2. To send money, the app calls write() to send transaction data to the server.
  3. The server uses read() to process the request and returns a confirmation via write().

4. Real-World Applications

In the Real World

  1. NTC’s Network Routers:

    • Use multiprocessor architecture to handle millions of concurrent connections.
    • Layered model: Packets are processed at different layers (network → transport → application).
    • Interrupts: Each incoming packet triggers an interrupt to route it efficiently.
  2. eSewa’s Transaction Processing:

    • I/O buffering: Transaction data is buffered before being sent to the server.
    • Spooling: Multiple transactions are spooled to avoid overwhelming the server.
    • System calls: The app uses open(), read(), and write() to interact with the server.
  3. Pathao’s Ride Dispatching:

    • I/O scheduling: Ride requests are scheduled using SCAN-like algorithms to minimize driver travel time.
    • Interrupts: When a rider requests a ride, an interrupt triggers the dispatch system to assign the nearest driver.

Exam Tip

  1. Layered Models:

    • Always draw the 5-layer stack (user → system calls → kernel → drivers → hardware) in exams. Highlight how each layer abstracts the one below.
    • Example Question: "Explain how a user program accesses a disk file using the layered architecture."
  2. I/O Scheduling:

    • Compare FCFS, SSTF, SCAN, C-SCAN with a disk arm movement diagram.
    • Example Question: "A disk has requests at positions 5, 20, 50, 80, 90. Compare the seek time for FCFS and SCAN."
  3. Interrupts vs. DMA:

    • Know the flow of interrupt handling and when DMA is used (e.g., network cards).
    • Example Question: "Why does a network card use DMA instead of interrupts for every byte?"
  4. System Calls:

    • Memorize the common system calls and their categories (process, file, device).
    • Example Question: "How does a system call differ from a function call in user space?"
  5. Multiprocessor vs. Uniprocessor:

    • Compare SMP vs. AMP with a real-world example (e.g., NTC’s routers vs. a single-core laptop).
    • Example Question: "Why do cloud servers use multiprocessor systems?"
  6. Worked Examples:

    • Always tie theory to real-world scenarios (e.g., Daraz’s order queue, eSewa’s transactions).
    • Example Question: "Explain how buffering is used in a web browser when downloading a large file."

Final Note: Focus on visualizing concepts like layered models, interrupt handling, and I/O scheduling. Examiners love diagrams—draw them clearly! For system calls, list 3-4 examples with their categories. Good luck!

Based on the TU BIT syllabus for Operating System (BIT204), unit 10.

Discussion

Loading…