Operating systems: resources, abstractions and services
How an operating system shares one CPU and one disk between programs, hides the hardware behind simple requests and refuses requests across owners. OS foundations, architecture and protection · Operating systems.
Two programs are running on one shared laptop. One is ana's editor, which saved report.txt a minute ago. The other is a script that ben started. Nothing in the hardware stops that script from asking the disk for report.txt, and nothing stops it from keeping the CPU for ever. How can the OS prevent either, and what does sharing one machine require?
What the hardware will and will not do
In our model a disk answers any request that reaches it: ask for a block and it returns the data, whoever asked. A CPU runs whatever instructions it is given for as long as it is given them. The hardware has no idea which user owns a file or whose turn it is. Something else has to hold those facts and make the rules stick. That something is the operating system (OS). It does three jobs: it gives programs a simple way to use the hardware, it shares the hardware out in turns, and it keeps programs apart. Each job is shown below on a small example before it gets a name.
One request, start to finish
The editor wants to open report.txt, which ana owns. In this model the editor cannot talk to the disk itself. The CPU has two modes. In user mode, where every ordinary program runs, an instruction that tries to drive a device directly is refused by the CPU. Only code running in kernel mode may do that. The kernel is the privileged core of the operating system: the code that runs in kernel mode. So the editor makes a system call, a request for the kernel to act on its behalf. It calls the C library function open("report.txt"), and that call enters the kernel. The next lesson shows how.
This is a simplified model: the kernel already has an ownership ledger, a table that lists each file and the user who owns it. A granted request contacts the disk; a refused one does not. Real file systems may need to read metadata to resolve a path or check permissions, and cached data may avoid a disk read. The editor runs as ana, and the model kernel works through the request in order:
- The CPU is now in kernel mode, still working for the editor.
- The ledger says
report.txtbelongs to ana and the editor runs as ana, so permission is granted; the file opens only after the lookup succeeds. - The file system, a part of the kernel, requests the directory entry for
report.txtfrom the disk. - The disk returns the entry, and the file system resolves where the file's bytes live, say blocks 812 to 815. The editor never sees those numbers. The kernel returns success to user mode: the file is open for the editor.
One request from a program to the disk and back
Read the steps as text
- Editor and script share one CPU. Editor holds it, in user mode.
- Editor calls open("report.txt"). Editor's system call enters the kernel and the CPU switches to kernel mode (1 boundary crossing).
- The ledger says report.txt belongs to user ana; editor runs as ana: granted.
- The kernel hands the request to the file system, which runs inside the kernel: an ordinary call (no boundary crossed). The file system sends the request to the disk in the hardware lane.
- The disk hands the result back to the file system. The file system hands the result back to the kernel.
- The kernel returns to user mode (1 boundary crossing): open succeeded.
Each switch between user mode and kernel mode is a crossing. This request needed two: in and out. The call from the kernel to the file system cost nothing because both run on the kernel's side of the line.
The editor wrote a name and got an open file back. It never handled blocks, controller commands or disk addresses. A simple interface that hides a lower-level mechanism is an abstraction; here the name stands for blocks the program never sees. Real systems also check permission bits, not only the owner; the note at the end of the lesson says how.
Two programs, one CPU
Now suppose the editor and the script both want to compute. One CPU runs one program at a time. If the editor ran until it chose to stop, a program stuck in an endless loop would freeze the machine. So the kernel hands out turns, and it uses a timer to do it. A timer is a hardware device that raises an interrupt signal at regular intervals; say every 10 milliseconds. When the signal arrives the CPU pauses the running program, saves where it was, and enters the kernel. The kernel picks the other program and returns to user mode inside it. The script starts at its first instruction.
Ten milliseconds later the timer fires again and the editor gets its turn back.
Two programs, one CPU
Read the steps as text
- Editor and script share one CPU. Editor holds it, in user mode.
- Editor runs add r1 r2. Next up: mul r1 r3.
- The timer raises its interrupt line. At the next instruction boundary the CPU saves editor's place and enters the kernel (1 boundary crossing).
- The kernel picks script and gives it the CPU. The kernel returns to user mode (1 boundary crossing). Script starts at load r4.
- Script runs load r4. Next up: add r4 r5.
- The timer raises its interrupt line. At the next instruction boundary the CPU saves script's place and enters the kernel (1 boundary crossing).
- The kernel picks editor and gives it the CPU. The kernel returns to user mode (1 boundary crossing). Editor resumes at mul r1 r3, where it left off.
- Editor runs mul r1 r3. Next up: store r1.
Look at the editor's progress. It ran one instruction, lost the CPU, and when it got the CPU back its next instruction was the second one, not the first. Its computation resumes from the saved state, though it can observe that time has passed. With 10 ms turns and two programs, each one holds the CPU for 10 of every 20 milliseconds, half the time, yet each behaves as if it owned a processor. Sharing a resource among competing programs by giving out turns or portions is multiplexing.
A refusal
Now ben types cat report.txt to print ana's file. The path is the same: the library call, the entry into the kernel, the ledger check. This time the ledger says the owner is ana and cat runs as ben, so the request is refused. In this model the kernel never contacts the disk. It returns to cat with an error; on Linux, open returns -1 and sets errno to EACCES, which prints as "Permission denied".
Ben runs cat on ana's file
Read the steps as text
- Editor and cat share one CPU. Cat holds it, in user mode.
- Cat calls open("report.txt"). Cat's system call enters the kernel and the CPU switches to kernel mode (1 boundary crossing).
- The ledger says report.txt belongs to user ana; cat runs as ben: refused. The file and its owner are unchanged.
- The kernel returns to user mode (1 boundary crossing): open("report.txt") returns -1 with errno EACCES (permission denied).
In this model the refused request leaves report.txt and the disk untouched. This illustrates isolation: the kernel mediates access between users. Here cat cannot skip the check by driving the disk directly.
The kernel and the rest of the OS
cat shows where the kernel ends. It ships with the operating system and ben would call it part of the OS, yet it is an ordinary program: it ran in user mode and went through the same ledger. The same holds for the open function from the library, a command such as ls that asks the kernel to read a directory, and background services. None of them is the kernel. The kernel is only the part that runs in kernel mode and makes the check.
Where the line falls varies between designs. Some systems run the file system as a user-mode service instead of inside the kernel. The next lesson compares those designs.
Recap
- In this model the OS hides blocks behind names (abstraction), gives 10 ms turns on a timer (multiplexing) and refuses a file request when the ledger names a different owner (isolation).
- The model uses two CPU modes: a program requests file access by a system call, while a timer interrupt also enters the kernel. The file request took two crossings.
- The kernel is the privileged core; the library, tools such as
catand services around it run in user mode and ask the kernel like any other program.
Users, permission bits, booting and other kinds of OS
On Linux and Windows each user has an identity, a user ID, and every process carries the ID of the user who started it. A file records the owning user, a group, and permission bits that say whether the owner, the group and everyone else may read, write or run it. The kernel compares the caller's user ID with those fields. That is why the ledger above names users: two programs started by the same user pass each other's check unless the permission bits or a sandbox stop them. The ledger is a simplified model of this check, and the next protection lesson covers users and permissions.
The kernel does not start itself. When a laptop powers on, firmware runs first and finds a boot loader on storage. The boot loader copies the kernel into memory and starts it. The kernel sets up memory and devices and then starts the first user program (process ID 1 on Linux), which starts the remaining services. Each step hands responsibility to the next.
Operating systems are built for different needs. A desktop system such as Linux, Windows or macOS favours sharing among interactive programs. A real-time OS, such as one in a car's brake controller, promises a bounded response time, for example reacting to a sensor within 1 millisecond. Real-time means the bound is guaranteed, not that the system is fast. Both kinds still manage shared resources and keep programs apart.
Continue to Kernel architecture and service entry.