1. Home
  2. Explore
  3. Computer Science
  4. Operating System Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Operating System Basics

Operating Systems: Comprehensive Study Notes

The Role of an Operating System

An operating system (OS) is the behind-the-scenes software that manages a computer's hardware and resources, enabling the user to interact with the machine without needing to understand low-level details. It acts as a "command and control structure" — analogous to the "Numbskulls" in the comic The Beano, where tiny workers manage cognitive tasks inside the brain. The OS handles scheduling, memory management, input/output, security, and provides a consistent interface between the user and the hardware.

Pre-Operating Systems: The Open Shop Era

Early computers had no operating system at all. In the open shop model (1950s), the computer designer, programmer, and user were often the same person. Programming involved sitting at a console and flicking switches to run a program. There were no high-level languages, no user-friendly interfaces, and the programmer had sole use of the machine during the session.

As computers became more popular (e.g., the IBM 701), demand grew. One approach was to divide usage into 15-minute slots. However, much of that time was spent setting up tapes and handling output — in one study, only 5 minutes of productive run time remained. The IBM 701 also had a mean time between failures of 3.5 hours, making reliability a major issue.

Historical Timeline of Operating Systems

The history of operating systems reflects a shift in resource demand. Early systems aimed to share one expensive computer among many users (many users → one machine). Modern systems, where hardware costs are negligible, try to give one user access to many machines (one user → many machines). The following phases capture key milestones:

1950s: Open Shop

  • Direct user control of the machine; no OS.

1960s: Closed Shop / Batch Processing

  • The machine room is restricted to operators (high priests).

  • Users submit jobs on punched cards or paper tape.

  • Jobs are stacked and run in batch mode.

  • A scheduler decides the order of job execution — a non-trivial problem (e.g., longest job first vs. shortest job first).

1960s–1970s: Multiprogramming and Time-Sharing

  • The OS gives the impression of running multiple jobs simultaneously.

  • Multiprogramming keeps the CPU busy while waiting for I/O.

  • Time-sharing extends this to multiple users, each feeling they have exclusive access.

1970s–1980s: Concurrent Programming

  • Focus on managing multiple processes that run at the same time.

  • Many ideas were theoretical but later influenced cloud computing.

1980s–1990s: Personal Computing and Graphical User Interfaces (GUIs)

  • Not covered in detail here (separate lectures exist).

2000s–Present: Distributed Systems and the Cloud

  • The OS extends across networked machines, managing resources as a single system.

Context Switching

The core mechanism behind multiprogramming is the context switch. When a running process (Process 1) needs to wait for I/O (e.g., a slow printer or disk), the CPU can switch to another process (Process 2) instead of idling.

How Context Switching Works

  1. An interrupt signal tells the CPU to pause the current process.

  2. The state of Process 1 is saved into a Program Control Block (PCB).
    The PCB typically holds the program counter (where it is in code), CPU registers, and local memory.

  3. The saved state of Process 2 is loaded from its PCB.

  4. Control transfers to Process 2.

  5. When Process 1 is ready again, the reverse happens: its PCB is restored, and execution continues.

The time required for this switch is called process switch latency (Δt\Delta tΔt). Designers aim to minimize this by:

  • Hardware-level optimizations for fast PCB save/restore.

  • Software-level minimization of the amount of state saved.

Lightweight vs. Heavyweight Processes

  • Heavyweight process (traditional process): requires saving a large context (entire address space, resources).

  • Lightweight process (thread): a sub-process that shares the address space of its parent, needing to save less state — faster context switches.

A Real-World Analogy

Imagine knitting (Process 1), then switching to cooking (Process 2), then a baby crying (high-priority interrupt). Each task's materials (knitting, ingredients, baby bottle) are placed on a stack. Switching between them is like popping and pushing items on the stack — the computer's memory stack stores the state of each interrupted task.

Interrupt Priority

Not all interrupts are equal. The interrupt processor checks the priority of each request. A high-priority interrupt (e.g., disk I/O needs immediate service) may preempt even important user tasks; a low-priority interrupt (e.g., a scheduled software signal) can wait.

Scheduling and Software Interrupts

Beyond hardware interrupts, the OS's scheduler runs periodically (e.g., every 20 milliseconds) to decide which process runs next. It generates a software interrupt to trigger a context switch, giving each user the illusion of continuous, dedicated service.

Protected Instructions and Privilege Levels

Modern operating systems enforce a distinction between user mode and supervisor mode (or kernel mode). Some CPU instructions — protected instructions — can only be executed in supervisor mode. This prevents a user process from:

  • Accessing hardware directly (which could corrupt other processes).

  • Switching tasks or modifying the memory of other processes.

  • Changing scheduling parameters.

Without protection, computer scientists could (and did) adjust scheduling priorities to give their own jobs more CPU time — a trick that exploited the lack of privilege separation on older systems (e.g., the Z80 processor). Today, every major OS implements a privilege hierarchy with super users (administrators) and normal users.

Virtual Memory

Virtual memory is a technique that gives each process the illusion of having its own large, contiguous block of memory, while actual physical memory may be fragmented or partially stored elsewhere.

Paging

  • Memory is divided into fixed-size blocks called pages (sometimes segments).

  • A page table maps virtual page addresses to physical memory addresses.

  • If a page is not in physical memory (RAM), it resides on a backing store (typically a disk or SSD).

When a program requests a page that is not in RAM, a page fault occurs:

  1. The system detects the missing page.

  2. It loads the page from the backing store into RAM (often replacing a less-used page).

  3. During this time, the CPU switches to another process to avoid idling.

  4. Once the page is ready, execution resumes.

Cache

