Operating Systems chapters (5)
  1. 01OS Structures & System Calls
  2. 02Process & Thread Management
  3. 03CPU Scheduling Algorithms
  4. 04Process Synchronization & Deadlocks
  5. 05Memory Management & Virtual Memory

Chapter 01 · OS

OS Structures & System Calls

4 topics · 1 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

An operating system manages hardware and gives programs a safe, uniform interface.

This chapter covers OS roles, the user/kernel split with dual-mode protection, system calls, monolithic vs microkernel designs, and how interrupts and traps transfer control into the kernel.

Every OS paper opens here.

GATE regularly asks which instructions are privileged, what happens on a system call, and how microkernels differ from monolithic kernels.

Linux/Windows internals, container isolation, device drivers, writing safe system software, and debugging syscall-heavy programs with tools like strace.

1

OS Overview & Types

An operating system is system software that manages hardware resources (CPU, memory, storage, I/O devices), provides abstractions (processes, files, virtual memory) and enforces protection between users and programs.

In short: The OS is the manager that shares the machine fairly and hides messy hardware details from programs.

Think about it

Without an OS each application would need its own drivers and could corrupt others’ memory. Two apps can run at the same time on one laptop without overwriting each other’s memory because the OS isolates them.

Two classic views

  • 1resource manager - allocate CPU time, memory and devices among competing programs;
  • 2extended machine - present a simpler interface such as files instead of disk blocks.

Core functions: process management, memory management, file system, I/O management, protection/security, and user interface.

Goals differ by type: batch (throughput), time-sharing (response time), real-time (deadline guarantees), distributed (resource sharing).

Key points

  • Kernel: Core part of the OS that runs in privileged mode.
  • Multiprogramming: Several programs kept in memory so the CPU stays busy when one waits for I/O.
  • Time-sharing: Multiprogramming plus frequent switching so users see interactive response.
  • Spooling: Overlapping I/O of one job with computation of others using a disk buffer (e.g. print queue).
  • Types of OS: Batch (jobs grouped, no interaction), Multiprogrammed (several jobs in memory), Time-sharing/multitasking (quantum-based, interactive), Real-time (hard/soft deadlines), Distributed (many machines appear as one), Network OS, Embedded and Mobile OS (Android, iOS), Multiprocessor/parallel (symmetric vs asymmetric).
  • Degree of multiprogramming = number of processes in memory; CPU utilization = 1 − p^n for n processes each waiting for I/O a fraction p of the time (e.g. p = 0.8, n = 4 gives 1 − 0.4096 ≈ 59%).
  • Multiprogramming raises CPU utilization; time-sharing improves response time.
  • Real-time OS: hard (missing a deadline is failure) vs soft (late result degrades value).
2

User Mode vs Kernel Mode

Hardware provides a mode bit: kernel (supervisor) mode allows all instructions; user mode forbids privileged instructions such as I/O, setting the timer, modifying page tables, and disabling interrupts.

In short: Ordinary programs run in a restricted sandbox; only the kernel may touch hardware directly.

Compare two ideas side by side

Think about it

If any program could disable interrupts or write to the disk controller, one bug could crash or hijack the whole system.

Dual-mode operation protects the OS from user programs.

Attempting a privileged instruction in user mode raises a trap and the OS usually terminates the process.

A timer interrupt ensures the OS regains the CPU even if a user program loops forever.

Memory protection (base/limit or paging) prevents a process from reading kernel or other process memory.

Switching user→kernel happens only through controlled entry points: system call, interrupt or exception, never by a user program simply setting the mode bit.

Key points

  • Mode bit: Hardware flag: 0 = kernel, 1 = user (convention varies).
  • Privileged instruction: Instruction that executes only in kernel mode.
  • Trap: Synchronous software-generated transfer into the kernel.
  • Privileged: set timer, clear memory, disable interrupts, I/O instructions, change mode bit, modify memory-management registers.
  • NOT privileged: read the clock, issue a system call (trap) instruction, normal arithmetic and load/store in own address space.
3

System Calls & fork()

A system call is the programmatic interface through which a user program requests a service from the kernel. It executes a trap instruction that switches to kernel mode and jumps to a handler selected by a system-call number.

In short: It is the polite way for a program to say: kernel, please open this file or create this process for me.

Think about it

System calls are the only controlled gateway between untrusted user code and privileged kernel services.

Typical flow

the program calls a library wrapper (e.

g.

read in libc) → arguments and the call number are placed in registers or on the stack → trap instruction → kernel looks up the number in the system call table → kernel validates arguments and does the work → result placed in a register → return to user mode.

Parameters can be passed in registers, in a memory block (address in a register), or on the stack.

Categories

