BIT204 Operating System

Operating SystemUnit 113 min read

Introduction to OS: Roles, Types, Kernels & System Calls

Unit 1 of Operating System: Explores the definition, functions, types, and architecture of operating systems, including kernels, system calls, and their real-world applications in apps like eSewa and NTC.

What is an Operating System (OS)?

An operating system (OS) is a software layer that manages hardware resources (CPU, memory, storage, I/O) and provides an interface between users/applications and the computer. It acts as a controller, ensuring efficient, fair, and secure execution of programs.

Key Functions of an OS

mindmap
  root((Operating System Functions))
    Hardware Abstraction
      [[CPU scheduling]]
      [[Memory management]]
      [[Device management]]
    User Interface
      [[Command-line (CLI)]]
      [[Graphical (GUI)]]
    Resource Allocation
      [[Process management]]
      [[File system]]
    Security & Protection
      [[Authentication]]
      [[Access control]]
    Error Handling
      [[Fault tolerance]]
      [[Recovery mechanisms]]

Why Do We Need an OS?

Without an OS, users would have to:

  • Write low-level code for every task.
  • Manually manage hardware (e.g., loading programs into RAM).
  • Handle errors and conflicts manually.

An OS automates these tasks, improving productivity, reliability, and usability.


Types of Operating Systems

Operating systems are classified based on usage, architecture, and design goals:

Type Description Example Advantages Disadvantages
Batch OS Processes jobs in groups without user interaction. Early mainframe systems (e.g., IBM OS) High throughput, low cost Poor response time, no interactivity
Time-Sharing OS Allows multiple users to share CPU time interactively. Unix, Linux Multitasking, responsiveness Higher resource usage
Real-Time OS (RTOS) Guarantees responses within strict time limits (e.g., milliseconds). VxWorks, FreeRTOS Predictable, critical applications Expensive, complex
Distributed OS Manages resources across multiple machines (networked). Google’s Borg, Kubernetes Scalability, fault tolerance Complex coordination
Embedded OS Optimized for dedicated hardware (e.g., microcontrollers). FreeRTOS, Zephyr Low power, compact Limited functionality
Mobile OS Designed for smartphones/tablets with touch interfaces. Android, iOS User-friendly, app ecosystem Battery drain, security risks

Layers of an Operating System

An OS is structured in layers (or hierarchies) to modularize functions. The classic layered model (from Dijkstra’s design) separates concerns:

User Applications (e.g.,Chrome, Notepad)API (e.g., Win32, POSIX)System Calls (e.g., `fork()`,`read()`)Kernel (Process/DeviceManagement)Hardware (CPU, RAM, Disk)
Classic layered OS architecture showing the kernel as the core layer managing hardware and services.
figure:

┌───────────────────────────────────────────────────────┐ │ User Applications │ └───────────────────────────────────────────────────────┘ ┌───────────────────────────────────────────────────────┐ │ Application Programming Interface │ │ (API) │ └───────────────────────────────────────────────────────┘ ┌───────────────────────────────────────────────────────┐ │ System Calls │ └───────────────────────────────────────────────────────┘ ┌───────────────────────────────────────────────────────┐ │ Kernel (Core OS) │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Process │ │ Memory │ │ File │ │ │ │ Management │ │ Management │ │ System │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Device │ │ Security │ │ Interrupt │ │ │ │ Management │ │ & Protection│ │ Handling │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ └───────────────────────────────────────────────────────┘ ┌───────────────────────────────────────────────────────┐ │ Hardware (CPU, Memory, I/O) │ └───────────────────────────────────────────────────────┘


  • User Applications: Programs like browsers, games, or eSewa.
  • API: High-level functions (e.g., open(), read() in C) that hide OS details.
  • System Calls: Low-level requests (e.g., fork(), exec()) to the kernel.
  • Kernel: The heart of the OS, managing hardware and core services.
  • Hardware: Physical components (CPU, RAM, disk) controlled by the kernel.

Why Layers?

  • Modularity: Changes in one layer (e.g., file system) don’t crash the whole OS.
  • Security: User apps can’t directly access hardware.
  • Efficiency: The kernel optimizes critical tasks (e.g., CPU scheduling).

The Kernel: Core of the OS

The kernel is the smallest, most trusted part of the OS that runs in privileged mode (ring 0 in x86). It handles:

  • Process/thread management.
  • Memory allocation.
  • Device drivers.
  • Security enforcement.
classDiagram
    class Kernel {
      +processManagement()
      +memoryManagement()
      +deviceDrivers()
      +securityEnforcement()
    }
    class MonolithicKernel {
      --Kernel
      +allServicesInKernelSpace()
    }
    class Microkernel {
      --Kernel
      +minimalCoreServices()
      +loadExtensions()
    }
    Kernel <|-- MonolithicKernel
    Kernel <|-- Microkernel
Kernel types: Monolithic (all services in kernel) vs. Microkernel (minimal core with extensions).

Types of Kernels

Type Description Example Pros Cons
Monolithic All OS functions run in kernel space (e.g., Linux kernel). Linux, Windows NT Fast, simple Hard to maintain, security risks
Microkernel Only essential services (e.g., IPC, memory management) run in kernel space. QNX, Minix Modular, secure Slower, higher overhead
Hybrid Combines monolithic and microkernel approaches. macOS, Windows Balance of performance and safety Complex design

System Calls: Interface to the Kernel

