Search

Search papers and lessons

Operating System

Level 1 · 9 min read

An operating system is the program that sits between the hardware and every other program, deciding which program runs, where its data lives and how it reaches the disk and the screen.

On this page

  • Colour = topic mix
  • Systems
  • ReadReady to readNot yet

TL;DR

An operating system (OS) is the core program on a computer that manages the hardware and runs every other program. Applications ask it for things, such as reading a file or drawing a window, and it decides when and how those requests are carried out. It lets many programs share one processor, one memory and one disk without getting in each other's way.

Builds on: Process (Running Program) · Leads to: File System, Virtual Memory, Thread

Where it's used

Phones. iOS and Android are operating systems. When you open a camera app, the OS gives the app a share of the processor, a region of memory of its own and access to the camera, and it stops the app from reading your messages.

Servers. A web server in a data centre usually runs Linux. The OS runs thousands of requests per second by switching between the processes that handle them, and it restarts a process that crashes without taking the others down.

Cars and appliances. Many embedded devices, such as a washing machine controller, run a small OS that handles one fixed set of tasks in a strict time order. The same ideas apply, with fewer features.

How it works

Take one small job: a program called notes asks to read the file shopping.txt. The disk takes 9 milliseconds to answer. Here is what happens, one step at a time.

  1. The program asks. notes makes a system call, a formal request to the OS, asking for the contents of shopping.txt. It does not touch the disk itself, because it is not allowed to.
  2. The OS takes control. The processor switches into a privileged mode, and the OS code starts running. Check: the program's request is now in the OS's hands.
  3. The OS checks permission. It looks up the file in the File System and checks that notes is allowed to read it. If not, it returns an error and stops here.
  4. The OS asks the disk. It sends the read request to the disk driver, the piece of the OS that knows how to talk to that disk, and marks notes as waiting.
  5. The OS runs someone else. While the disk works, the processor is free. The scheduler, the part of the OS that picks what runs next, gives the processor to another process. Check: the processor is busy during the 9 milliseconds, even though notes is not.
  6. The disk finishes. The disk sends a signal called an interrupt. The OS pauses whatever is running, notes that the read is complete, and copies the data into memory that notes can see, the fast working storage described in Memory Hierarchy.
  7. The OS returns. notes wakes up and receives the text. From its point of view, the call simply returned the file.
sequenceDiagram
  participant App as notes
  participant OS as Operating system
  participant Disk
  App->>OS: read shopping.txt (system call)
  OS->>OS: check permission
  OS->>Disk: read request
  OS->>OS: run another process while waiting
  Disk-->>OS: done (interrupt)
  OS-->>App: data returned

The key idea is that the OS sits in the middle of every request and decides the order. Without it, each program would have to drive the disk, the screen and the memory itself, and two programs could overwrite each other's data. Preventing that kind of clash between running programs is the job of mutual exclusion, and it starts with the OS controlling who touches what.

The math

The math here is about how much of the processor a set of jobs can keep busy. Suppose a job needs cc milliseconds of processor time, then waits ww milliseconds for something like a disk, and then repeats.

Definition: Processor utilisation
The fraction of time the processor is busy. For nn identical jobs, each with cc milliseconds of computing and ww milliseconds of waiting, the utilisation is approximately given by the formula below
U≈min⁡(1,  n cc+w)U \approx \min\left(1, \; \frac{n \, c}{c + w}\right)

Here min⁡(1,x)\min(1, x) means the smaller of 1 and xx, so utilisation never goes above 100 percent.

Step 5 in symbols: one job alone

One job computes for cc milliseconds and then waits ww milliseconds. In each cycle of c+wc + w milliseconds, the processor works only for cc. So with one job, U=c/(c+w)U = c / (c + w). This matches the formula with n=1n = 1.

Step 5 in symbols: many jobs

With nn jobs, while one job waits, another can compute. The total computing per cycle is ncn c, and the cycle still lasts about c+wc + w if there is enough work to fill the gaps. The processor cannot exceed 100 percent, so the formula takes the minimum with 1.

The formula ignores the small cost of the OS switching between jobs, so real systems achieve a bit less.

Worked examples

Example
1. One job alone (tiny)
  1. A job computes for c=1c = 1 millisecond, then waits w=9w = 9 milliseconds for the disk.
  2. One cycle lasts 1+9=101 + 9 = 10 milliseconds.
  3. The processor works for 1 of those 10 milliseconds.
  4. So U=1/10=0.1U = 1 / 10 = 0.1, which is 10 percent.
  5. Interpretation: alone, this job leaves the processor idle 90 percent of the time.