process control (fork, exec, exit, wait), file management (open, read, write, close), device management (ioctl), information maintenance (getpid, time), communication (pipe, socket, shmget), protection (chmod).

An API such as printf is a library function that may eventually call the write system call; a library call is not always a system call.

Key points

  • System call table: Kernel array mapping call numbers to handler functions.
  • Wrapper / API: Library function (libc) that prepares arguments and issues the trap.
  • Context of call: Register state saved so the user program resumes after the call.
  • fork() is the system call that creates a new process: it returns 0 to the child, the child PID (> 0) to the parent and -1 on failure. Child gets a copy of the parent address space (copy-on-write) and the same open files.
  • Counting rule: n unconditional fork() calls in sequence create 2^n processes in total (2^n − 1 new children). Example: fork(); fork(); printf("A"); prints A four times.
  • Mode switch cost: a system call needs a user→kernel trap, argument validation and return, so it is much cheaper than a context switch but costlier than a normal function call.
  • Output example: int main(){ if(fork()==0) printf("C "); else printf("P "); } prints "P C" or "C P" (order not guaranteed).

Example

Code
C example with system calls:
#include <unistd.h>
#include <fcntl.h>

int main(void) {
    int fd = open("data.txt", O_RDONLY);   // system call (via wrapper)
    char buf[16];
    ssize_t n = read(fd, buf, sizeof buf); // system call
    write(1, buf, n);                      // write to stdout (fd 1)
    close(fd);                             // system call
    return 0;
}
/* strace ./a.out shows open, read, write, close syscalls */
4

Monolithic vs Microkernel

Monolithic: the whole OS runs as a single large program in kernel space. Layered: OS divided into layers, each using only the layer below. Microkernel: only minimal services (IPC, basic scheduling, low-level memory) in the kernel; file systems, drivers and networking run as user-space servers.

In short: Monolithic = everything in the kernel (fast, riskier). Microkernel = tiny kernel, services outside (safer, more message passing).

Think about it

The design decides performance, reliability and how easily the OS can be extended.

Monolithic kernels (classic Unix, Linux) make calls between components as plain function calls, giving speed, but a faulty driver can crash the whole system.

Linux mitigates size with loadable kernel modules.

Layered design gives modularity and easy debugging but is hard to define layers cleanly and may be slower.

A microkernel (Mach, QNX, MINIX) gains reliability, security and extensibility since a crashed driver server can be restarted, but communication between servers uses message passing through the kernel, adding overhead.

Hybrid kernels (Windows NT, macOS XNU) mix both ideas.

Key points

  • IPC: Inter-process communication, typically message passing in microkernels.
  • Loadable module: Kernel code added or removed at run time without rebooting.
  • Hybrid kernel: Microkernel-like structure with many services still in kernel space for speed.
  • Microkernel: + easier to extend and port, + more secure and reliable, - performance overhead from user↔kernel message copies.
  • Monolithic: + speed, - larger attack surface and hard to maintain.
5

Interrupts, traps and exceptions

An interrupt is an asynchronous signal from hardware (I/O completion, timer) that makes the CPU save its state and run an interrupt service routine (ISR). A trap or exception is synchronous and caused by the executing instruction (system call, divide by zero, page fault, illegal instruction).

In short: Interrupt = a device taps the CPU on the shoulder. Trap = the running program itself causes the switch.

Try → catch errors safely

Think about it

Interrupts avoid wasteful polling and let the OS keep control of the CPU through the timer.

Steps on interrupt

finish current instruction → save PC/flags (and mode) → look up the ISR address in the interrupt vector table using the interrupt number → run ISR in kernel mode → restore state and resume.

Interrupts can be masked (disabled) by the kernel for critical sections, except the non-maskable interrupt (NMI) used for severe hardware errors.

Priorities let urgent interrupts preempt lower ones.

A page fault is an exception that the OS handles and then re-executes the faulting instruction.

I/O methods: programmed I/O (polling), interrupt-driven I/O, and DMA where the controller transfers a block to memory and interrupts once at the end.

Key points

  • ISR: Interrupt service routine that handles a specific interrupt.
  • Interrupt vector: Table of ISR addresses indexed by interrupt number.
  • DMA: Direct memory access: device moves data to/from memory without per-byte CPU work.
  • NMI: Non-maskable interrupt that cannot be disabled.
  • Synchronous: trap/exception (same instruction always triggers). Asynchronous: hardware interrupt (can occur between any two instructions).

Solved examples

Example 01 · Basic

Which of these must run in kernel mode: (a) read system clock, (b) disable interrupts, (c) execute a trap instruction, (d) set the timer register?

