Object-Oriented Programming
Operating Systems
Lecture 2a
Dr Ronald Grau School of Engineering and Informatics Spring term 2018
Previous Lecture
Operating system architectures
Abstractions
Processes
Virtual memory
Files
System call interface
User mode vs.
Kernel mode
1
Basic design principles
Separation of interface and implementation
Separation of policy and mechanism
Portability
Coherence
Basic architectures
Layers & modules
Monolithic kernels
Microkernels
Today
Processes
Bootstrapping
Processes
Creation
Management
Execution
Termination
2
Bootstrapping
How to start up an operating system?
There is usually a program (called bootstrap) in Read-Only Memory (ROM)
It loads the operating system files from persistent storage into main memory
3
BIOS now largely replaced by UEFI (Unified Extensible Firmware Interface)
Example: A local operating system on a PC
BIOS:
Basic Input/Output System
MBR:
Master Boot Record
Bootsector
Bootloader
Recap: The process abstraction
A process is a program in execution
Operating system duties:
Process creation:
Load the executable file from secondary storage into primary memory (RAM)
Handle concurrency
Scheduling
Coordination of resource requests
Synchronisation
Signalling of events (e.g. I/O)
Inter-process communication (IPC)
4
Recap: The process abstraction
Why do we need the process abstraction? Can’t we just run programs?
On modern operating systems, we often want programs to run at the same time.
We need mechanisms to coordinate their concurrent execution. This means these
programs will have to share available resources during execution.
We also want to provide the „illusion“ that each program can have exclusive access
to the CPU, memory, and other resources – without burdening the developers of
these programs with the finer details about how all that will work during runtime.
The process abstraction provides the operating system with the means to effectively
manage different aspects of program execution.
5
Processes
A few fun facts about processes
Processes can be created and killed.
Processes can have children.
A process is more than just a program.
6
Process creation
When is a process created?
Whenever some kind of program is started, e.g.:
Operating system startup
OS starting a service (daemon)
User login
User opening an application
Process creating another process (child)
7
Process creation
About parents and children
A process becomes a parent when it creates other processes (its children).
Child processes „receive“ resources from their parent or independently from the OS.
Flexible execution: Parent and child can either run concurrently, or the former waits
for the latter to finish.
Flexible use of address space: The child can be directly derived from the parent (a
copy of the same program & data) or run an entirely different program.
Related system call (UNIX): fork
8
Process creation
Process hierarchy
Operating systems like LINUX have a root process (init) that spawns all others
9
Example of a process hierarchy (LINUX)
Process management
Some book-keeping the operating system does
Many things to watch: processes, memory, files, devices, etc.
The operating system maintains a process table to keep track of active processes.
For each process, a process image is stored in virtual memory, which contains all
relevant information about its state.
10
Process management
Process image contents:
Process Control Block (PCB)
Process ID, parent process ID, user ID
Process state (e.g., current activity, register contents, program counter)
CPU scheduling information (e.g., priority)
Memory management information, process privileges
Accounting information (e.g., resource use statistics, time limits)
I/O status information (e.g., devices & files allocated to the process)
User program
User data
Stack
Heap
11
All of this must
be stored in
virtual memory
while the
process is
active
Process management
Virtual address space
Processes use a private virtual address space that “pretends” they have
exclusive access to main memory
12
00
01
02
.
.
.
FF
Process 1 Process 2
00
01
02
.
.
.
FF
Process management
Virtual address space
In reality, processes will occupy different addresses of the same physical memory
Some of the memory pages may be swapped to secondary memory
13
00
01
02
.
.
.
FF
Process 1 Process 2
00
01
02
.
.
.
FF
Primary memory
Secondary memory
00
01
02
.
.
.
FF
Process execution
Control flow #1
In principle, a central processing unit
(CPU) will execute individual
instructions in sequence.
Instructions are retrieved from main
memory (or a cache) to be executed
by the CPU’s control unit.
The flow of control is represented by
the sequence of values stored in the
program counter as the system operates.
14
Basic operation cycle of a computing system
Process execution
Control flow #2
A simple flow of control might be a
sequence of instructions that are stored
in adjacent memory locations.
However, this simple flow of control can
change when the sequence is interrupted by
use of control structures at the program level
(e.g., conditional and unconditional jumps),
but also if there are lower-level events that
the CPU needs to attend to.
15
Memory locations
…
Process execution
Exceptional control flow (ECF)
Exceptions do not only exist in user applications
but also on the operating system level.
Typical exception classes at those levels are
Interrupt, Trap, Fault, and Abort.
Whenever an exception occurs, control
is transferred to an appropriate
exception handler.
ECF is the underlying mechanism for
how operating systems control
the execution of processes.
16
Application
EVENT
Exception
Exception Handler
Exception handling
Possible exception returns
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5
Instruction 6
…
Process execution
Exception handling
An exception triggers a sudden transfer of control from the user program to the OS
Each exception has a unique number
The OS looks up the correct exception handler from an exception table
The exception handler runs in kernel mode
17
Process execution
Interrupt Exception
Interrupts occur asynchronously as a result of signals from I/O devices that are
external to the processor.
They are not “caused” by program execution as such.
18
Application
E.g.,
User presses a mouse button
or
Read from HDD finished
or
Incoming network traffic, etc.
Exception
Interrupt Handler
Exception handling
Program execution is continued
at the next instruction
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5
Instruction 6
…
Process execution
Trap Exception
Traps are intentional exceptions that occur as a result of executing an instruction
that triggers control transfer to the operating system, usually a system call.
19
Application
E.g.,
Create a child process
or
Load another program
or
Allocate some memory
or
Open / read a file, etc.
Exception
Trap Handler
Exception handling
Program execution is continued
at the next instruction
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5
Instruction 6
…
Process execution
Fault Exception
Faults result from error conditions that a handler might be able to fix.
If the handler is able to correct the error condition, it returns control to the current
instruction for re-execution. Else, the handler runs a routine that terminates the
process.
20
Application
E.g.,
Memory page missing
or
Unauthorised access of
protected memory
Exception
Fault Handler
Exception handling
successful?
The current
instruction
is repeated
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5
Instruction 6
…
abort routine
(terminate the application)
NOYES
Process execution
Abort Exception
Aborts result from unrecoverable fatal errors, typically hardware errors such as
parity errors that occur when memory bits are corrupted.
Abort handlers do not return control back to the application program.
21
Application
Fatal hardware error
or
Division by zero (UNIX)
Exception
Abort Handler
Exception handling
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5
Instruction 6
…
abort routine
(terminate the application)
Process execution
Flow of control for concurrent processes
Processes running concurrently do not have exclusive access to the CPU
(Although multi-core CPUs can support parallel execution).
Each process executes a portion of its normal flow and may then be pre-empted
(temporarily suspended) while the other processes take their turns.
To a program running in the context of one of these processes, appearances are
that it has exclusive use of the CPU.
22
time
Process 1
Process 2
Process 3
Process execution
Process switching
A process switch is an exception-like activity that the operating system kernel
uses to assign a particular process to the CPU to work on.
The Kernel maintains the context of every process (remember the process image?)
and can use this information to pre-empt and resume different concurrent processes.
23
time
Process 1
Process 2
Process 3
Process execution
Process switching
During a process switch, the kernel (1) saves the context of the current process
(remember the process image?), then (2) pre-empts that process, and (3) restores the
context of another process before (4) passing control to it.
24
Process 1
Process 2
(Process) context switch (Process) context switch
Process execution
Basic process model
As a process executes, it changes state:
Created: Waiting to be admitted for scheduling
Ready: Waiting to be assigned to CPU
Running: Instructions are being executed
Blocked: Waiting for something to happen
Terminated: Stopped/finished execution,
deletion imminent
25
Created
Terminated
Running
Blocked
Ready
admitted
dispatched
I/O or event
completion
interrupt
I/O or
event
wait exit
Basic model of the process life-cycle
How does this work with swapping?
Process memory management
Swapping
Virtual memory can make use of
secondary storage in order to
temporarily suspend certain processes
Possible reasons:
Insufficient memory
Process suspected of causing a problem
User request, e.g. debugging
Timing, e.g. periodic process
Parent process request
26
00
01
02
.
.
.
FF
Virtual address
space of the
process
Primary memory
Secondary memory
Process execution
Process model with suspension
27
Created
Terminated
Running
Blocked
Ready
admitted
dispatched
I/O or event
completion
interrupt
I/O or
event
wait exit
Model of the process life-cycle that supports suspending processes
Suspended,
Ready
Suspended,
Blocked
I/O or event
completion
suspended
suspended
resumed
resumed
Process termination
Execution stops indefinitely
Program finished – job done.
Programmatic error routine – e.g., exception handling
An unexpected fatal error – e.g., division by 0, illegal memory reference, etc.
Killed by another process – requires authorisation, e.g., a superior process getting rid
of its helper processes.
Related system calls: exit , kill
28
Summary
Processes
Bootstrapping
Processes
Creation (fork system call, parents, children)
Management (process table, private virtual memory address space, PCB, swapping)
Execution (exceptional control flow, scheduling, process switch, process models)
Termination (kill and exit system calls)
29
Read
Tanenbaum & Bos., Modern Operating Systems
Chapter 2
Silberschatz et al., Operating System Concepts
Chapter 3
30
Next Lecture
Introduction
Operating System Architectures
Processes
Threads
Process Scheduling
Process Synchronisation
31
Deadlocks
Memory Management
File Systems
Input / Output
Security and Virtualisation