System Software
How the operating system shares the processor and memory, and how source code is checked and translated.
The OS makes the most of the hardware
The operating system shares the computer's resources between programs, so the hardware does as much work as possible.
An operating system (OS) decides which program uses each resource, and when. The resources are:
- processor time
- memory
- storage
- input/output devices.
| Problem | What happens |
|---|---|
| A program often has to wait for slow input or output, such as a disk read. | While it waits, the OS gives the processor to another program. So the processor is not left idle. |
| Devices need attention. | Devices send interrupts (signals that ask for attention). So the processor does not spend time checking them. |
| Several programs need memory. | The OS shares memory between programs, so several can be loaded at once. |
Remember:The result is more work from the same hardware.
The interface hides the hardware
The OS hides the hard details of the hardware. Users and programmers work with simple things like icons and files.
You click an icon, drag a file or type a command. You never deal with memory addresses, disk sectors or the signals a printer needs.
The OS hides these details:
- Its user interface, graphical or command line, turns your actions into work for the hardware.
- A device driver (a small program for one device) handles each device.
- Files and folders stand in for blocks on a disk.
Programs get the same help. The OS gives them standard routines. So a programmer can write "save this file" instead of driving the disk.
Processes and their three states
A process is always running, ready or blocked. Set events move it from one state to another.
A process is a program that is being executed.
Multitasking means the OS switches the processor between processes so quickly that they seem to run together. On one core, only one runs at any moment.
| State | Meaning |
|---|---|
| running | on the processor |
| ready | able to run, waiting in the ready queue |
| blocked | waiting for an event, such as I/O finishing |
| Move | Why it happens |
|---|---|
| running → ready | its time slice ends, or an interrupt lets a higher-priority process pre-empt it (take its place) |
| running → blocked | it asks for I/O |
| blocked → ready | the I/O finishes |
| ready → running | the scheduler picks it |
Watch out:A blocked process never goes straight to running.
Why schedule, and the four routines
Scheduling picks which ready process runs next. Each of the four routines picks in its own way.
Scheduling decides which ready process runs next. Its aims:
- keep the processor busy
- be fair
- respond quickly
- finish as many processes as possible.
| Routine | How it picks | Good and bad |
|---|---|---|
| First come first served (FCFS) | runs processes to completion, in arrival order | simple, but a long process holds up the rest |
| Shortest job first (SJF) | runs the shortest waiting process next | lowers the average waiting time; but lengths must be known first, and a long process may wait for ever (starvation) |
| Shortest remaining time (SRT) | the pre-emptive version of SJF: a new arrival with less time left takes over | — |
| Round robin (RR) | gives each process a fixed time slice in turn | fair, and no process starves |
Pre-emptive means a running process can be stopped before it finishes.
Working out a schedule by hand
Work out a schedule on a timeline. Then two formulas give each process's turnaround time and waiting time.
- Draw a timeline.
- Every time the processor becomes free, ask the routine its one question: which process next? For SRT, ask again every time a process arrives.
- Then work out two times for each process.
- turnaround time = finish time − arrival time
- waiting time = turnaround time − burst time (the processor time the process needs).
Exam:For round robin, keep a written ready queue and state your tie rule.
Here the tie rule is: a process that arrives at the moment another's slice ends joins the queue first.
Example:The working below is round robin with a time slice of 2. P1 arrives at 0 and finishes at 22, so its waiting time is 22 − 8 = 14.
Round robin, time slice 2
Process arrives burst
P1 0 8
P2 1 4
P3 2 9
P4 3 5
0-2 P1 2-4 P2 4-6 P3 6-8 P1 8-10 P4
10-12 P2 12-14 P3 14-16 P1 16-18 P4
18-20 P3 20-22 P1 22-23 P4 23-26 P3
finish turnaround waiting
P1 22 22 14
P2 12 11 7
P3 26 24 15
P4 23 20 15
average waiting time = 51 / 4 = 12.75The kernel handles interrupts
The kernel is the core of the OS. It handles every interrupt and then chooses which process runs next.
The kernel is the core of the OS. It stays in memory and controls the processor directly.
An interrupt can come from:
- the timer: a time slice is over
- a device: its data is ready
- an error.
The kernel is the interrupt handler:
- It checks the interrupt's priority.
- It saves the state of the running process: the contents of its registers, including the program counter. In the figure they go into the process's control block (PCB).
- It runs the routine for that interrupt.
- The low-level scheduler chooses which ready process runs, and that process's saved registers are loaded.
Remember:In round robin, a timer interrupt ends every slice.
Paging, segmentation and virtual memory
Paging cuts memory into equal pieces; segmentation cuts a program into logical parts. Virtual memory uses storage as extra RAM.
| Paging | Segmentation | |
|---|---|---|
| What is divided | memory into fixed-size frames, and a program into pages of the same size | a program into variable-size logical parts, such as its code, its data or a procedure |
| Who decides the size | the OS | the compiler works out the sizes |
| Its table | a page table records which frame holds each page | a segment table holds each segment's start address and length |
| Gaps | no gaps between programs; only a program's last page may be part-empty | leaves gaps that may be too small to use |
Virtual memory uses part of secondary storage as if it were extra RAM. Only the pages in use are kept in RAM.
Page faults, page replacement and thrashing
When a needed page is not in RAM, the OS loads it. With too little RAM, it does almost nothing else: thrashing.
A page fault occurs when a program needs a page that is not in RAM. The OS loads it from disk. If every frame is full, one page is removed first. Which one?
| Rule | Removes the page... |
|---|---|
| First in first out | loaded longest ago |
| Least recently used | last used longest ago |
| Optimal | not needed for the longest time |
Watch out:Optimal needs to know the future. So it is used only to judge the other two.
If RAM is too small for the pages in use, a removed page is soon needed again. The OS spends most of its time swapping pages and does little useful work. That is disk thrashing.
Interpreting: one statement at a time
An interpreter checks and runs one statement at a time. It never makes a translated copy of the program.
An interpreter runs a program without producing a translated version of it. It repeats two steps:
- Take one statement of the source code and check it for errors.
- If the statement is correct, carry it out at once and move to the next.
Watch out:If it finds an error, it reports it and stops. The statements before the error have already run.
Why is it slow?
- A statement inside a loop is examined again on every pass.
- All of this is repeated every time the program runs.
So interpreted programs run more slowly than compiled ones.
Total ← 0 FOR Count ← 1 TO 3 Total ← Total + Count OUTPUT Total NEXT Count OUTPUT "Done" &
The four stages of compilation
A compiler works in four stages: make tokens, check the grammar, produce object code, then make it better.
| Stage | What it does |
|---|---|
| Lexical analysis | removes spaces and comments; turns the source code into tokens (keywords, identifiers, operators and literals); builds the symbol table of identifiers |
| Syntax analysis | checks the tokens against the grammar rules of the language; builds a parse tree; reports any syntax errors |
| Code generation | produces the object code |
| Optimisation | changes the code so that it runs faster or uses less memory, without changing what it does |
A token is one meaningful unit of code, like one word in a sentence.
Example:Optimisation removes instructions that are never used, or works out a repeated calculation once. Below, A * 2 is worked out once instead of twice.
// before optimisation X ← A * 2 + B Y ← A * 2 + C // after: A * 2 is worked out once Temp ← A * 2 X ← Temp + B Y ← Temp + C
Writing a grammar: BNF and syntax diagrams
A grammar says which strings are allowed. BNF writes it as rules; a syntax diagram draws the same rules as paths.
The grammar of a language says which strings of symbols are allowed. One way to write it is Backus-Naur Form (BNF):
- Each rule has a name in angle brackets, such as
<digit>. - Then comes
::=, meaning "is defined as". - Then come the parts it is made of.
- The symbol
|separates alternatives: it means "or".
A rule that uses its own name on the right is recursive. Recursion is how BNF says "one or more", as in the rule for <digits> below.
A syntax diagram shows the same rules as paths to follow along the arrows:
- a box for each rule or symbol
- a branch for each alternative
- a line that loops back for a repeat.
Example:With the rules below, AB123$ is valid. A12# is invalid: it has only one letter.
<letter> ::= A | B | C | D | E
<digit> ::= 0 | 1 | 2 | 3 | 4
| 5 | 6 | 7 | 8 | 9
<symbol> ::= # | $ | &
<digits> ::= <digit> | <digit><digits>
<code> ::= <letter><letter><digits>
<symbol>
AB7# valid
AB123$ valid <digits> repeats
A12# invalid only one letter
AB12 invalid no symbol at the endReverse Polish notation
In Reverse Polish notation, each operator comes after its two values. A stack can then work out the answer, with no brackets.
In Reverse Polish notation (RPN), each operator is written after the two values it works on.
| Usual (infix) | RPN |
|---|---|
a + b | a b + |
(a + b) × (c − d) | a b + c d − × |
RPN needs no brackets and no precedence rules (rules such as "× before +"). It is read strictly from left to right. That makes it easy for a compiler or interpreter to evaluate with a stack:
- Push each operand.
- At an operator, pop two values, apply the operator and push the result. The value popped second is the left-hand one.
- The last value left is the answer.
Remember:Reading an expression tree in post-order gives its RPN.
infix (a + b) * (c - d)
RPN a b + c d - *
a = 6, b = 2, c = 5, d = 1
token: a b + c d - *
stack: 6 6 2 8 8 5 8 5 1 8 4 32Match each job to the part of the operating system that does it.
PICK ONE, THEN PICK ITS GROUP
- empty
- empty
- empty
- empty
- empty
- Click the stages below in the order they happen.
KEY TERMS
CHECK YOURSELF
1.A process asks to read from disk. Which state does it move to?
2.Which scheduling algorithm guarantees that no process is starved?
3.What is disk thrashing?
4.Which stage of compilation removes comments and produces tokens?
5.An interpreter runs a program that has an error on line 6. What is true when it stops?
6.In RPN, an operator pops two values from the stack. Which one is the left-hand operand?
7.How does BNF show that something can repeat, as in one or more digits?