XXX 03 Processes
XXX 03 Processes
1.1
OUTLINE
Process Concept
Relationship to a Program
What is a Process?
Process Lifecycle
Creation
Termination
Blocking
Process Management
Process Control Blocks
Context Switching
Threads
Inter-Process Communication
Requirements
Concept
Mechanisms
1.2
PROCESS CONCEPTS
Process Concept
Relationship to a Program
What is a Process?
Process Lifecycle
Process Management
Inter-Process Communication
2.1
WHAT IS A PROCESS?
The computer is there to execute programs, not the operating system!
Process Program
A program is static, on-disk
A process is dynamic, a program in execution
On a batch system, might refer to jobs instead of processes
2.2
WHAT IS A PROCESS?
Unit of protection and resource allocation
So you may have multiple copies of a process running
Each process executed on a virtual processor
Has a virtual address space (later)
Has one or more threads, each of which has
1. Program Counter: which instruction is executing
2. Stack: temporary variables, parameters, return addresses, etc.
3. Data Section: global variables shared among threads
2.3
PROCESS STATES
2.4
PROCESS LIFECYCLE
Process Concept
Process Lifecycle
Creation
Termination
Blocking
Process Management
Inter-Process Communication
3.1
PROCESS CREATION
Nearly all systems are hierarchical:
parent processes create child processes
Resource sharing:
Parent and children share all resources
Children share subset of parent's resources
Parent and child share no resources
3.2
PROCESS CREATION
Nearly all systems are hierarchical:
parent processes create child processes
Resource sharing
Execution:
Parent and children execute concurrently
Parent waits until children terminate
3.3
PROCESS CREATION
Nearly all systems are hierarchical:
parent processes create child processes
Resource sharing
Execution
Address space:
Child duplicate of parent
Child has a program loaded into it
3.4
EXAMPLES
Unix:
fork() system call creates a child process, cloned from parent; then
execve() system call used to replace the process' memory space with a new
program
NT/2K/XP:
3.5
PROCESS TERMINATION
Occurs under three circumstances
3.6
PROCESS TERMINATION
Occurs under three circumstances
3.7
PROCESS TERMINATION
Occurs under three circumstances
3.8
BLOCKING
In general a process blocks on an event, e.g.,
An IO device completes an operation
Another process sends a message
Assume OS provides some kind of general-purpose blocking primitive, e.g.,
await()
Need care handling concurrency issues, e.g.,
if(no key being pressed) {
await(keypress);
print("Key has been pressed!\n");
}
// handle keyboard input
3.9
CPU IO BURST CYCLE
Process execution consists of a cycle of CPU execution and IO wait
Processes can be described as either:
1. IO-bound:
spends more time doing IO than computation
many short CPU bursts
2. CPU-bound:
spends more time doing computations
a few, very long, CPU bursts
3 . 10
CPU IO BURST CYCLE
Observe that most processes execute for at most a few milliseconds before blocking
We need multiprogramming to obtain decent overall CPU utilisation
3 . 11
PROCESS MANAGEMENT
Process Concept
Process Lifecycle
Process Management
Process Control Blocks
Context Switching
Threads
Inter-Process Communication
4.1
PROCESS CONTROL BLOCK
OS maintains information about every process in a
data structure called a process control block (PCB). The
Process Context (highlighted) is the machine
environment during the time the process is actively
using the CPU:
Program counter
General purpose registers
Processor status register
[ Caches, TLBs, Page tables, ... ]
4.2
CONTEXT SWITCHING
To switch between processes, the OS must:
Save the context of the currently
executing process (if any), and
Restore the context of that being
resumed.
Note this is wasted time — no useful work is
carried out while switching
Time taken depends on hardware support
From nothing, to
Save/load multiple registers to/from
memory, to
Complete hardware "task switch"
THREADS
A thread represents an individual execution context
Threads are managed by a scheduler that determines which thread to run
Each thread has an associated Thread Control Block (TCB) with metadata about the
thread: saved context (registers, including stack pointer), scheduler info, etc.
Context switches occur when the OS saves the state of one thread and restores the
state of another. If between threads in different processes, process state also
switches
Threads visible to the OS are kernel threads — may execute in kernel or address
user space
4.4
3
INTER-PROCESS
COMMUNICATION
Process Concept
Process Lifecycle
Process Management
Inter-Process Communication
Requirements
Concept
Mechanisms
5.1
REQUIREMENTS
For meaningful communication to take place, two or more parties have to exchange
information according to a protocol:
Data transferred must be in a commonly-understood format (syntax)
Data transferred must have mutually-agreed meaning (semantics)
Data must be transferred according to mutually understood rules
(synchronisation)
In computer communications, the parties in question come in a range of forms,
typically:
Threads
Processes
Hosts
Ignore problems of discovery, identification, errors, etc. for now
5.2
INTER-PROCESS COMMUNICATION
In the context of this course, we are concerned with Inter-Process Communication
(IPC)
What it says on the tin — communication between processes on the same host
Key point — it is possible to share memory between those processes
Given the protection boundaries imposed by the OS, by design, the OS must be
involved in any communication between processes
Otherwise it would be tantamount to allowing one process to write over
another's address space
We'll focus on POSIX mechanisms
5.3
INTER-THREAD COMMUNICATION
It is a common requirement for two running threads to need to communicate
E.g., to coordinate around access to a shared variable
If coordination is not implemented, then all sorts of problems can occur. Range of
mechanisms to manage this:
Mutexes
Semaphores
Monitors
Lock-Free Data Structures
...
Not discussed here!
You'll get into the details next year in Concurrent and Distributed Systems
(Particularly the first half, on Concurrency)
5.4
INTER-HOST COMMUNICATION
Passing data between different hosts:
Traditionally different physical hosts
Nowadays often virtual hosts
Key distinction is that there is now no shared memory, so some form of transmission
medium must be used — networking
Also not discussed here!
In some sense it is "harder" than IPC because real networks are inherently:
Unreliable: data can be lost
Asynchronous: even if data is not lost, no guarantees can be given about
when it arrived
You'll see a lot more of this next year in Computer Networking
5.5
CONCEPT
For IPC to be a thing, first you need multiple processes
Initially created by running processes from a shell
Subsequently may be created by those processes, ad infinitum
(...until your machine dies from your fork bomb...)
5.6
FORK(2), WAIT(2)
Simply put, fork(2) allows a process to clone itself:
Parent process creates child process
Child receives copy-on-write (COW) snapshot of parent's address space
Parent typically then either:
5.7
SIGNALS
Simple asynchronous notifications on another process
A range of signals (28 at my last count), defined as numbers
Mapped to standard #defines, a few of which have standard mappings to
numbers
Use sigaction(2) to specify what function the signalled process should invoke
on receipt of a given signal
5.8
PIPES
open(2)
read(2)
write(2)
open(2) will block by default, until some other process opens the FIFO for reading
5 . 10
SHARED MEMORY SEGMENTS
What it says on the tin — obtain a segment of memory that is shared between two
(or more) processes
shmdt(2) to detach
shmctl(2) to destroy once you know no-one still using it
5 . 11
FILES
Locking can be mandatory (enforced) or advisory (cooperative)
Advisory is more widely available
fcntl(2) sets, tests and clears the lock status
Processes can then coordinate over access to files
read(2), write(2), seek(2) to interact and navigate
Memory Mapped Files present a simpler — and often more efficient — API
mmap(2) "maps" a file into memory so you interact with it via a pointer
Still need to lock or use some other concurrency control mechanism
5 . 12
UNIX DOMAIN SOCKETS
Sockets are commonly used in network programming — but there is (effectively) a
shared memory version for use between local processes, having the same API:
5 . 13
SUMMARY
Process Concept
Relationship to a Program
What is a Process?
Process Lifecycle
Creation
Termination
Blocking
Process Management
Process Control Blocks
Context Switching
Threads
Inter-Process Communication
Requirements
Concept
Mechanisms
6