Modern Operating Systems: Third Edition Andrew S. Tanenbaum
Modern Operating Systems: Third Edition Andrew S. Tanenbaum
Modern Operating Systems: Third Edition Andrew S. Tanenbaum
Third Edition
ANDREW S. TANENBAUM
Chapter 1
Introduction
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
What Is An Operating System (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
What Is An Operating System (2)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Operating System as an Extended
Machine
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
History of Operating Systems
Generations:
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Transistors and Batch Systems (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Multithreaded and Multicore Chips
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Disks
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
I/O Devices
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Processes
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Files (1)
Figure 1-15. (a) Before mounting, the files on the CD-ROM are not
accessible. (b) After mounting, they are part of the file
hierarchy.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Files (3)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
System Calls
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
System Calls for File Management (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
System Calls for File Management (2)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Miscellaneous System Calls
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
A Simple Shell
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory Layout
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Windows Win32 API
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Monolithic Systems
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Layered Systems
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Microkernels
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Client-Server Model
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Virtual Machines (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Virtual Machines (2)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The World According to C
• The C language
• Header files
• Large programming projects
• The model of run time
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Model of Run Time
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 3: Processes
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server Systems
Operating System Concepts – 9th Edition 3.2 Silberschatz, Galvin and Gagne ©2013
Objectives
To introduce the notion of a process -- a program in
execution, which forms the basis of all computation
To describe the various features of processes, including
scheduling, creation and termination, and communication
To explore interprocess communication using shared memory
and message passing
To describe communication in client-server systems
Operating System Concepts – 9th Edition 3.3 Silberschatz, Galvin and Gagne ©2013
Process Concept
Operating System Concepts – 9th Edition 3.4 Silberschatz, Galvin and Gagne ©2013
Process Concept (Cont.)
Program is passive entity stored on disk (executable file),
process is active
Program becomes process when executable file loaded into
memory
Execution of program started via GUI mouse clicks, command
line entry of its name, etc
One program can be several processes
Consider multiple users executing the same program
Operating System Concepts – 9th Edition 3.5 Silberschatz, Galvin and Gagne ©2013
Process in Memory
Operating System Concepts – 9th Edition 3.6 Silberschatz, Galvin and Gagne ©2013
Process State
Operating System Concepts – 9th Edition 3.7 Silberschatz, Galvin and Gagne ©2013
Diagram of Process State
Operating System Concepts – 9th Edition 3.8 Silberschatz, Galvin and Gagne ©2013
Process Control Block (PCB)
Information associated with each process
(also called task control block)
Process state – running, waiting, etc
Program counter – location of
instruction to next execute
CPU registers – contents of all process-
centric registers
CPU scheduling information- priorities,
scheduling queue pointers
Memory-management information –
memory allocated to the process
Accounting information – CPU used,
clock time elapsed since start, time
limits
I/O status information – I/O devices
allocated to process, list of open files
Operating System Concepts – 9th Edition 3.9 Silberschatz, Galvin and Gagne ©2013
CPU Switch From Process to Process
Operating System Concepts – 9th Edition 3.10 Silberschatz, Galvin and Gagne ©2013
Threads
So far, process has a single thread of execution
Consider having multiple program counters per process
Multiple locations can execute at once
Multiple threads of control -> threads
Must then have storage for thread details, multiple program
counters in PCB
See next chapter
Operating System Concepts – 9th Edition 3.11 Silberschatz, Galvin and Gagne ©2013
Process Representation in Linux
Operating System Concepts – 9th Edition 3.12 Silberschatz, Galvin and Gagne ©2013
Process Scheduling
Operating System Concepts – 9th Edition 3.13 Silberschatz, Galvin and Gagne ©2013
Ready Queue And Various I/O Device Queues
Operating System Concepts – 9th Edition 3.14 Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling
Operating System Concepts – 9th Edition 3.15 Silberschatz, Galvin and Gagne ©2013
Schedulers
Short-term scheduler (or CPU scheduler) – selects which process should
be executed next and allocates CPU
Sometimes the only scheduler in a system
Short-term scheduler is invoked frequently (milliseconds) ⇒ (must be
fast)
Long-term scheduler (or job scheduler) – selects which processes should
be brought into the ready queue
Long-term scheduler is invoked infrequently (seconds, minutes) ⇒
(may be slow)
The long-term scheduler controls the degree of multiprogramming
Processes can be described as either:
I/O-bound process – spends more time doing I/O than computations,
many short CPU bursts
CPU-bound process – spends more time doing computations; few very
long CPU bursts
Long-term scheduler strives for good process mix
Operating System Concepts – 9th Edition 3.16 Silberschatz, Galvin and Gagne ©2013
Addition of Medium Term Scheduling
Medium-term scheduler can be added if degree of multiple
programming needs to decrease
Remove process from memory, store on disk, bring back in
from disk to continue execution: swapping
Operating System Concepts – 9th Edition 3.17 Silberschatz, Galvin and Gagne ©2013
Multitasking in Mobile Systems
Some mobile systems (e.g., early version of iOS) allow only one
process to run, others suspended
Due to screen real estate, user interface limits iOS provides for a
Single foreground process- controlled via user interface
Multiple background processes– in memory, running, but not
on the display, and with limits
Limits include single, short task, receiving notification of events,
specific long-running tasks like audio playback
Android runs foreground and background, with fewer limits
Background process uses a service to perform tasks
Service can keep running even if background process is
suspended
Service has no user interface, small memory use
Operating System Concepts – 9th Edition 3.18 Silberschatz, Galvin and Gagne ©2013
Context Switch
When CPU switches to another process, the system must save
the state of the old process and load the saved state for the
new process via a context switch
Context of a process represented in the PCB
Context-switch time is overhead; the system does no useful
work while switching
The more complex the OS and the PCB the longer the
context switch
Time dependent on hardware support
Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
Operating System Concepts – 9th Edition 3.19 Silberschatz, Galvin and Gagne ©2013
Operations on Processes
Operating System Concepts – 9th Edition 3.20 Silberschatz, Galvin and Gagne ©2013
Process Creation
Parent process create children processes, which, in turn
create other processes, forming a tree of processes
Generally, process identified and managed via a process
identifier (pid)
Resource sharing options
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options
Parent and children execute concurrently
Parent waits until children terminate
Operating System Concepts – 9th Edition 3.21 Silberschatz, Galvin and Gagne ©2013
A Tree of Processes in Linux
Operating System Concepts – 9th Edition 3.22 Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
Address space
Child duplicate of parent
Child has a program loaded into it
UNIX examples
fork() system call creates new process
exec() system call used after a fork() to replace the
process’ memory space with a new program
Operating System Concepts – 9th Edition 3.23 Silberschatz, Galvin and Gagne ©2013
C Program Forking Separate Process
Operating System Concepts – 9th Edition 3.24 Silberschatz, Galvin and Gagne ©2013
Creating a Separate Process via Windows API
Operating System Concepts – 9th Edition 3.25 Silberschatz, Galvin and Gagne ©2013
Process Termination
Operating System Concepts – 9th Edition 3.26 Silberschatz, Galvin and Gagne ©2013
Process Termination
Operating System Concepts – 9th Edition 3.27 Silberschatz, Galvin and Gagne ©2013
Multiprocess Architecture – Chrome Browser
Operating System Concepts – 9th Edition 3.28 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
Operating System Concepts – 9th Edition 3.29 Silberschatz, Galvin and Gagne ©2013
Communications Models
(a) Message passing. (b) shared memory.
Operating System Concepts – 9th Edition 3.30 Silberschatz, Galvin and Gagne ©2013
Cooperating Processes
Independent process cannot affect or be affected by the execution
of another process
Cooperating process can affect or be affected by the execution of
another process
Advantages of process cooperation
Information sharing
Computation speed-up
Modularity
Convenience
Operating System Concepts – 9th Edition 3.31 Silberschatz, Galvin and Gagne ©2013
Producer-Consumer Problem
Paradigm for cooperating processes, producer process
produces information that is consumed by a consumer
process
unbounded-buffer places no practical limit on the size
of the buffer
bounded-buffer assumes that there is a fixed buffer
size
Operating System Concepts – 9th Edition 3.32 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer – Shared-Memory Solution
Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Operating System Concepts – 9th Edition 3.33 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer – Producer
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Operating System Concepts – 9th Edition 3.34 Silberschatz, Galvin and Gagne ©2013
Bounded Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
Operating System Concepts – 9th Edition 3.35 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Shared Memory
Operating System Concepts – 9th Edition 3.36 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Message Passing
Operating System Concepts – 9th Edition 3.37 Silberschatz, Galvin and Gagne ©2013
Message Passing (Cont.)
Operating System Concepts – 9th Edition 3.38 Silberschatz, Galvin and Gagne ©2013
Message Passing (Cont.)
Operating System Concepts – 9th Edition 3.39 Silberschatz, Galvin and Gagne ©2013
Direct Communication
Processes must name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
Properties of communication link
Links are established automatically
A link is associated with exactly one pair of communicating
processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
Operating System Concepts – 9th Edition 3.40 Silberschatz, Galvin and Gagne ©2013
Indirect Communication
Operating System Concepts – 9th Edition 3.41 Silberschatz, Galvin and Gagne ©2013
Indirect Communication
Operations
create a new mailbox (port)
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Operating System Concepts – 9th Edition 3.42 Silberschatz, Galvin and Gagne ©2013
Indirect Communication
Mailbox sharing
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 receive
Who gets the message?
Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive
operation
Allow the system to select arbitrarily the receiver.
Sender is notified who the receiver was.
Operating System Concepts – 9th Edition 3.43 Silberschatz, Galvin and Gagne ©2013
Synchronization
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send -- the sender is blocked until the message is
received
Blocking receive -- the receiver is blocked until a message
is available
Non-blocking is considered asynchronous
Non-blocking send -- the sender sends the message and
continue
Non-blocking receive -- the receiver receives:
A valid message, or
Null message
Different combinations possible
If both send and receive are blocking, we have a rendezvous
Operating System Concepts – 9th Edition 3.44 Silberschatz, Galvin and Gagne ©2013
Synchronization (Cont.)
message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);
Operating System Concepts – 9th Edition 3.45 Silberschatz, Galvin and Gagne ©2013
Buffering
Operating System Concepts – 9th Edition 3.46 Silberschatz, Galvin and Gagne ©2013
Examples of IPC Systems - POSIX
Operating System Concepts – 9th Edition 3.47 Silberschatz, Galvin and Gagne ©2013
IPC POSIX Producer
Operating System Concepts – 9th Edition 3.48 Silberschatz, Galvin and Gagne ©2013
IPC POSIX Consumer
Operating System Concepts – 9th Edition 3.49 Silberschatz, Galvin and Gagne ©2013
Examples of IPC Systems - Mach
Mach communication is message based
Even system calls are messages
Each task gets two mailboxes at creation- Kernel and Notify
Only three system calls needed for message transfer
msg_send(), msg_receive(), msg_rpc()
Mailboxes needed for commuication, created via
port_allocate()
Send and receive are flexible, for example four options if mailbox full:
Wait indefinitely
Wait at most n milliseconds
Return immediately
Temporarily cache a message
Operating System Concepts – 9th Edition 3.50 Silberschatz, Galvin and Gagne ©2013
Examples of IPC Systems – Windows
Operating System Concepts – 9th Edition 3.51 Silberschatz, Galvin and Gagne ©2013
Local Procedure Calls in Windows
Operating System Concepts – 9th Edition 3.52 Silberschatz, Galvin and Gagne ©2013
Communications in Client-Server Systems
Sockets
Remote Procedure Calls
Pipes
Remote Method Invocation (Java)
Operating System Concepts – 9th Edition 3.53 Silberschatz, Galvin and Gagne ©2013
Sockets
All ports below 1024 are well known, used for standard
services
Operating System Concepts – 9th Edition 3.54 Silberschatz, Galvin and Gagne ©2013
Socket Communication
Operating System Concepts – 9th Edition 3.55 Silberschatz, Galvin and Gagne ©2013
Sockets in Java
Operating System Concepts – 9th Edition 3.56 Silberschatz, Galvin and Gagne ©2013
Remote Procedure Calls
Remote procedure call (RPC) abstracts procedure calls
between processes on networked systems
Again uses ports for service differentiation
Stubs – client-side proxy for the actual procedure on the
server
The client-side stub locates the server and marshalls the
parameters
The server-side stub receives this message, unpacks the
marshalled parameters, and performs the procedure on the
server
On Windows, stub code compile from specification written in
Microsoft Interface Definition Language (MIDL)
Operating System Concepts – 9th Edition 3.57 Silberschatz, Galvin and Gagne ©2013
Remote Procedure Calls (Cont.)
Data representation handled via External Data
Representation (XDL) format to account for different
architectures
Big-endian and little-endian
Remote communication has more failure scenarios than local
Messages can be delivered exactly once rather than at
most once
OS typically provides a rendezvous (or matchmaker) service
to connect client and server
Operating System Concepts – 9th Edition 3.58 Silberschatz, Galvin and Gagne ©2013
Execution of RPC
Operating System Concepts – 9th Edition 3.59 Silberschatz, Galvin and Gagne ©2013
Pipes
Acts as a conduit allowing two processes to communicate
Issues:
Is communication unidirectional or bidirectional?
In the case of two-way communication, is it half or full-
duplex?
Must there exist a relationship (i.e., parent-child) between
the communicating processes?
Can the pipes be used over a network?
Ordinary pipes – cannot be accessed from outside the process
that created it. Typically, a parent process creates a pipe and
uses it to communicate with a child process that it created.
Named pipes – can be accessed without a parent-child
relationship.
Operating System Concepts – 9th Edition 3.60 Silberschatz, Galvin and Gagne ©2013
Ordinary Pipes
Operating System Concepts – 9th Edition 3.61 Silberschatz, Galvin and Gagne ©2013
Named Pipes
Operating System Concepts – 9th Edition 3.62 Silberschatz, Galvin and Gagne ©2013
End of Chapter 3
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 6: CPU Scheduling
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 6: CPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling
Real-Time CPU Scheduling
Operating Systems Examples
Algorithm Evaluation
Operating System Concepts – 9th Edition 6.2 Silberschatz, Galvin and Gagne ©2013
Objectives
Operating System Concepts – 9th Edition 6.3 Silberschatz, Galvin and Gagne ©2013
Basic Concepts
Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne ©2013
Histogram of CPU-burst Times
Operating System Concepts – 9th Edition 6.5 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
Short-term scheduler selects from among the processes in
ready queue, and allocates the CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne ©2013
Dispatcher
Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne ©2013
Scheduling Criteria
Operating System Concepts – 9th Edition 6.8 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 9th Edition 6.9 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 6.10 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
Operating System Concepts – 9th Edition 6.11 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest
time
SJF is optimal – gives minimum average waiting time for a given
set of processes
The difficulty is knowing the length of the next CPU request
Could ask the user
Operating System Concepts – 9th Edition 6.12 Silberschatz, Galvin and Gagne ©2013
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne ©2013
Determining Length of Next CPU Burst
Can only estimate the length – should be similar to the previous one
Then pick process with shortest predicted next CPU burst
Operating System Concepts – 9th Edition 6.14 Silberschatz, Galvin and Gagne ©2013
Prediction of the Length of the Next CPU Burst
Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne ©2013
Examples of Exponential Averaging
α =0
τn+1 = τn
Recent history does not count
α =1
τn+1 = α tn
Only the actual last CPU burst counts
If we expand the formula, we get:
τn+1 = α tn+(1 - α)α tn -1 + …
+(1 - α )j α tn -j + …
+(1 - α )n +1 τ0
Operating System Concepts – 9th Edition 6.16 Silberschatz, Galvin and Gagne ©2013
Example of Shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating System Concepts – 9th Edition 6.17 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
Operating System Concepts – 9th Edition 6.18 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling
P1 P2 P1 P3 P4
0 1 6 16 18 19
Operating System Concepts – 9th Edition 6.19 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)
Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time in
chunks of at most q time units at once. No process waits more
than (n-1)q time units.
Timer interrupts every quantum to schedule next process
Performance
q large ⇒ FIFO
q small ⇒ q must be large with respect to context switch,
otherwise overhead is too high
Operating System Concepts – 9th Edition 6.20 Silberschatz, Galvin and Gagne ©2013
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Operating System Concepts – 9th Edition 6.21 Silberschatz, Galvin and Gagne ©2013
Time Quantum and Context Switch Time
Operating System Concepts – 9th Edition 6.22 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With The Time Quantum
Operating System Concepts – 9th Edition 6.23 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue
Ready queue is partitioned into separate queues, eg:
foreground (interactive)
background (batch)
Process permanently in a given queue
Each queue has its own scheduling algorithm:
foreground – RR
background – FCFS
Scheduling must be done between the queues:
Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR
20% to background in FCFS
Operating System Concepts – 9th Edition 6.24 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling
Operating System Concepts – 9th Edition 6.25 Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue
Operating System Concepts – 9th Edition 6.26 Silberschatz, Galvin and Gagne ©2013
Example of Multilevel Feedback Queue
Three queues:
Q0 – RR with time quantum 8
milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is
served FCFS
When it gains CPU, job receives 8
milliseconds
If it does not finish in 8
milliseconds, job is moved to
queue Q1
At Q1 job is again served FCFS and
receives 16 additional milliseconds
If it still does not complete, it is
preempted and moved to queue Q2
Operating System Concepts – 9th Edition 6.27 Silberschatz, Galvin and Gagne ©2013
Thread Scheduling
Operating System Concepts – 9th Edition 6.28 Silberschatz, Galvin and Gagne ©2013
Pthread Scheduling
Operating System Concepts – 9th Edition 6.29 Silberschatz, Galvin and Gagne ©2013
Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[]) {
int i, scope;
pthread_t tid[NUM THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* first inquire on the current scope */
if (pthread_attr_getscope(&attr, &scope) != 0)
fprintf(stderr, "Unable to get scheduling scope\n");
else {
if (scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS");
else if (scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM");
else
fprintf(stderr, "Illegal scope value.\n");
}
Operating System Concepts – 9th Edition 6.30 Silberschatz, Galvin and Gagne ©2013
Pthread Scheduling API
/* set the scheduling algorithm to PCS or SCS */
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* create the threads */
for (i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i],&attr,runner,NULL);
/* now join on each thread */
for (i = 0; i < NUM_THREADS; i++)
pthread_join(tid[i], NULL);
}
/* Each thread will begin control in this function */
void *runner(void *param)
{
/* do some work ... */
pthread_exit(0);
}
Operating System Concepts – 9th Edition 6.31 Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are
available
Homogeneous processors within a multiprocessor
Asymmetric multiprocessing – only one processor accesses
the system data structures, alleviating the need for data sharing
Symmetric multiprocessing (SMP) – each processor is self-
scheduling, all processes in common ready queue, or each has
its own private queue of ready processes
Currently, most common
Processor affinity – process has affinity for processor on which
it is currently running
soft affinity
hard affinity
Variations including processor sets
Operating System Concepts – 9th Edition 6.32 Silberschatz, Galvin and Gagne ©2013
NUMA and CPU Scheduling
Operating System Concepts – 9th Edition 6.33 Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling – Load Balancing
Operating System Concepts – 9th Edition 6.34 Silberschatz, Galvin and Gagne ©2013
Multicore Processors
Operating System Concepts – 9th Edition 6.35 Silberschatz, Galvin and Gagne ©2013
Multithreaded Multicore System
Operating System Concepts – 9th Edition 6.36 Silberschatz, Galvin and Gagne ©2013
Real-Time CPU Scheduling
Can present obvious
challenges
Soft real-time systems – no
guarantee as to when critical
real-time process will be
scheduled
Hard real-time systems –
task must be serviced by its
deadline
Two types of latencies affect
performance
1. Interrupt latency – time from
arrival of interrupt to start of
routine that services interrupt
2. Dispatch latency – time for
schedule to take current process
off CPU and switch to another
Operating System Concepts – 9th Edition 6.37 Silberschatz, Galvin and Gagne ©2013
Real-Time CPU Scheduling (Cont.)
Conflict phase of
dispatch latency:
1. Preemption of
any process
running in kernel
mode
2. Release by low-
priority process
of resources
needed by high-
priority
processes
Operating System Concepts – 9th Edition 6.38 Silberschatz, Galvin and Gagne ©2013
Priority-based Scheduling
For real-time scheduling, scheduler must support preemptive, priority-
based scheduling
But only guarantees soft real-time
Operating System Concepts – 9th Edition 6.39 Silberschatz, Galvin and Gagne ©2013
Virtualization and Scheduling
Virtualization software schedules multiple guests onto
CPU(s)
Each guest doing its own scheduling
Not knowing it doesn’t own the CPUs
Can result in poor response time
Can effect time-of-day clocks in guests
Can undo good scheduling algorithm efforts of guests
Operating System Concepts – 9th Edition 6.40 Silberschatz, Galvin and Gagne ©2013
Rate Montonic Scheduling
Operating System Concepts – 9th Edition 6.41 Silberschatz, Galvin and Gagne ©2013
Missed Deadlines with Rate Monotonic Scheduling
Operating System Concepts – 9th Edition 6.42 Silberschatz, Galvin and Gagne ©2013
Earliest Deadline First Scheduling (EDF)
Operating System Concepts – 9th Edition 6.43 Silberschatz, Galvin and Gagne ©2013
Proportional Share Scheduling
Operating System Concepts – 9th Edition 6.44 Silberschatz, Galvin and Gagne ©2013
POSIX Real-Time Scheduling
The POSIX.1b standard
API provides functions for managing real-time threads
Defines two scheduling classes for real-time threads:
1. SCHED_FIFO - threads are scheduled using a FCFS strategy with a
FIFO queue. There is no time-slicing for threads of equal priority
2. SCHED_RR - similar to SCHED_FIFO except time-slicing occurs for
threads of equal priority
Defines two functions for getting and setting scheduling policy:
1. pthread_attr_getsched_policy(pthread_attr_t *attr,
int *policy)
2. pthread_attr_setsched_policy(pthread_attr_t *attr,
int policy)
Operating System Concepts – 9th Edition 6.45 Silberschatz, Galvin and Gagne ©2013
POSIX Real-Time Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[])
{
int i, policy;
pthread_t_tid[NUM_THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* get the current scheduling policy */
if (pthread_attr_getschedpolicy(&attr, &policy) != 0)
fprintf(stderr, "Unable to get policy.\n");
else {
if (policy == SCHED_OTHER) printf("SCHED_OTHER\n");
else if (policy == SCHED_RR) printf("SCHED_RR\n");
else if (policy == SCHED_FIFO) printf("SCHED_FIFO\n");
}
Operating System Concepts – 9th Edition 6.46 Silberschatz, Galvin and Gagne ©2013
POSIX Real-Time Scheduling API (Cont.)
Operating System Concepts – 9th Edition 6.47 Silberschatz, Galvin and Gagne ©2013
Operating System Examples
Linux scheduling
Windows scheduling
Solaris scheduling
Operating System Concepts – 9th Edition 6.48 Silberschatz, Galvin and Gagne ©2013
Linux Scheduling Through Version 2.5
Operating System Concepts – 9th Edition 6.49 Silberschatz, Galvin and Gagne ©2013
Linux Scheduling in Version 2.6.23 +
Operating System Concepts – 9th Edition 6.50 Silberschatz, Galvin and Gagne ©2013
CFS Performance
Operating System Concepts – 9th Edition 6.51 Silberschatz, Galvin and Gagne ©2013
Linux Scheduling (Cont.)
Real-time scheduling according to POSIX.1b
Real-time tasks have static priorities
Real-time plus normal map into global priority scheme
Nice value of -20 maps to global priority 100
Nice value of +19 maps to priority 139
Operating System Concepts – 9th Edition 6.52 Silberschatz, Galvin and Gagne ©2013
Windows Scheduling
Windows uses priority-based preemptive scheduling
Highest-priority thread runs next
Dispatcher is scheduler
Thread runs until (1) blocks, (2) uses time slice, (3)
preempted by higher-priority thread
Real-time threads can preempt non-real-time
32-level priority scheme
Variable class is 1-15, real-time class is 16-31
Priority 0 is memory-management thread
Queue for each priority
If no run-able thread, runs idle thread
Operating System Concepts – 9th Edition 6.53 Silberschatz, Galvin and Gagne ©2013
Windows Priority Classes
Win32 API identifies several priority classes to which a process can belong
REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,
ABOVE_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS,
BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS
All are variable except REALTIME
A thread within a given priority class has a relative priority
TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL, BELOW_NORMAL,
LOWEST, IDLE
Priority class and relative priority combine to give numeric priority
Base priority is NORMAL within the class
If quantum expires, priority lowered, but never below base
Operating System Concepts – 9th Edition 6.54 Silberschatz, Galvin and Gagne ©2013
Windows Priority Classes (Cont.)
Operating System Concepts – 9th Edition 6.55 Silberschatz, Galvin and Gagne ©2013
Windows Priorities
Operating System Concepts – 9th Edition 6.56 Silberschatz, Galvin and Gagne ©2013
Solaris
Priority-based scheduling
Six classes available
Time sharing (default) (TS)
Interactive (IA)
Real time (RT)
System (SYS)
Fair Share (FSS)
Fixed priority (FP)
Given thread can be in one class at a time
Each class has its own scheduling algorithm
Time sharing is multi-level feedback queue
Loadable table configurable by sysadmin
Operating System Concepts – 9th Edition 6.57 Silberschatz, Galvin and Gagne ©2013
Solaris Dispatch Table
Operating System Concepts – 9th Edition 6.58 Silberschatz, Galvin and Gagne ©2013
Solaris Scheduling
Operating System Concepts – 9th Edition 6.59 Silberschatz, Galvin and Gagne ©2013
Solaris Scheduling (Cont.)
Scheduler converts class-specific priorities into a per-thread global
priority
Thread with highest priority runs next
Runs until (1) blocks, (2) uses time slice, (3) preempted by
higher-priority thread
Multiple threads at same priority selected via RR
Operating System Concepts – 9th Edition 6.60 Silberschatz, Galvin and Gagne ©2013
Algorithm Evaluation
How to select CPU-scheduling algorithm for an OS?
Determine criteria, then evaluate algorithms
Deterministic modeling
Type of analytic evaluation
Takes a particular predetermined workload and defines the
performance of each algorithm for that workload
Consider 5 processes arriving at time 0:
Operating System Concepts – 9th Edition 6.61 Silberschatz, Galvin and Gagne ©2013
Deterministic Evaluation
RR is 23ms:
Operating System Concepts – 9th Edition 6.62 Silberschatz, Galvin and Gagne ©2013
Queueing Models
Describes the arrival of processes, and CPU and I/O bursts
probabilistically
Commonly exponential, and described by mean
Computes average throughput, utilization, waiting time, etc
Computer system described as network of servers, each with
queue of waiting processes
Knowing arrival rates and service rates
Computes utilization, average queue length, average wait
time, etc
Operating System Concepts – 9th Edition 6.63 Silberschatz, Galvin and Gagne ©2013
Little’s Formula
n = average queue length
W = average waiting time in queue
λ = average arrival rate into queue
Little’s law – in steady state, processes leaving queue must equal
processes arriving, thus:
n=λxW
Valid for any scheduling algorithm and arrival distribution
For example, if on average 7 processes arrive per second, and
normally 14 processes in queue, then average wait time per
process = 2 seconds
Operating System Concepts – 9th Edition 6.64 Silberschatz, Galvin and Gagne ©2013
Simulations
Queueing models limited
Simulations more accurate
Programmed model of computer system
Clock is a variable
Gather statistics indicating algorithm performance
Data to drive simulation gathered via
Random number generator according to probabilities
Distributions defined mathematically or empirically
Trace tapes record sequences of real events in real systems
Operating System Concepts – 9th Edition 6.65 Silberschatz, Galvin and Gagne ©2013
Evaluation of CPU Schedulers by Simulation
Operating System Concepts – 9th Edition 6.66 Silberschatz, Galvin and Gagne ©2013
Implementation
Operating System Concepts – 9th Edition 6.67 Silberschatz, Galvin and Gagne ©2013
End of Chapter 6
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 7: Deadlocks
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 7: Deadlocks
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Operating System Concepts – 9th Edition 7.2 Silberschatz, Galvin and Gagne ©2013
Chapter Objectives
Operating System Concepts – 9th Edition 7.3 Silberschatz, Galvin and Gagne ©2013
System Model
System consists of resources
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request
use
release
Operating System Concepts – 9th Edition 7.4 Silberschatz, Galvin and Gagne ©2013
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Operating System Concepts – 9th Edition 7.5 Silberschatz, Galvin and Gagne ©2013
Deadlock with Mutex Locks
Deadlocks can occur via system calls, locking, etc.
See example box in text page 318 for mutex deadlock
Operating System Concepts – 9th Edition 7.6 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes
in the system
Operating System Concepts – 9th Edition 7.7 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Operating System Concepts – 9th Edition 7.8 Silberschatz, Galvin and Gagne ©2013
Example of a Resource Allocation Graph
Operating System Concepts – 9th Edition 7.9 Silberschatz, Galvin and Gagne ©2013
Resource Allocation Graph With A Deadlock
Operating System Concepts – 9th Edition 7.10 Silberschatz, Galvin and Gagne ©2013
Graph With A Cycle But No Deadlock
Operating System Concepts – 9th Edition 7.11 Silberschatz, Galvin and Gagne ©2013
Basic Facts
Operating System Concepts – 9th Edition 7.12 Silberschatz, Galvin and Gagne ©2013
Methods for Handling Deadlocks
Operating System Concepts – 9th Edition 7.13 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention
Restrain the ways request can be made
Operating System Concepts – 9th Edition 7.14 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention (Cont.)
No Preemption –
If a process that is holding some resources requests
another resource that cannot be immediately allocated to
it, then all resources currently being held are released
Preempted resources are added to the list of resources
for which the process is waiting
Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting
Circular Wait – impose a total ordering of all resource types,
and require that each process requests resources in an
increasing order of enumeration
Operating System Concepts – 9th Edition 7.15 Silberschatz, Galvin and Gagne ©2013
Deadlock Example
/* thread one runs in this function */
void *do_work_one(void *param)
{
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
/** * Do some work */
pthread_mutex_unlock(&second_mutex);
pthread_mutex_unlock(&first_mutex);
pthread_exit(0);
}
/* thread two runs in this function */
void *do_work_two(void *param)
{
pthread_mutex_lock(&second_mutex);
pthread_mutex_lock(&first_mutex);
/** * Do some work */
pthread_mutex_unlock(&first_mutex);
pthread_mutex_unlock(&second_mutex);
pthread_exit(0);
}
Operating System Concepts – 9th Edition 7.16 Silberschatz, Galvin and Gagne ©2013
Deadlock Example with Lock Ordering
void transaction(Account from, Account to, double amount)
{
mutex lock1, lock2;
lock1 = get_lock(from);
lock2 = get_lock(to);
acquire(lock1);
acquire(lock2);
withdraw(from, amount);
deposit(to, amount);
release(lock2);
release(lock1);
}
Operating System Concepts – 9th Edition 7.17 Silberschatz, Galvin and Gagne ©2013
Deadlock Avoidance
Requires that the system has some additional a priori information
available
Simplest and most useful model requires that each process
declare the maximum number of resources of each type
that it may need
The deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can never
be a circular-wait condition
Resource-allocation state is defined by the number of
available and allocated resources, and the maximum
demands of the processes
Operating System Concepts – 9th Edition 7.18 Silberschatz, Galvin and Gagne ©2013
Safe State
Operating System Concepts – 9th Edition 7.19 Silberschatz, Galvin and Gagne ©2013
Basic Facts
Operating System Concepts – 9th Edition 7.20 Silberschatz, Galvin and Gagne ©2013
Safe, Unsafe, Deadlock State
Operating System Concepts – 9th Edition 7.21 Silberschatz, Galvin and Gagne ©2013
Avoidance Algorithms
Operating System Concepts – 9th Edition 7.22 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Scheme
Claim edge Pi → Rj indicated that process Pj may request
resource Rj; represented by a dashed line
Claim edge converts to request edge when a process requests
a resource
Request edge converted to an assignment edge when the
resource is allocated to the process
When a resource is released by a process, assignment edge
reconverts to a claim edge
Resources must be claimed a priori in the system
Operating System Concepts – 9th Edition 7.23 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph
Operating System Concepts – 9th Edition 7.24 Silberschatz, Galvin and Gagne ©2013
Unsafe State In Resource-Allocation Graph
Operating System Concepts – 9th Edition 7.25 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Algorithm
Operating System Concepts – 9th Edition 7.26 Silberschatz, Galvin and Gagne ©2013
Banker’s Algorithm
Multiple instances
Operating System Concepts – 9th Edition 7.27 Silberschatz, Galvin and Gagne ©2013
Data Structures for the Banker’s Algorithm
Operating System Concepts – 9th Edition 7.28 Silberschatz, Galvin and Gagne ©2013
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
4. If Finish [i] == true for all i, then the system is in a safe state
Operating System Concepts – 9th Edition 7.29 Silberschatz, Galvin and Gagne ©2013
Resource-Request Algorithm for Process Pi
Operating System Concepts – 9th Edition 7.30 Silberschatz, Galvin and Gagne ©2013
Example of Banker’s Algorithm
Operating System Concepts – 9th Edition 7.31 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
The content of the matrix Need is defined to be Max – Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
Operating System Concepts – 9th Edition 7.32 Silberschatz, Galvin and Gagne ©2013
Example: P1 Request (1,0,2)
Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true
Allocation Need Available
ABC ABC ABC
P0 010 743 230
P1 302 020
P2 302 600
P3 211 011
P4 002 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
Operating System Concepts – 9th Edition 7.33 Silberschatz, Galvin and Gagne ©2013
Deadlock Detection
Detection algorithm
Recovery scheme
Operating System Concepts – 9th Edition 7.34 Silberschatz, Galvin and Gagne ©2013
Single Instance of Each Resource Type
Operating System Concepts – 9th Edition 7.35 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph and Wait-for Graph
Operating System Concepts – 9th Edition 7.36 Silberschatz, Galvin and Gagne ©2013
Several Instances of a Resource Type
Available: A vector of length m indicates the number of
available resources of each type
Allocation: An n x m matrix defines the number of resources
of each type currently allocated to each process
Request: An n x m matrix indicates the current request of
each process. If Request [i][j] = k, then process Pi is
requesting k more instances of resource type Rj.
Operating System Concepts – 9th Edition 7.37 Silberschatz, Galvin and Gagne ©2013
Detection Algorithm
Operating System Concepts – 9th Edition 7.38 Silberschatz, Galvin and Gagne ©2013
Detection Algorithm (Cont.)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
Operating System Concepts – 9th Edition 7.39 Silberschatz, Galvin and Gagne ©2013
Example of Detection Algorithm
Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Operating System Concepts – 9th Edition 7.40 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
State of system?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4
Operating System Concepts – 9th Edition 7.41 Silberschatz, Galvin and Gagne ©2013
Detection-Algorithm Usage
When, and how often, to invoke depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
one for each disjoint cycle
Operating System Concepts – 9th Edition 7.42 Silberschatz, Galvin and Gagne ©2013
Recovery from Deadlock: Process Termination
Operating System Concepts – 9th Edition 7.43 Silberschatz, Galvin and Gagne ©2013
Recovery from Deadlock: Resource Preemption
Operating System Concepts – 9th Edition 7.44 Silberschatz, Galvin and Gagne ©2013
End of Chapter 7
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 8: Main Memory
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 8: Memory Management
Background
Swapping
Contiguous Memory Allocation
Segmentation
Paging
Structure of the Page Table
Example: The Intel 32 and 64-bit Architectures
Example: ARM Architecture
Operating System Concepts – 9th Edition 8.2 Silberschatz, Galvin and Gagne ©2013
Objectives
Operating System Concepts – 9th Edition 8.3 Silberschatz, Galvin and Gagne ©2013
Background
Operating System Concepts – 9th Edition 8.4 Silberschatz, Galvin and Gagne ©2013
Base and Limit Registers
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to
be sure it is between base and limit for that user
Operating System Concepts – 9th Edition 8.5 Silberschatz, Galvin and Gagne ©2013
Hardware Address Protection
Operating System Concepts – 9th Edition 8.6 Silberschatz, Galvin and Gagne ©2013
Address Binding
Programs on disk, ready to be brought into memory to execute form an
input queue
Without support, must be loaded into address 0000
Inconvenient to have first user process physical address always at 0000
How can it not be?
Further, addresses represented in different ways at different stages of a
program’s life
Source code addresses usually symbolic
Compiled code addresses bind to relocatable addresses
i.e. “14 bytes from beginning of this module”
Linker or loader will bind relocatable addresses to absolute addresses
i.e. 74014
Each binding maps one address space to another
Operating System Concepts – 9th Edition 8.7 Silberschatz, Galvin and Gagne ©2013
Binding of Instructions and Data to Memory
Operating System Concepts – 9th Edition 8.8 Silberschatz, Galvin and Gagne ©2013
Multistep Processing of a User Program
Operating System Concepts – 9th Edition 8.9 Silberschatz, Galvin and Gagne ©2013
Logical vs. Physical Address Space
Operating System Concepts – 9th Edition 8.10 Silberschatz, Galvin and Gagne ©2013
Memory-Management Unit (MMU)
Hardware device that at run time maps virtual to physical
address
Many methods possible, covered in the rest of this chapter
To start, consider simple scheme where the value in the
relocation register is added to every address generated by a
user process at the time it is sent to memory
Base register now called relocation register
MS-DOS on Intel 80x86 used 4 relocation registers
The user program deals with logical addresses; it never sees the
real physical addresses
Execution-time binding occurs when reference is made to
location in memory
Logical address bound to physical addresses
Operating System Concepts – 9th Edition 8.11 Silberschatz, Galvin and Gagne ©2013
Dynamic relocation using a relocation register
Operating System Concepts – 9th Edition 8.12 Silberschatz, Galvin and Gagne ©2013
Dynamic Linking
Static linking – system libraries and program code combined by
the loader into the binary program image
Dynamic linking –linking postponed until execution time
Small piece of code, stub, used to locate the appropriate
memory-resident library routine
Stub replaces itself with the address of the routine, and executes
the routine
Operating system checks if routine is in processes’ memory
address
If not in address space, add to address space
Dynamic linking is particularly useful for libraries
System also known as shared libraries
Consider applicability to patching system libraries
Versioning may be needed
Operating System Concepts – 9th Edition 8.13 Silberschatz, Galvin and Gagne ©2013
Swapping
A process can be swapped temporarily out of memory to a
backing store, and then brought back into memory for continued
execution
Total physical memory space of processes can exceed
physical memory
Backing store – fast disk large enough to accommodate copies
of all memory images for all users; must provide direct access to
these memory images
Roll out, roll in – swapping variant used for priority-based
scheduling algorithms; lower-priority process is swapped out so
higher-priority process can be loaded and executed
Major part of swap time is transfer time; total transfer time is
directly proportional to the amount of memory swapped
System maintains a ready queue of ready-to-run processes
which have memory images on disk
Operating System Concepts – 9th Edition 8.14 Silberschatz, Galvin and Gagne ©2013
Swapping (Cont.)
Does the swapped out process need to swap back in to same
physical addresses?
Depends on address binding method
Plus consider pending I/O to / from process memory space
Modified versions of swapping are found on many systems (i.e.,
UNIX, Linux, and Windows)
Swapping normally disabled
Started if more than threshold amount of memory allocated
Disabled again once memory demand reduced below
threshold
Operating System Concepts – 9th Edition 8.15 Silberschatz, Galvin and Gagne ©2013
Schematic View of Swapping
Operating System Concepts – 9th Edition 8.16 Silberschatz, Galvin and Gagne ©2013
Context Switch Time including Swapping
Operating System Concepts – 9th Edition 8.17 Silberschatz, Galvin and Gagne ©2013
Context Switch Time and Swapping (Cont.)
Operating System Concepts – 9th Edition 8.18 Silberschatz, Galvin and Gagne ©2013
Swapping on Mobile Systems
Not typically supported
Flash memory based
Small amount of space
Limited number of write cycles
Poor throughput between flash memory and CPU on mobile
platform
Instead use other methods to free memory if low
iOS asks apps to voluntarily relinquish allocated memory
Read-only data thrown out and reloaded from flash if needed
Failure to free can result in termination
Android terminates apps if low free memory, but first writes
application state to flash for fast restart
Both OSes support paging as discussed below
Operating System Concepts – 9th Edition 8.19 Silberschatz, Galvin and Gagne ©2013
Contiguous Allocation
Main memory must support both OS and user processes
Limited resource, must allocate efficiently
Contiguous allocation is one early method
Main memory usually into two partitions:
Resident operating system, usually held in low memory with
interrupt vector
User processes then held in high memory
Each process contained in single contiguous section of
memory
Operating System Concepts – 9th Edition 8.20 Silberschatz, Galvin and Gagne ©2013
Contiguous Allocation (Cont.)
Relocation registers used to protect user processes from each
other, and from changing operating-system code and data
Base register contains value of smallest physical address
Limit register contains range of logical addresses – each
logical address must be less than the limit register
MMU maps logical address dynamically
Can then allow actions such as kernel code being transient
and kernel changing size
Operating System Concepts – 9th Edition 8.21 Silberschatz, Galvin and Gagne ©2013
Hardware Support for Relocation and Limit Registers
Operating System Concepts – 9th Edition 8.22 Silberschatz, Galvin and Gagne ©2013
Multiple-partition allocation
Multiple-partition allocation
Degree of multiprogramming limited by number of partitions
Variable-partition sizes for efficiency (sized to a given process’ needs)
Hole – block of available memory; holes of various size are scattered
throughout memory
When a process arrives, it is allocated memory from a hole large enough to
accommodate it
Process exiting frees its partition, adjacent free partitions combined
Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
Operating System Concepts – 9th Edition 8.23 Silberschatz, Galvin and Gagne ©2013
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?
Worst-fit: Allocate the largest hole; must also search entire list
Produces the largest leftover hole
First-fit and best-fit better than worst-fit in terms of speed and storage
utilization
Operating System Concepts – 9th Edition 8.24 Silberschatz, Galvin and Gagne ©2013
Fragmentation
External Fragmentation – total memory space exists to
satisfy a request, but it is not contiguous
Internal Fragmentation – allocated memory may be slightly
larger than requested memory; this size difference is memory
internal to a partition, but not being used
First fit analysis reveals that given N blocks allocated, 0.5 N
blocks lost to fragmentation
1/3 may be unusable -> 50-percent rule
Operating System Concepts – 9th Edition 8.25 Silberschatz, Galvin and Gagne ©2013
Fragmentation (Cont.)
Operating System Concepts – 9th Edition 8.26 Silberschatz, Galvin and Gagne ©2013
Segmentation
Memory-management scheme that supports user view of memory
A program is a collection of segments
A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
Operating System Concepts – 9th Edition 8.27 Silberschatz, Galvin and Gagne ©2013
User’s View of a Program
Operating System Concepts – 9th Edition 8.28 Silberschatz, Galvin and Gagne ©2013
Logical View of Segmentation
4
1
3 2
4
Operating System Concepts – 9th Edition 8.29 Silberschatz, Galvin and Gagne ©2013
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset>,
Operating System Concepts – 9th Edition 8.30 Silberschatz, Galvin and Gagne ©2013
Segmentation Architecture (Cont.)
Protection
With each entry in segment table associate:
validation bit = 0 ⇒ illegal segment
read/write/execute privileges
Protection bits associated with segments; code sharing
occurs at segment level
Since segments vary in length, memory allocation is a
dynamic storage-allocation problem
A segmentation example is shown in the following diagram
Operating System Concepts – 9th Edition 8.31 Silberschatz, Galvin and Gagne ©2013
Segmentation Hardware
Operating System Concepts – 9th Edition 8.32 Silberschatz, Galvin and Gagne ©2013
Paging
Physical address space of a process can be noncontiguous;
process is allocated physical memory whenever the latter is
available
Avoids external fragmentation
Avoids problem of varying sized memory chunks
Divide physical memory into fixed-sized blocks called frames
Size is power of 2, between 512 bytes and 16 Mbytes
Divide logical memory into blocks of same size called pages
Keep track of all free frames
To run a program of size N pages, need to find N free frames and
load program
Set up a page table to translate logical to physical addresses
Backing store likewise split into pages
Still have Internal fragmentation
Operating System Concepts – 9th Edition 8.33 Silberschatz, Galvin and Gagne ©2013
Address Translation Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
Operating System Concepts – 9th Edition 8.34 Silberschatz, Galvin and Gagne ©2013
Paging Hardware
Operating System Concepts – 9th Edition 8.35 Silberschatz, Galvin and Gagne ©2013
Paging Model of Logical and Physical Memory
Operating System Concepts – 9th Edition 8.36 Silberschatz, Galvin and Gagne ©2013
Paging Example
Operating System Concepts – 9th Edition 8.37 Silberschatz, Galvin and Gagne ©2013
Paging (Cont.)
Operating System Concepts – 9th Edition 8.38 Silberschatz, Galvin and Gagne ©2013
Free Frames
Operating System Concepts – 9th Edition 8.39 Silberschatz, Galvin and Gagne ©2013
Implementation of Page Table
Page table is kept in main memory
Page-table base register (PTBR) points to the page table
Page-table length register (PTLR) indicates size of the page
table
In this scheme every data/instruction access requires two
memory accesses
One for the page table and one for the data / instruction
The two memory access problem can be solved by the use of
a special fast-lookup hardware cache called associative
memory or translation look-aside buffers (TLBs)
Operating System Concepts – 9th Edition 8.40 Silberschatz, Galvin and Gagne ©2013
Implementation of Page Table (Cont.)
Some TLBs store address-space identifiers (ASIDs) in each
TLB entry – uniquely identifies each process to provide
address-space protection for that process
Otherwise need to flush at every context switch
TLBs typically small (64 to 1,024 entries)
On a TLB miss, value is loaded into the TLB for faster access
next time
Replacement policies must be considered
Some entries can be wired down for permanent fast
access
Operating System Concepts – 9th Edition 8.41 Silberschatz, Galvin and Gagne ©2013
Associative Memory
Page # Frame #
Operating System Concepts – 9th Edition 8.42 Silberschatz, Galvin and Gagne ©2013
Paging Hardware With TLB
Operating System Concepts – 9th Edition 8.43 Silberschatz, Galvin and Gagne ©2013
Effective Access Time
Associative Lookup = ε time unit
Can be < 10% of memory access time
Hit ratio = α
Hit ratio – percentage of times that a page number is found in the
associative registers; ratio related to number of associative
registers
Consider α = 80%, ε = 20ns for TLB search, 100ns for memory access
Effective Access Time (EAT)
EAT = (1 + ε) α + (2 + ε)(1 – α)
=2+ε–α
Consider α = 80%, ε = 20ns for TLB search, 100ns for memory access
EAT = 0.80 x 100 + 0.20 x 200 = 120ns
Consider more realistic hit ratio -> α = 99%, ε = 20ns for TLB search,
100ns for memory access
EAT = 0.99 x 100 + 0.01 x 200 = 101ns
Operating System Concepts – 9th Edition 8.44 Silberschatz, Galvin and Gagne ©2013
Memory Protection
Memory protection implemented by associating protection bit
with each frame to indicate if read-only or read-write access is
allowed
Can also add more bits to indicate page execute-only, and
so on
Valid-invalid bit attached to each entry in the page table:
“valid” indicates that the associated page is in the
process’ logical address space, and is thus a legal page
“invalid” indicates that the page is not in the process’
logical address space
Or use page-table length register (PTLR)
Any violations result in a trap to the kernel
Operating System Concepts – 9th Edition 8.45 Silberschatz, Galvin and Gagne ©2013
Valid (v) or Invalid (i) Bit In A Page Table
Operating System Concepts – 9th Edition 8.46 Silberschatz, Galvin and Gagne ©2013
Shared Pages
Shared code
One copy of read-only (reentrant) code shared among
processes (i.e., text editors, compilers, window systems)
Similar to multiple threads sharing the same process space
Also useful for interprocess communication if sharing of
read-write pages is allowed
Private code and data
Each process keeps a separate copy of the code and data
The pages for the private code and data can appear
anywhere in the logical address space
Operating System Concepts – 9th Edition 8.47 Silberschatz, Galvin and Gagne ©2013
Shared Pages Example
Operating System Concepts – 9th Edition 8.48 Silberschatz, Galvin and Gagne ©2013
Structure of the Page Table
Memory structures for paging can get huge using straight-
forward methods
Consider a 32-bit logical address space as on modern
computers
Page size of 4 KB (212)
Page table would have 1 million entries (232 / 212)
If each entry is 4 bytes -> 4 MB of physical address space /
memory for page table alone
That amount of memory used to cost a lot
Don’t want to allocate that contiguously in main memory
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
Operating System Concepts – 9th Edition 8.49 Silberschatz, Galvin and Gagne ©2013
Hierarchical Page Tables
Operating System Concepts – 9th Edition 8.50 Silberschatz, Galvin and Gagne ©2013
Two-Level Page-Table Scheme
Operating System Concepts – 9th Edition 8.51 Silberschatz, Galvin and Gagne ©2013
Two-Level Paging Example
A logical address (on 32-bit machine with 1K page size) is divided into:
a page number consisting of 22 bits
a page offset consisting of 10 bits
Since the page table is paged, the page number is further divided into:
a 12-bit page number
a 10-bit page offset
Operating System Concepts – 9th Edition 8.52 Silberschatz, Galvin and Gagne ©2013
Address-Translation Scheme
Operating System Concepts – 9th Edition 8.53 Silberschatz, Galvin and Gagne ©2013
64-bit Logical Address Space
Operating System Concepts – 9th Edition 8.54 Silberschatz, Galvin and Gagne ©2013
Three-level Paging Scheme
Operating System Concepts – 9th Edition 8.55 Silberschatz, Galvin and Gagne ©2013
Hashed Page Tables
Common in address spaces > 32 bits
The virtual page number is hashed into a page table
This page table contains a chain of elements hashing to the same
location
Each element contains (1) the virtual page number (2) the value of the
mapped page frame (3) a pointer to the next element
Virtual page numbers are compared in this chain searching for a
match
If a match is found, the corresponding physical frame is extracted
Variation for 64-bit addresses is clustered page tables
Similar to hashed but each entry refers to several pages (such as
16) rather than 1
Especially useful for sparse address spaces (where memory
references are non-contiguous and scattered)
Operating System Concepts – 9th Edition 8.56 Silberschatz, Galvin and Gagne ©2013
Hashed Page Table
Operating System Concepts – 9th Edition 8.57 Silberschatz, Galvin and Gagne ©2013
Inverted Page Table
Rather than each process having a page table and keeping track
of all possible logical pages, track all physical pages
One entry for each real page of memory
Entry consists of the virtual address of the page stored in that
real memory location, with information about the process that
owns that page
Decreases memory needed to store each page table, but
increases time needed to search the table when a page
reference occurs
Use hash table to limit the search to one — or at most a few —
page-table entries
TLB can accelerate access
But how to implement shared memory?
One mapping of a virtual address to the shared physical
address
Operating System Concepts – 9th Edition 8.58 Silberschatz, Galvin and Gagne ©2013
Inverted Page Table Architecture
Operating System Concepts – 9th Edition 8.59 Silberschatz, Galvin and Gagne ©2013
Oracle SPARC Solaris
Consider modern, 64-bit operating system example with tightly
integrated HW
Goals are efficiency, low overhead
Based on hashing, but more complex
Two hash tables
One kernel and one for all user processes
Each maps memory addresses from virtual to physical memory
Each entry represents a contiguous area of mapped virtual
memory,
More efficient than having a separate hash-table entry for
each page
Each entry has base address and span (indicating the number
of pages the entry represents)
Operating System Concepts – 9th Edition 8.60 Silberschatz, Galvin and Gagne ©2013
Oracle SPARC Solaris (Cont.)
TLB holds translation table entries (TTEs) for fast hardware lookups
A cache of TTEs reside in a translation storage buffer (TSB)
Includes an entry per recently accessed page
Virtual address reference causes TLB search
If miss, hardware walks the in-memory TSB looking for the TTE
corresponding to the address
If match found, the CPU copies the TSB entry into the TLB
and translation completes
If no match found, kernel interrupted to search the hash table
– The kernel then creates a TTE from the appropriate hash
table and stores it in the TSB, Interrupt handler returns
control to the MMU, which completes the address
translation.
Operating System Concepts – 9th Edition 8.61 Silberschatz, Galvin and Gagne ©2013
Example: The Intel 32 and 64-bit Architectures
Operating System Concepts – 9th Edition 8.62 Silberschatz, Galvin and Gagne ©2013
Example: The Intel IA-32 Architecture
Operating System Concepts – 9th Edition 8.63 Silberschatz, Galvin and Gagne ©2013
Example: The Intel IA-32 Architecture (Cont.)
Operating System Concepts – 9th Edition 8.64 Silberschatz, Galvin and Gagne ©2013
Logical to Physical Address Translation in IA-32
Operating System Concepts – 9th Edition 8.65 Silberschatz, Galvin and Gagne ©2013
Intel IA-32 Segmentation
Operating System Concepts – 9th Edition 8.66 Silberschatz, Galvin and Gagne ©2013
Intel IA-32 Paging Architecture
Operating System Concepts – 9th Edition 8.67 Silberschatz, Galvin and Gagne ©2013
Intel IA-32 Page Address Extensions
32-bit address limits led Intel to create page address extension (PAE),
allowing 32-bit apps access to more than 4GB of memory space
Paging went to a 3-level scheme
Top two bits refer to a page directory pointer table
Page-directory and page-table entries moved to 64-bits in size
Net effect is increasing address space to 36 bits – 64GB of physical
memory
Operating System Concepts – 9th Edition 8.68 Silberschatz, Galvin and Gagne ©2013
Intel x86-64
Current generation Intel x86 architecture
64 bits is ginormous (> 16 exabytes)
In practice only implement 48 bit addressing
Page sizes of 4 KB, 2 MB, 1 GB
Four levels of paging hierarchy
Can also use PAE so virtual addresses are 48 bits and physical
addresses are 52 bits
Operating System Concepts – 9th Edition 8.69 Silberschatz, Galvin and Gagne ©2013
Example: ARM Architecture
Dominant mobile platform chip
(Apple iOS and Google Android
devices for example)
Modern, energy efficient, 32-bit
CPU
4 KB and 16 KB pages
1 MB and 16 MB pages (termed
sections)
One-level paging for sections, two-
level for smaller pages
Two levels of TLBs
Outer level has two micro
TLBs (one data, one
instruction)
Inner is single main TLB
First inner is checked, on
miss outers are checked,
and on miss page table
walk performed by CPU
Operating System Concepts – 9th Edition 8.70 Silberschatz, Galvin and Gagne ©2013
End of Chapter 8
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013