System calls (syscalls) are programmed interfaces that allow user applications to request OS services (e.g., file I/O, process creation). They are traps (software interrupts) that switch from user mode to kernel mode.

sequenceDiagram
    participant App
    participant Syscall
    participant Kernel
    participant Hardware
    
    App->>Syscall: invoke `open("file.txt")`
    Syscall->>Kernel: Trap to kernel mode
    Kernel->>Hardware: Execute file I/O
    Hardware-->>Kernel: Data ready
    Kernel-->>Syscall: Return file descriptor
    Syscall-->>App: Return success
System call flow: Application → Syscall → Kernel → Hardware interaction.

How a System Call Works

  1. Application invokes a syscall (e.g., open("file.txt")).
  2. CPU triggers a software interrupt (e.g., int 0x80 in x86).
  3. Kernel handles the request (checks permissions, allocates resources).
  4. Result is returned to the application.
sequenceDiagram
    participant UserApp
    participant CPU
    participant Kernel
    participant Hardware

    UserApp->>CPU: Invoke syscall (e.g., open())
    CPU->>Kernel: Trigger interrupt (e.g., int 0x80)
    Kernel->>Hardware: Check permissions
    Hardware-->>Kernel: Grant/deny access
    Kernel-->>CPU: Return status (success/failure)
    CPU-->>UserApp: Return result

Common System Calls

Category System Call Purpose
Process Control fork(), exec(), exit() Create, run, terminate processes.
File Management open(), read(), write(), close() Handle files and I/O.
Device Management ioctl(), read(), write() Control hardware devices (e.g., printers).
Information getpid(), time() Retrieve system info (e.g., process ID).
Communication pipe(), socket() Enable inter-process communication.

Why Use System Calls Instead of Direct Hardware Access?

  • Security: Prevents apps from crashing the system.
  • Abstraction: Hides hardware details (e.g., file systems).
  • Efficiency: Kernel optimizes resource usage.

In the Real World

  1. eSewa (Nepal)

    • Idea Used: Process Management and File System
    • How?
      • When you initiate a payment, eSewa’s backend runs as a process managed by the OS (e.g., Linux).
      • Transactions are stored in a database file system (e.g., MySQL), where the OS handles disk I/O and concurrency.
    • Worked Example:
      • If 100 users request payments simultaneously, the OS’s scheduler allocates CPU time fairly, and the file system ensures transaction logs are written safely (even if the system crashes).
  2. NTC (Nepal Telecom) Call Routing

    • Idea Used: Real-Time OS (RTOS) and Kernel
    • How?
      • NTC’s switches run on RTOS to handle millions of calls per second with sub-millisecond latency.
      • The kernel manages processes for call setup, memory for call data, and device drivers for network cards.
    • Worked Example:
      • When you dial a number, the OS’s interrupt handler (part of the kernel) processes the call request immediately, routes it via the network stack, and allocates resources (e.g., memory for call audio) without delay.
  3. Daraz (E-Commerce Platform)

    • Idea Used: Memory Management and Virtualization
    • How?
      • Daraz’s servers use virtual memory to handle thousands of concurrent users. The OS maps logical addresses (e.g., http://daraz.com/product123) to physical memory efficiently.
      • If a user’s request exceeds available RAM, the OS swaps less-used data to disk (secondary storage).
    • Worked Example:
      • During a sale, Daraz’s OS ensures that popular product pages stay in RAM (cached), while less-demand pages are paged out to disk. This prevents crashes and keeps the site responsive.

Exam Tip: How This Unit is Tested

  1. Definitions & Concepts (20-30%)

    • Expect questions like:
      • "Define address space. Explain the limitations of a batch system."
      • "What is a kernel? Differentiate between monolithic and microkernel."
    • How to Answer:
      • Start with a clear definition.
      • Use examples (e.g., "Linux uses a monolithic kernel").
      • Mention advantages/disadvantages (e.g., "Microkernels are secure but slower").
  2. Types of OS (15-20%)

    • Compare time-sharing vs. real-time OS or batch vs. distributed OS.
    • How to Answer:
      • Use a table (like the one above) for clarity.
      • Highlight key differences (e.g., "Real-time OS has strict deadlines").
  3. System Calls & Kernel (20-25%)

    • Draw a flowchart of how a system call works (like the Mermaid diagram above).
    • Explain why kernel mode is privileged.
    • How to Answer:
      • Use step-by-step explanation with diagrams.
      • Mention real-world impact (e.g., "Without syscalls, apps couldn’t access files safely").
  4. Layered Model (10-15%)

    • Questions may ask: "Why is the OS designed in layers?"
    • How to Answer:
      • Explain modularity, security, and efficiency.
      • Relate to real apps (e.g., "eSewa’s backend relies on the OS’s layered design to handle transactions").
  5. Worked Examples (10-15%)

    • Solve problems like:
      • "A system has 2 processes and 3 identical resources. Each process needs a max of 2 resources. Is deadlock possible?"
    • How to Answer:
      • Draw a Resource Allocation Graph (RAG).
      • Apply Banker’s Algorithm (if needed) to check for safety.
      • Use logical reasoning (e.g., "If both processes hold 1 resource each, they’ll wait indefinitely").

Final Advice:

  • Memorize key terms (e.g., kernel, syscall, monolithic/microkernel).
  • Draw diagrams for system calls, layered models, and RAGs.
  • Relate to real apps (eSewa, NTC, Daraz) to understand practical use.
  • Practice past papers—focus on definitions, comparisons, and problem-solving.

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

Discussion

Loading…