The Big Picture: Four Jobs

Section 1 said the OS manages and simplifies. Now let's break that into four core jobs. Almost everything an OS does fits under one of them.

┌──────────────────────────────────────────────────────────┐
│                  THE OPERATING SYSTEM                    │
├────────────────┬──────────────┬────────────┬─────────────┤
│  1. SHARING    │ 2. HIDING    │ 3. PROTECT │ 4. JUGGLE   │
│  (allocation)  │ (abstraction)│ (security) │ (coordinate)│
├────────────────┴──────────────┴────────────┴─────────────┤
│   CPU      RAM      Disk      Network      Devices       │
└──────────────────────────────────────────────────────────┘

Think of a restaurant. The manager shares the tables and the kitchen, hides the kitchen mess behind a simple menu, protects guests from trouble, and coordinates many orders at once. The OS does the same for programs.

2.1 Resource Allocation (Sharing Hardware)

What it is: Resource allocation means deciding which program gets which part of the hardware, and for how long. A resource is anything limited: CPU time, memory, disk space, network speed.

Two ways to share:

WayWhat it meansExample
Time sharingPrograms take turns, each for a very short momentThe CPU: program A runs for a few milliseconds, then B, then C...
Space sharingThe resource is cut into pieces, each program gets its own piece at the same timeRAM and disk: A gets these pages, B gets those

Time sharing is the reason your PC seems to run everything at once. A CPU core runs one thread at a time, but it switches so fast (hundreds of times a second) that you cannot tell. Think of one chef cooking five dishes by stirring each pot for a few seconds in turn.

One CPU core, time-sliced:

time ─►  | A | B | C | A | B | C | A | B | C |

The OS must be fair and smart. It balances three goals that can fight each other:

GoalMeaning
FairnessNo program is starved forever
EfficiencyHardware is never left idle when there is work
PriorityImportant work (your mouse cursor, music) goes first

On Windows: Open Task Manager → Performance. You see CPU, Memory, Disk and Ethernet or Wi-Fi graphs. Each graph is a resource the OS is sharing out. The Details tab lets you right-click a process and set its priority (for example, Below normal). That is you asking the OS to change its sharing decision.

2.2 Abstraction (Hide Complexity)

What it is: Abstraction means hiding hard details behind a simple view. The OS builds a ladder of abstractions, one on top of another.

Hardware realityOS abstractionWhat the programmer sees
Magnetic spots or flash cellsFileopen("notes.txt")
Physical RAM chipsAddress space (virtual memory)"I have my own big, empty memory"
CPU coresProcess / thread"My program runs on its own"
Network card and radioSocket"Send these bytes to that address"
Keyboard, mouse, touchInput events"The user pressed A"
Graphics chipWindow / drawing surface"Draw here"

Why does this matter? Three reasons:

  1. Simplicity. A programmer writes read a file, not "send command 0x25 to the disk controller".
  2. Portability. The same program runs on any PC, whatever its hardware.
  3. Safety. The OS checks every request, so a program cannot do something silly directly to the hardware.

Example: "Open a web page" in layers.

Browser:        "Get me this web page"
   │
OS network:     splits data into packets, picks a connection
   │
Driver:         knows YOUR Wi-Fi chip's exact commands
   │
Hardware:       radio waves leave the antenna

Each layer only talks to the layer next to it. A layer can be replaced without the others noticing.

2.3 Security (Isolation and Protection)

What it is: The OS keeps programs and users apart and keeps the system safe. It must stop one program from spying on, damaging or crashing another, or the OS itself.

The OS uses these tools, each explained fully in Section 6:

ToolWhat it protects
Privilege rings (kernel mode and user mode)Only trusted OS code can touch hardware directly
Private address spacesA program cannot see another program's memory
Permissions on filesOnly allowed users can read or change a file
Quotas (limits)No program can use up all the resources

Remember Chapter 2: the CPU itself has privilege rings. The OS uses that hardware feature, so security is not only software. The CPU enforces it.

An everyday picture: a hotel. Guests (programs) get a room key (permission) that opens only their own room. The staff (the kernel) have a master key. Guests cannot make a copy of the master key.

2.4 Coordination (Many Apps at Once)

What it is: Coordination means making many programs live together smoothly. The OS gives them ways to wait for each other, share information, and not trip over one another.

What the OS coordinates:

NeedOS toolExample
Programs trade dataInter-process communication (IPC), such as pipes and shared memoryA browser sends a page to its own plug-in process
Two threads must not change the same data togetherLocks (also called mutexes)Two threads adding to one bank balance
A program must wait for slow hardwareBlocking and wake-upA program waits for the disk and sleeps meanwhile
EventsSignals and messages"The user closed the window"
Keep the order of disk workQueuesMany programs writing to one disk

Why locks matter: Imagine two cashiers both reading "balance = 100", each adding 50, and each writing back 150. The balance should be 200! A lock makes the second cashier wait until the first has finished. This kind of bug is called a race condition, and the OS gives programmers the tools to avoid it.

Waiting is clever. When a program asks for the disk, the OS does not make the CPU stare at the disk. It puts the program to sleep, runs something else, and wakes the program when the disk interrupts (Chapter 4, Section 3) to say "data ready". That one trick is the reason a PC stays responsive during a big file copy.

Summary

TopicRemember this
Resource allocationThe OS shares CPU, RAM, disk and network by time sharing and space sharing
AbstractionFiles, processes, virtual memory and sockets hide the real hardware
SecurityRings, private memory, permissions and quotas keep programs apart
CoordinationIPC, locks and sleep/wake let many programs cooperate
The patternShare, hide, protect, coordinate: the four jobs behind everything else in this chapter