Example
2. Three jobs sharing one processor (standard)
  1. The same job, with c=1c = 1 and w=9w = 9, now runs as n=3n = 3 copies.
  2. Compute nc=3×1=3n c = 3 \times 1 = 3 milliseconds of processor work per cycle.
  3. Divide by the cycle length c+w=10c + w = 10: 3/10=0.33 / 10 = 0.3.
  4. Since 0.3 is below 1, nothing is capped. So U=0.3U = 0.3, which is 30 percent.
  5. Interpretation: adding jobs fills the gaps while others wait. Three jobs triple the busy time of one.
Example
3. A busy server, realistically (applied)

A web server handles requests. Each request needs 2 milliseconds of processor time and waits 8 milliseconds for a database. Estimate the processor utilisation with 4 requests in flight at once, then with 12.

  1. Here c=2c = 2 and w=8w = 8, so c+w=10c + w = 10.
  2. With n=4n = 4: nc=8n c = 8, and 8/10=0.88 / 10 = 0.8. So U≈0.8U \approx 0.8, or 80 percent.
  3. With n=12n = 12: nc=24n c = 24, and 24/10=2.424 / 10 = 2.4.
  4. The formula gives min⁡(1,2.4)=1\min(1, 2.4) = 1, so U≈1U \approx 1, or 100 percent.
  5. Interpretation: 4 requests leave the processor fairly busy. 12 requests keep it saturated, so more requests now wait in a queue for the processor rather than being served faster.

Try it: 3 requests, each with 2 milliseconds of processor time and 6 milliseconds of waiting, run at once. What is the approximate utilisation? Answer: c+w=8c + w = 8, nc=6n c = 6, and 6/8=0.756 / 8 = 0.75, so about 75 percent.

Common mistakes and tradeoffs

It is tempting to think that the operating system is the same thing as the graphical screen, the desktop with icons. In fact the screen is one part of a typical OS. The kernel, which manages processes, memory and devices, runs with no screen at all. A headless server runs a full OS with no screen or keyboard attached.

It is tempting to think that running many programs at once means they all run at the same instant. In fact on one processor the OS gives each program short turns, switching quickly enough that they look simultaneous. The switching costs time, and it is why the utilisation formula above stays below 100 percent in practice.

It is tempting to think that the OS trusts every program. In fact the OS guards the boundary. Programs run in a less privileged mode and must ask through system calls. This protects the system from a buggy or malicious program, at the cost of a small delay on each request.

Tradeoffs: a general-purpose OS such as Linux or Windows does many things and can be slower to respond than a specialised real-time OS, which does one set of jobs predictably. A small embedded OS is simple and reliable but offers fewer features, so it suits devices with a fixed task.

History

Early computers in the 1950s ran one program at a time, and the operator loaded each job by hand. A small supervisor program, which later grew into an OS, began to load jobs automatically to save time. IBM's OS/360, introduced in 1964, was an influential attempt to run a whole family of machines with one OS. Unix began at Bell Labs in 1969, and its design of a small core with many small tools shaped Linux, macOS and the systems in phones today. Time-sharing, where many users shared one machine by taking turns, is what made the idea of switching between programs practical and popular.

Recap

  • An operating system manages the hardware and runs every other program.
  • Programs make requests through system calls, and the OS checks each one.
  • While a program waits for a slow device, the scheduler runs another process, which is how the processor stays busy.
  • Interrupts tell the OS when a device has finished, so it can resume the waiting program.
  • The fraction of processor time kept busy is roughly min⁡(1,nc/(c+w))\min(1, n c / (c + w)), which explains why adding jobs helps until the processor is saturated.
  • Next, read about File System for how the OS organises data on disk, Virtual Memory for how it gives each program its own memory, and Thread for how a single program can run several of its own tasks.

Check yourself

QuestionWhat is a system call, and why must a program use one to read a file?Show answer
A system call is a formal request from a program to the OS. Programs are not allowed to drive the disk directly, because the OS must check permissions and share the disk safely between programs.
QuestionA job computes for 2 milliseconds and waits 8 milliseconds. What is the processor utilisation with one job alone?Show answer
The cycle is 2+8=102 + 8 = 10 milliseconds, and the processor works for 2 of them, so U=2/10=0.2U = 2 / 10 = 0.2, or 20 percent.
QuestionWhy does the OS run another process while a program waits for the disk?Show answer
The processor would otherwise sit idle during the wait. Giving it to another process uses that time, which raises utilisation and lets the whole system finish more work.
QuestionA program is in the middle of a read, and the disk sends an interrupt. What does the OS do?Show answer
It pauses whatever is currently running, records that the read is finished, copies the data into the waiting program's memory, and then lets that program resume.
QuestionFive jobs each compute for 1 millisecond and wait 4 milliseconds. Estimate the processor utilisation, and say whether the processor is saturated.Show answer
The cycle is 1+4=51 + 4 = 5 milliseconds, and nc=5n c = 5, so 5/5=15 / 5 = 1. The utilisation is about 100 percent, so the processor is saturated. Adding more jobs would only lengthen queues.

Sources