Skip to content

System Software

A-Level Computer Science · Topic 16

Train
16.1

How an OS maximises use of resources

Syllabus
Candidates should be able to: Notes and guidance
Show understanding of how an OS can maximise the use of resources
Describe the ways in which the user interface hides the complexities of the hardware from the user
Show understanding of process management The concept of multi-tasking and a process The process states: running, ready and blocked The need for scheduling and the function and benefits of different scheduling routines (including round robin, shortest job first, first come first served, shortest remaining time) How the kernel of the OS acts as an interrupt handler and how interrupt handling is used to manage low-level scheduling
Show understanding of virtual memory, paging and segmentation for memory management The concepts of paging, virtual memory and segmentation The difference between paging and segmentation How pages can be replaced How disk thrashing can occur

Source: Cambridge International syllabus

A computer has many resources (CPU time, memory, disk, I/O) and many programs competing for them. The OS shares them fairly and efficiently so each is well used and the system stays responsive:

The OS shares CPU time, memory, disk and input/output between programs The OS shares the CPU, memory, disk and I/O between programs

  • multi-tasking 多任务 — switch the CPU quickly between processes so several seem to run at once.
  • memory management — give each process the memory it needs; use disk paging 分页 when RAM runs out.
  • spooling 假脱机 and buffering — print jobs queue on disk so the CPU never waits for the printer.
  • caching — keep recently-used disk data in cache 高速缓存 / RAM.

A processor with its cooler The processor is a key resource the OS shares between competing tasks

Memory modules (RAM) The OS also manages memory (RAM), deciding what to keep in it and what to page out to disk

Explore

The life of a process

Tap round the loop a process travels. It only runs when the scheduler picks it; needing I/O sends it to blocked, and finishing its time slice sends it back to ready — round and round until it's done.

Vocabulary Train
English Chinese Pinyin
multi-tasking 多任务 duō rèn wù
paging 分页 fēn yè
spooling 假脱机 jiǎ tuō jī
cache 高速缓存 gāo sù huǎn cún
16.1

The user interface

The user interface hides the hardware behind friendly abstractions: the user sees windows, menus and folders, not addresses or sectors. One click on an icon makes the OS find the program on disk, allocate memory, load it and start it. A CLI (command line) is powerful and scriptable for experts; a GUI (graphical) is easier to learn. Most systems offer both.

16.1

Process management

A process 进程 is a program in execution — its code, current state, memory and open files.

Scheduling

The scheduler 调度器 chooses which ready process runs next, and for how long:

  • round robin 轮转 — each process gets a fixed time slice 时间片, then goes to the back of the queue.
  • first-come-first-served; shortest job first; shortest remaining time (run the job with the least work left); priority; multilevel feedback queues.

The trade-off is responsiveness vs throughput vs fairness.

A Gantt timeline showing P1 then P2, P3, P4 run one after another from time 0 to 39, with a key giving each process's CPU burst time First-come-first-served scheduling of four processes

Round-robin scheduling shown as a timeline: P1, P2, P3 each get a fixed time slice in turn, then the cycle repeats, sharing the CPU between them Round-robin: each process gets a fixed time slice in turn, then the next runs (unlike first-come-first-served)

Process states

A process is new, ready (waiting for the CPU), running, blocked 阻塞 (waiting for I/O or a lock), or terminated. When its time slice ends it goes running → ready; when it requests I/O it goes running → blocked; when the I/O finishes it goes blocked → ready.

A state diagram: new to ready (admit), ready to running (dispatch by the scheduler), running to ready (interrupt or time-out), running to blocked (request I/O), blocked back to ready (I/O complete), running to terminated (exit) A process moves between the new, ready, running, blocked and terminated states

Process control block and context switch

For each process the OS keeps a process control block 进程控制块 (PCB) — the saved program counter, registers, state and memory info.

A context switch saves process A's state (its PCB) and loads process B's A context switch saves one process's state and loads another's

  • a context switch 上下文切换 suspends one process and starts another: it saves the state into one PCB and restores it from another. This small cost is paid on every switch.
  • the kernel 内核 (the core of the OS) acts as an interrupt handler 中断处理程序. When a device or the timer raises an interrupt, interrupt handling 中断处理 saves the running process and runs the right routine — this is what drives low-level scheduling.

Inter-process communication

Processes are isolated, so the OS provides inter-process communication 进程间通信: pipes 管道 (one program's output feeds another's input), shared memory 共享内存 (a region several processes can use), and message passing.

