OS & Architecture
Modern Operating System Functionality
Process and Thread Management
Concurrency: Doing many things simultaneously (I/0, processing, multiple programs, etc...)
Several users work at the same time as if each has a private machine.
Threads (unit of OS control), one thread on the CPU at a time, but many threads active concurrently.
I/O devices: Let the CPU work while a slow I/O device is working.
Memory management: OS coordinates allocation of memory and moving data between disk and main memory.
Files: OS coordinates how disk space is used for files, in order to find files and to store multiple files.
Distributed systems & networks: Allow a group of machines to work together on distributed hardware.
Generic Computer Architecture
CPU: The processor that performs the actual computation.
Multiple “cores” common in today’s processors.
I/O devices: Terminal, disks, video board, printer, etc...
Network card is a key component, but also an I/O device.
Memory: RAM containing data and programs used by the CPU.
System bus: Communication medium between CPU, memory, and peripherals.
Protection
CPU supports a set of assembly instructions:
MOV [address], ax
ADD ax, bx
MOV CRn (move control register)
IN, INS (input string)
HLT (halt)
LTR (load task register)
INT n (software interrupt)
Some instructions are sensitive or privileged.
Kernel mode vs User mode
To protect the system from aberrant users and processors, some instructions are restricted to use only by the OS.
Users may not:
Address I/O directly
Use instructions that manipulate the state of memory (page table pointers, TLB load, etc...)
Set the mode bits that determine user or kernel mode
Disable and enable interrupts
Halt the machine
In kernel mode, the OS can do all these things.
The hardware must support at least kernel and user mode.
A status bit in a protected processor register indicates the mode.
Protected instructions can only be executed in kernel mode.
System call: OS procedure that executes privileged instructions (e.g., I/O), also API exported by the kernel.
Examples of system calls:
Memory Protection
Architecture must provide support so that the OS can:
Protect user programs from each other.
Protect the OS from user programs.
The simplest technique is to use base and limit registers.
Base and limit registers are loaded by the OS before starting a program.
The CPU checks each user reference (instruction and data addresses), ensuring it falls between the base and limit register values.
Process layout in memory
Processes have three segments:
text
data
stack
Registers
Register is a dedicated name for one word of memory managed by CPU.
General-purpose on x86:
EAX
EBX
ECX
Special-purpose:
“SP” = stack pointer
“FP” = Frame pointer
“PC” = Program counter
Change processes: Save current registers & load saved registers is called a context switch.
Memory Hierarchy
Caches
Access to main memory is expensive:
100 cycles (slow, but relatively cheap)
Caches are small, fast, expensive memory:
Hold recently-accessed data (D$) or instructions (I$)
Different sizes & locations
Level 1 (L1): On-chip, smallish (tens of KB)
Level 2 (L2): On or next to chip, larger (few MB)
Level 3 (L3): Pretty large, on bus (several MB)
Manages lines of memory (32-128 bytes)
Caches are managed by hardware (no explicit OS management)
Traps
Traps: special conditions detected by the architecture
Examples:
Page fault
Write to a read-only page
Overflow
Systems call
On detecting a trap, the hardware:
Saves the state of the process (PC, stack, etc...)
Transfers control to appropriate trap handler (OS routine)
The CPU indexes the memory-mapped trap vector with the trap number.
Then jumps to the address given in the vector.
Starts to execute at that address.
On completion, the OS resumes execution of the process.
Modern OS use Virtual Memory traps for many functions:
Debugging
Distributed VM
Garbage collection
Copy-on-write, etc...
Traps are a performance optimization. A less efficient solution is to insert extra instructions into the code everywhere a special condition could arise.
I/O Control
Each I/O device has a little processor inside it that enables it to run autonomously.
CPU issues commands to I/O devices, and continues.
When the I/O device completes the command, it issues an interrupt.
CPU stops whatever it was doing and the OS processes the I/O device's interrupt.
Three I/O Methods
Synchronous
Asynchronous
Memory-mapped
Virtual Memory
Virtual memory allows users to run programs without loading the entire program in memory at once.
Instead, pieces of the program are loaded as they are needed.
The OS must keep track of which pieces are in which parts of physical memory and which pieces are on disk.
In order for pieces of the program to be located and loaded without causing a major disruption to the program, the hardware provides a translation lookaside buffer to speed the lookup.
Last updated