A cache is an additional fast memory layer that speeds up access to frequently used data from slower storage (disk or even RAM). For example, a CPU cache sits between the processor and main memory; a disk cache buffers recently read disk blocks.

Thrashing

If the system spends more time swapping pages in and out than doing actual work, it enters a state called thrashing. This was a notorious problem on older systems with insufficient RAM, such as the VAX/VMS systems, where paging could dominate performance.

Unix: A Foundational Operating System

Unix was developed in 1969 by Ken Thompson and Dennis Ritchie at Bell Labs. Their goal was to reverse the trend of overly complex operating systems and create a small, simple system around which a community could form. Key design principles:

  • Kernel: the core of the OS, handling process scheduling, memory management, I/O, and communication.

  • Shell: the command interpreter that communicates between the user and the system.

  • Utilities: a collection of programs (e.g., file editors, sort, plot) that perform specific tasks.

The Unix philosophy emphasizes decomposing complex tasks into simple subtasks that can be combined using standard interfaces.

Modern OS Structure

A typical OS kernel is organized as shown in the following layers:

Layer

Description

Hardware

CPU, memory, disks, peripherals

Device Drivers

Interface to specific hardware components

Kernel

Scheduler, memory manager, file system, networking stack, IPC

System Libraries

Provide standard functions (e.g., printf, file open)

User Processes

Application programs running in user mode

This layered structure provides standard interfaces — a programmer convenience that makes software portable across different hardware. Without such abstraction, a program written for one keyboard would not work with another.

Monolithic vs. Microkernel

  • Monolithic kernel: most OS services (scheduling, file system, networking, drivers) run in supervisor mode (kernel space). This allows fast internal communication, but results in a large, hard-to-verify codebase.

  • Microkernel: only the absolute minimum services run in kernel mode — typically process management and inter-process communication (IPC). Other services (file systems, drivers) run as user-space processes. This follows the principle of least privilege.

A notable microkernel is L4; its high-assurance version seL4 has been formally verified — mathematically proven to behave exactly as specified. This is critical for embedded systems, routers, and security-sensitive applications where the OS must be trusted.

Android: Linux as a Microkernel

Android phones run a Linux kernel (a Unix-like system). However, Android applications are written in Java and executed via a virtual machine or just-in-time compilation (JIT), not linked statically to the kernel. This avoids the DLL hell problem of mismatched dynamic libraries common in Windows.

Scheduling

The scheduler determines which process gets the CPU next and for how long. A common algorithm (used in Solaris, a Unix variant) works as follows:

  1. Time slicing: the CPU time is divided into fixed-length slots (e.g., 20 ms).

  2. Each thread is assigned a priority (default or user-adjustable).

  3. High-priority threads run first. Once they use their time slice, their priority is decreased (they go to the back of the queue).

  4. The process repeats: highest priority ready thread runs next.

Real-Time Scheduling

Real-time does not simply mean "fast." A real-time task must complete within a fixed deadline. The scheduler must guarantee that high-priority real-time tasks meet their timing constraints. The plate-spinning analogy: each plate (task) must be "twisted" (serviced) before it falls (misses its deadline).

Inter-Process Communication and Race Conditions

When multiple processes run concurrently, they may need to share data. Without proper coordination, race conditions occur.

Example: Incrementing a Shared Variable

Consider two processes that both read a variable X (initially 0), increment it, and write it back. If they interleave as follows:

  • Process 1 reads X = 0

  • Process 2 reads X = 0

  • Process 1 writes X = 1

  • Process 2 writes X = 1

The result is 1 instead of the expected 2 – a race condition because the second read overlapped the first write.

Priority Inversion and Deadlock

A more complex problem is priority inversion, famously encountered by a Mars rover mission. A low-priority task acquires a lock on a resource, then gets preempted. A high-priority task that needs the same resource cannot proceed because the lock is held. The OS may then run a medium-priority task, further delaying the high-priority task. This can lead to deadlock (tasks waiting indefinitely).

Preventing these issues requires careful design of synchronization primitives (e.g., locks, semaphores, mutexes) and scheduling policies.

Cloud Computing

The cloud shifts computing from physical machines in the user's presence to remote data centers. Advantages include:

  • Capital savings: renting rather than buying hardware.

  • Location advantages: data centers can be built where electricity is cheap (e.g., Iceland) and physical security is strong.

  • Scalability: resources can be added or removed on demand.

Cloud Service Models

  • Software as a Service (SaaS): the provider runs the application (e.g., Google Docs, Office 365) and the user accesses it via a browser.

  • Platform as a Service (PaaS): the provider offers a development platform (e.g., AWS Elastic Beanstalk, Azure App Services) that includes databases, authentication, and other services.

  • Infrastructure as a Service (IaaS): virtual machines (VMs) are provided, allowing the user to install their own OS and applications. The underlying hardware is managed by a hypervisor (a VM manager).

Hypervisors and Virtual Machines

A hypervisor is an OS of operating systems — it runs directly on hardware and manages multiple virtual machines, each with its own guest OS (e.g., Linux, Windows). The hypervisor controls resource allocation and isolation. Linux (or Unix) is the most common choice for hypervisor platforms.

Modern Operating System Market Share

Looking at personal devices (excluding servers), the OS landscape has become remarkably homogenous:

  • Windows: dominant on desktops/laptops, derived from OS/2, not DOS.

  • Unix-like systems:

    • macOS: based on a microkernel/hybrid system (XNU, derived from NeXTSTEP/Unix).

    • iOS, watchOS, tvOS: all Unix-based.

    • Android: the largest single share — uses a Linux kernel.

  • Linux itself runs the vast majority of servers, cloud instances, and embedded devices.

The winners (Unix/Linux and Windows) were the creation of a tiny handful of individuals — highlighting the immense impact of foundational OS design.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent