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.
- The program asks.
notesmakes a system call, a formal request to the OS, asking for the contents ofshopping.txt. It does not touch the disk itself, because it is not allowed to. - 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.
- The OS checks permission. It looks up the file in the File System and checks that
notesis allowed to read it. If not, it returns an error and stops here. - 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
notesas waiting. - 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
notesis not. - 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
notescan see, the fast working storage described in Memory Hierarchy. - The OS returns.
noteswakes 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 milliseconds of processor time, then waits milliseconds for something like a disk, and then repeats.
Here means the smaller of 1 and , so utilisation never goes above 100 percent.
Step 5 in symbols: one job alone
One job computes for milliseconds and then waits milliseconds. In each cycle of milliseconds, the processor works only for . So with one job, . This matches the formula with .
Step 5 in symbols: many jobs
With jobs, while one job waits, another can compute. The total computing per cycle is , and the cycle still lasts about 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
- A job computes for millisecond, then waits milliseconds for the disk.
- One cycle lasts milliseconds.
- The processor works for 1 of those 10 milliseconds.
- So , which is 10 percent.
- Interpretation: alone, this job leaves the processor idle 90 percent of the time.
- The same job, with and , now runs as copies.
- Compute milliseconds of processor work per cycle.
- Divide by the cycle length : .
- Since 0.3 is below 1, nothing is capped. So , which is 30 percent.
- Interpretation: adding jobs fills the gaps while others wait. Three jobs triple the busy time of one.
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.
- Here and , so .
- With : , and . So , or 80 percent.
- With : , and .
- The formula gives , so , or 100 percent.
- 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: , , and , 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 , 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.