Vocabulary Train
English Chinese Pinyin
process 进程 jìn chéng
scheduler 调度器 diào dù qì
round robin 轮转 lún zhuàn
time slice 时间片 shí jiān piàn
blocked 阻塞 zǔ sè
process control block 进程控制块 jìn chéng kòng zhì kuài
context switch 上下文切换 shàng xià wén qiè huàn
inter-process communication 进程间通信 jìn chéng jiān tōng xìn
pipes 管道 guǎn dào
shared memory 共享内存 gòng xiǎng nèi cún
kernel 内核 nèi hé
interrupt handler 中断处理程序 zhōng duàn chǔ lǐ chéng xù
interrupt handling 中断处理 zhōng duàn chǔ lǐ
16.1

Virtual memory, paging, segmentation

Each process gets its own virtual address space 虚拟地址空间 — a clean, contiguous range of addresses the OS maps to physical memory. This gives each process a simple space, protects processes from each other, and lets the total memory exceed physical RAM.

In paging, the virtual space is split into fixed-size pages and physical memory into same-sized frames 页框. A page table maps each page to a frame. If an accessed page is not in RAM — a page fault 缺页 — the OS reads it from the swap file 交换文件 into a frame, evicting another page if RAM is full. Frequent faults cause thrashing 抖动 (disk thrashing), where the OS spends most of its time swapping pages instead of doing useful work.

Logical-memory pages mapped through a page table to non-contiguous physical-memory frames Paging maps each page of logical memory to a frame of physical memory

In segmentation 分段, memory is split into variable-sized logical segments (code, stack, heap), each with its own permissions. Many systems use paging within segments.

Variable-sized logical segments (code, heap, stack) mapped through a segment table of sizes and start addresses to physical memory Segmentation maps variable-sized segments using a segment map table

Explore

What happens on a page fault

Step through a page fault. When the program touches a page that isn't in RAM, the OS quietly fetches it from disk and updates the page table — so the program sees more memory than physically exists.

Vocabulary Train
English Chinese Pinyin
virtual address space 虚拟地址空间 xū nǐ dì zhǐ kōng jiān
pages
frames 页框 yè kuāng
page fault 缺页 quē yè
swap file 交换文件 jiāo huàn wén jiàn
thrashing 抖动 dǒu dòng
segmentation 分段 fēn duàn
16.2

How an interpreter runs a program

Syllabus
Candidates should be able to: Notes and guidance
Show understanding of how an interpreter can execute programs without producing a translated version
Show understanding of the various stages in the compilation of a program Including lexical analysis, syntax analysis, code generation and optimisation
Show understanding of how the grammar of a language can be expressed using syntax diagrams or Backus-Naur Form (BNF) notation
Show understanding of how Reverse Polish Notation (RPN) can be used to carry out the evaluation of expressions

Source: Cambridge International syllabus

An interpreter 解释器 translates and runs the source at the same time. For each statement it reads the line, does lexical and syntax analysis, checks types, then executes the action, and moves on. Errors are reported immediately and it usually stops; no executable is produced. The translation is redone every run (slower), but it gives fast development feedback and is portable.

Explore

The phases of compilation

Step through what a compiler does to your source. Each phase hands its output to the next — characters become tokens, tokens become a tree, the tree becomes optimised machine code.

Vocabulary Train
English Chinese Pinyin
interpreter 解释器 jiě shì qì
16.2

Stages of compilation

A compiler 编译器 turns source into machine code 机器码 in phases:

  1. lexical analysis 词法分析 — the lexer groups characters into tokens 词法单元 (keywords, identifiers, operators, literals), discarding whitespace and comments.
  2. syntax analysis (parsing) 语法分析 — check the tokens fit the grammar and build an abstract syntax tree 抽象语法树. A missing bracket gives a syntax error 语法错误.
  3. semantic analysis 语义分析 — check the program makes sense (variables declared, types match).
  4. code generation 代码生成 — walk the tree and emit target code, choosing registers and layouts.
  5. code optimisation 代码优化 — remove redundant work, fold constants, reorder for the pipeline.

The output is an executable.

The phases of compilation: source code passes through lexical analysis (tokens), syntax analysis (AST), semantic analysis (checks), code generation and optimisation to produce an executable The phases of compilation, from source code to an optimised executable

Vocabulary Train
English Chinese Pinyin
compiler 编译器 biān yì qì
machine code 机器码 jī qì mǎ
lexical analysis 词法分析 cí fǎ fēn xī
tokens 词法单元 cí fǎ dān yuán
syntax analysis (parsing) 语法分析 yǔ fǎ fēn xī
abstract syntax tree 抽象语法树 chōu xiàng yǔ fǎ shù
syntax error 语法错误 yǔ fǎ cuò wù
semantic analysis 语义分析 yǔ yì fēn xī
code generation 代码生成 dài mǎ shēng chéng
code optimisation 代码优化 dài mǎ yōu huà
16.2

Grammar: BNF and syntax diagrams

A grammar 文法 says which token sequences are valid programs.

Backus-Naur Form 巴科斯-诺尔范式 (BNF) is textual. A production rule 产生式 has the form:

<symbol> ::= alternative1 | alternative2 | ...

Each alternative is a sequence of terminal 终结符 symbols (literal text) and non-terminal 非终结符 symbols (other rule names):

<digit>      ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>

The recursive third rule expresses "a letter followed by any number of letters or digits". An IF statement:

<if-statement> ::= IF <condition> THEN <statement> ENDIF
                 | IF <condition> THEN <statement> ELSE <statement> ENDIF

A syntax diagram 语法图 (railroad diagram) shows the same thing graphically: boxes for non-terminals, rounded boxes for terminals, arrows for valid paths, loops for repetition. The two notations are equivalent. The parser uses the grammar to decide whether a program is valid.

A railroad diagram for an assignment: a rectangular identifier box, a rounded assignment-symbol box, then a rectangular expression box, connected left to right A syntax (railroad) diagram for an assignment statement

Vocabulary Train
English Chinese Pinyin
grammar 文法 wén fǎ
Backus-Naur Form 巴科斯-诺尔范式 bā kē sī - nuò ěr fàn shì
production rule 产生式 chǎn shēng shì
terminal 终结符 zhōng jié fú
non-terminal 非终结符 fēi zhōng jié fú
syntax diagram 语法图 yǔ fǎ tú
16.2

Reverse Polish Notation (RPN)

In infix 中缀 notation the operator sits between its operands (3 + 4 * 2), needing brackets and precedence rules. In Reverse Polish Notation 逆波兰表示法 (RPN, postfix 后缀) the operator follows its operands (3 4 2 * +), needing no brackets.

Converting infix to RPN

Use an operator stack. Scan left to right: output an operand; for an operator, first pop any stacked operators of higher or equal precedence 优先级 to the output, then push it; push (; on ) pop to output until the matching (. At the end, pop all operators. Example: (3 + 4) * 23 4 + 2 *.

Evaluating RPN

Use a stack of operands. Scan left to right: push each operand; on an operator, pop the top two, apply it, and push the result. Evaluating 3 4 2 * +:

Token Stack
3 3
4 3, 4
2 3, 4, 2
* 3, 8
+ 11

Result: 11. RPN needs no brackets at evaluation time and suits a stack machine — which is how the JVM and many bytecode 字节码 interpreters work.

Worked example. Convert $(A + B) \times (C - D)$ to RPN, then evaluate $(3 + 4) \times (5 - 2)$. Scan left to right using an operator stack. Push (; output A; push +; output B; on ) pop back to the matching (, giving A B + so far. Push ×, and the second bracket behaves the same way, giving C D -. At the end pop the ×. Result: A B + C D - ×. To evaluate the numbers, use a stack of operands: push 3, push 4; + pops both and pushes 7; push 5, push 2; - pops both and pushes 3; × pops 7 and 3 and pushes 21. Two things make these reliable: the operands keep their original order through the conversion (only the operators move), and every operator acts on the two values immediately below it on the stack.

Explore

Operator precedence — what RPN removes

In ordinary infix maths × and ÷ bind tighter than + and −, so you must apply rules in the right order. Reverse Polish Notation writes the operands first (3 4 2 × + 1 −), fixing the order so no precedence rules are needed.

Vocabulary Train
English Chinese Pinyin
infix 中缀 zhōng zhuì
Reverse Polish Notation 逆波兰表示法 nì bō lán biǎo shì fǎ
postfix 后缀 hòu zhuì
stack zhàn
precedence 优先级 yōu xiān jí
bytecode 字节码 zì jié mǎ
16.2

Exam tips

  • List the stages of compilation (lexical, syntax and semantic analysis, code generation, optimisation) and what each does.
  • Explain virtual memory and paging (disk used as extra RAM) and its cost (disk thrashing).
  • Evaluate Reverse Polish Notation with a stack — no brackets are needed.
  • Use BNF or a syntax diagram to test whether a string is valid.

Log in or create account

IGCSE & A-Level