Given Data:
    Required To Find:

    Identify privileged instructions

    Formula Applied:—
    Step-by-Step Solution:
    Step 1: Privileged instructions change global machine state or access hardware directly.
    Step 2: (a) Reading the clock is harmless, so it is allowed in user mode.
    Step 3: (b) Disabling interrupts could stop the OS from regaining control, so it is privileged.
    Step 4: (c) The trap instruction is how user code enters the kernel, so it must be allowed in user mode.
    Step 5: (d) Setting the timer controls CPU preemption, so it is privileged.
    Final Answer: Only (b) and (d) are privileged.

    Approach note: Ask: could this let a user program take control of the machine? If yes, it is privileged.

    Example 02 · Intermediate

    Describe the sequence of events when a user program calls read(fd, buf, n).

    Given Data:
      Required To Find:

      Ordered steps of a system call

      Formula Applied:—
      Step-by-Step Solution:
      Step 1: The program calls the read wrapper in the C library (an ordinary function call in user mode).
      Step 2: The wrapper loads the system call number and arguments into registers, then executes a trap instruction.
      Step 3: The hardware switches to kernel mode and jumps through the trap vector to the system call dispatcher.
      Step 4: The dispatcher indexes the system call table with the number and calls the kernel read routine.
      Step 5: The kernel validates fd and buf, may block the process while the disk works, and copies data into buf.
      Step 6: The return value is stored in a register, the mode bit is reset to user mode, and execution resumes after the trap.
      Final Answer: User wrapper → trap → kernel dispatch via table → service executes → return value → back to user mode.

      Approach note: Key words for full marks: trap, mode switch, system call table, parameter passing, return to user mode.

      Example 03 · Basic

      A user program executes a divide-by-zero. Is this a system call? What mechanism brings the OS in?

      Given Data:
        Required To Find:

        Classification

        Formula Applied:—
        Step-by-Step Solution:
        Step 1: Divide-by-zero is a CPU exception/trap, not a voluntary system call
        Step 2: Hardware vectors to the kernel exception handler
        Step 3: OS may terminate the process or deliver a signal
        Final Answer: Not a system call; it is a hardware exception/trap handled by the kernel.

        Approach note: System calls are intentional; exceptions/interrupts can be involuntary.

        Example 04 · Intermediate

        Name two advantages of a monolithic kernel and one disadvantage versus a microkernel.

        Given Data:
          Required To Find:

          Trade-offs

          Formula Applied:—
          Step-by-Step Solution:
          Step 1: Monolithic: fast in-kernel communication (function calls)
          Step 2: Monolithic: mature drivers often live in-kernel for performance
          Step 3: Disadvantage: one buggy driver can crash the whole OS
          Final Answer: Pros: performance, simple in-kernel APIs. Con: large trusted base—driver bugs can panic the system.

          Approach note: Exams love: monolithic = speed; microkernel = isolation + IPC cost.

          Real-world applications

          OS

          Using OS Structures & System Calls in real projects

          Linux/Windows internals, container isolation, device drivers, writing safe system software, and debugging syscall-heavy programs with tools like strace.

          Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

          Common mistakes

          ❌ Myth: Every library function such as printf is a system call.

          Why this is wrong: printf formats text in user space and only eventually calls write, which is the system call.

          ✅ Verified Truth: A system call is a controlled entry into the kernel via a trap; library functions may or may not use one.

          ⚠️ Exam Tip: Differentiate API/library call from system call in definitions.

          ❌ Myth: A microkernel is always faster because it is smaller.

          Why this is wrong: Services outside the kernel communicate through message passing, which adds copying and context switches.

          ✅ Verified Truth: Microkernels trade some speed for reliability and modularity.

          ⚠️ Exam Tip: Quote the trade-off: reliability and security vs IPC overhead.

          Revision notes

          An operating system manages hardware and gives programs a safe, uniform interface. This chapter covers OS roles, the user/kernel split with dual-mode protection, system calls, monolithic vs microkernel designs, and how interrupts and traps transfer control into the kernel.

          High-Yield Quick Revision Points

          1. OS = resource manager + abstraction + protection
          2. Mode bit: privileged instructions only in kernel mode
          3. System call: wrapper → trap → table dispatch → return
          4. Monolithic fast but fragile; microkernel safe but IPC overhead
          5. Interrupt = async (hardware); trap/exception = sync (instruction)
          6. Timer interrupt guarantees OS regains the CPU

          Practice questions

          Question 1 · MCQ (GATE)

          Which instruction is NOT privileged?

          Question 2 · MCQ (GATE)

          A trap is best described as:

          Question 3 · MCQ (GATE / semester)

          Which is a key advantage of a microkernel over a monolithic kernel?

          Question 4 · MCQ (GATE)

          The mechanism that lets the OS regain CPU control from a looping user program is: