OS (New)
OS (New)
Systems
UTTAM K. ROY
Books
• Silberschatz Galvin, “Operating System Concepts”, Addison
Wesley publication
| U. K. Roy | [ ]
Operating Systems 3
| U. K. Roy | [ ]
Operating Systems 4
| U. K. Roy | [ ]
Operating Systems 5
Components (Cont.)
A computer system consists of
hardware
system programs
application programs
| U. K. Roy | [ ]
Operating Systems 6
Components (Cont.)
Computer system components
Hardware – provides basic computing resources
• CPU, memory, I/O devices
Operating system
• Controls and coordinates use of hardware among
various applications and users
Application programs
• Define the ways in which the system resources are
used to solve the computing problems of the users
• Word processors, compilers, web browsers, database
systems, video games
Users
• People, machines, other computers
| U. K. Roy | [ ]
Operating Systems 7
| U. K. Roy | [ ]
Operating Systems 8
| U. K. Roy | [ ]
Operating Systems 9
| U. K. Roy | [ ]
Operating Systems 10
| U. K. Roy | [ ]
Operating Systems 11
Storage Structure
Main memory – only large storage media that the CPU
can access directly.
Secondary storage – extension of main memory that
provides large nonvolatile storage capacity.
Magnetic disks – rigid metal or glass platters covered
with magnetic recording material
Disk surface is logically divided into tracks, which are
subdivided into sectors.
The disk controller determines the logical interaction
between the device and the computer.
| U. K. Roy | [ ]
Operating Systems 12
Storage Hierarchy
| U. K. Roy | [ ]
Operating Systems 13
Storage-Device Hierarchy
| U. K. Roy | [ ]
Operating Systems 14
Caching
| U. K. Roy | [ ]
Operating Systems 15
| U. K. Roy | [ ]
Operating Systems 16
Memory Management
All data in memory before and after processing
All instructions in memory in order to execute
Memory management determines what is in memory
when
Optimizing CPU utilization and computer response to users
Memory management activities
Keeping track of which parts of memory are currently
being used and by whom
Deciding which processes (or parts thereof) and data to
move into and out of memory
Allocating and deallocating memory space as needed
| U. K. Roy | [ ]
Operating Systems 17
Storage Management
OS provides uniform, logical view of information storage
Abstracts physical properties to logical storage unit - file
Each medium is controlled by device (i.e., disk drive, tape
drive)
• Varying properties include access speed, capacity, data-transfer
rate, access method (sequential or random)
File-System management
Files usually organized into directories
Access control on most systems to determine who can access
what
OS activities include
• Creating and deleting files and directories
• Primitives to manipulate files and dirs
• Mapping files onto secondary storage
• Backup files onto stable (non-volatile) storage media
| U. K. Roy | [ ]
Operating Systems 18
Mass-Storage Management
Usually disks used to store data that does not fit in main
memory or data that must be kept for a “long” period of
time.
Proper management is of central importance
Entire speed of computer operation hinges on disk subsystem
and its algorithms
OS activities
Free-space management
Storage allocation
Disk scheduling
Some storage need not be fast
Tertiary storage includes optical storage, magnetic tape
Still must be managed
Varies between WORM (write-once, read-many-times) and RW
(read-write)
| U. K. Roy | [ ]
Operating Systems 19
I/O Subsystem
One purpose of OS is to hide peculiarities of hardware
devices from the user
I/O subsystem responsible for
Memory management of I/O including buffering (storing
data temporarily while it is being transferred), caching
(storing parts of data in faster storage for performance),
spooling (the overlapping of output of one job with input
of other jobs)
General device-driver interface
Drivers for specific hardware devices
| U. K. Roy | [ ]
Operating Systems 20
| U. K. Roy | [ ]
Operating Systems 22
| U. K. Roy | [ ]
Process
Management
Operating Systems 24
Process Management
Process Management
Creating and deleting both user and system processes
Suspending and resuming processes
Providing mechanisms for process synchronization
Providing mechanisms for process communication
Providing mechanisms for deadlock handling
| U. K. Roy | [ ]
Operating Systems 25
Process-Basic Concept
A process is a program in execution.
It is a unit of work within the system.
Program is a passive entity, process is an active entity.
| U. K. Roy | [ ]
Operating Systems 26
Process—Basic Concept
Single-threaded process has one program counter
specifying location of next instruction to execute
Process executes instructions sequentially, one at a time,
until completion
| U. K. Roy | [ ]
Operating Systems 27
Process States
| U. K. Roy | [ ]
Operating Systems 28
Scheduling—Basic Concept
Process Scheduling
Multiprogramming needed for efficiency
Single user cannot keep CPU and I/O devices busy at all
times
Multiprogramming organizes jobs (code and data) so CPU
always has one to execute
A subset of total jobs in system is kept in memory
One job selected and run via job scheduling
When it has to wait (for I/O for example), OS switches to
another job
Context Switch
| U. K. Roy | [ ]
Operating Systems 31
Process Scheduling
Timesharing (multitasking) is logical extension in which
CPU switches jobs so frequently that users can interact
with each job while it is running, creating interactive
computing
Response time should be small enough
Each user has at least one program executing in memory
process
If several jobs ready to run at the same time CPU
scheduling
If processes don’t fit in memory, swapping moves them in
and out to run
Virtual memory allows execution of processes not completely
in memory
| U. K. Roy | [ ]
Operating Systems 32
CPU Scheduling
Basic Concept
Maximum CPU utilization obtained with
multiprogramming
| U. K. Roy | [ ]
Operating Systems 33
Scheduling
| U. K. Roy | [ ]
Operating Systems 34
| U. K. Roy | [ ]
Operating Systems 35
CPU Scheduler
Selects from among the processes in memory that
are ready to execute, and allocates the CPU to one of
them
| U. K. Roy | [ ]
Operating Systems 36
Dispatcher
Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler; this
involves:
switching context
switching to user mode
jumping to the proper location in the user program to
restart that program
| U. K. Roy | [ ]
Operating Systems 37
Scheduling Criteria
CPU utilization
keep the CPU as busy as possible
Throughput
# of processes that complete their execution per time unit
Turnaround time
amount of time to execute a particular process
Waiting time
amount of time a process has been waiting in the ready
queue
Response time
amount of time it takes from when a request was
submitted until the first response is produced, not output
(for time-sharing environment)
| U. K. Roy | [ ]
Operating Systems 38
| U. K. Roy | [ ]
Operating Systems 39
P1 P2 P3
0 24 27 30
| U. K. Roy | [ ]
Operating Systems 40
P2 P3 P1
0 3 6 30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process
| U. K. Roy | [ ]
Operating Systems 41
Two schemes:
nonpreemptive – once CPU given to the process it cannot be
preempted until completes its CPU burst
preemptive – if a new process arrives with CPU burst length
less than remaining time of current executing process,
preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF)
| U. K. Roy | [ ]
Operating Systems 42
0 3 7 8 12 16
| U. K. Roy | [ ]
Operating Systems 43
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
| U. K. Roy | [ ]
Operating Systems 44
| U. K. Roy | [ ]
Operating Systems 45
| U. K. Roy | [ ]
Operating Systems 46
α =1
τn+1 = α tn
Only the actual last CPU burst counts
Priority Scheduling
A priority (an integer) is associated with each process
The CPU is allocated to the process with the highest
priority (smallest integer ≡ highest priority)
Preemptive
Nonpreemptive
SJF is a priority scheduling where priority is the
predicted next CPU burst time
Problem ≡ Starvation – low priority processes may
never execute
Solution ≡ Aging – as time progresses increase the
priority of the process
| U. K. Roy | [ ]
Operating Systems 48
Performance
q large ⇒ FIFO
q small ⇒ q must be large with respect to context switch,
otherwise overhead is too high
| U. K. Roy | [ ]
Operating Systems 49
Example of RR Scheduling
Time quantum=20
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
| U. K. Roy | [ ]
Operating Systems 50
| U. K. Roy | [ ]
Operating Systems 51
| U. K. Roy | [ ]
Operating Systems 52
| U. K. Roy | [ ]
Operating Systems 53
| U. K. Roy | [ ]
Operating Systems 54
| U. K. Roy | [ ]
Operating Systems 55
| U. K. Roy | [ ]
Operating Systems 56
Scheduling
A new job enters queue Q0 which is served RR. When it
gains CPU, job receives 8 milliseconds. If it does not
finish in 8 milliseconds, job is moved to queue Q1.
| U. K. Roy | [ ]
Operating Systems 58
Asymmetric multiprocessing
only one processor accesses the system data structures
Others executes user processes
alleviating the need for data sharing—avoid inconsistency
| U. K. Roy | [ ]
Operating Systems 59
i =1 Pi
≤1
| U. K. Roy | [ ]
Operating Systems 60
Algorithm Evaluation
Criteria used in selecting an algorithm
Maximum CPU utilization under the constraint the maximum
response time is 1 second
Maximum throughput such that average turnaround time
linearly proportional to execution time
Deterministic Modeling
Evaluates using a predetermined work load
• can not cope with the dynamic behavior of the system
Queueing Models
Uses mathematical analysis
• Complicated—hence used unrealistic assumptions
• Uses some assumptions—that may not occur
• An approximation of real system and accuracy is questionable
Simulation
Expensive—takes hours of computer time
| U. K. Roy | [ ]
Process
Synchronization
Operating Systems 62
Process Synchronization
Background
The Critical-Section Problem
Simple Solutions
Solutions using Synchronization Hardware
Semaphores
Solutions of Classic Problems of Synchronization
| U. K. Roy | [ ]
Operating Systems 63
Background
Cooperating Processes
Processes that can affect or be affected by other processes
Implementation uses shared logical address space or shared
data using files
| U. K. Roy | [ ]
Operating Systems 64
Producer-Consumer Problem
Representative of operating system process
| U. K. Roy | [ ]
Operating Systems 65
Race Condition
count++ could be implemented as
register1 = count
register1 = register1 + 1
count = register1
Race condition
Outcome depends upon the order of execution
| U. K. Roy | [ ]
Operating Systems 66
| U. K. Roy | [ ]
Operating Systems 67
<CS>
turn = j;
REMAINDER SECTION
}
Structure of process i
| U. K. Roy | [ ]
Operating Systems 68
while (true) {
…
flag[i]=true;
while (flag[j]) ;
<CS>
flag[i]=false;
REMAINDER SECTION
}
Structure of process i
| U. K. Roy | [ ]
Operating Systems 69
while (true) {
…
flag[i]=true;
turn=j
while (flag[j] && turn==j) ;
<CS>
flag[i]=false;
REMAINDER SECTION
}
Structure of process i
| U. K. Roy | [ ]
Operating Systems 70
Synchronization Hardware
Many systems provide hardware support for critical
section code
TestAndSet Instruction
Definition:
while (true) {
while ( TestAndSet (lock ))
; /* do nothing
<CS>
lock = FALSE;
// remainder section
}
| U. K. Roy | [ ]
Operating Systems 72
Swap Instruction
Definition:
void Swap (bool &a, bool &b) {
bool temp = a;
a = b;
b = temp:
}
Solution
Shared Boolean variable lock initialized to false; Each
process has a local boolean variable key.
Solution:
while (true) {
key = true;
while ( key == true)
Swap (lock, key );
// critical section
lock = false;
// remainder section
}
| U. K. Roy | [ ]
Operating Systems 73
Semaphore
Synchronization tool that does not require busy waiting
Semaphore S – integer variable
Two standard operations modify S: wait() and signal()
Originally called P() and V()
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
Less complicated
Can only be accessed via two indivisible (atomic) operations
| U. K. Roy | [ ]
Operating Systems 74
wait (S);
Critical Section
signal (S);
Requirement:
S2 be executed only after S1
Solution:
Use a a semaphore variable sync initialized to 0
… …
S1 wait(sync)
signal(sync) S2
… …
Process P1 Process P2
| U. K. Roy | [ ]
Operating Systems 76
Semaphore Implementation
Must guarantee that no two processes can execute wait ()
and signal () on the same semaphore at the same time
| U. K. Roy | [ ]
Operating Systems 77
Typical example
struct Semaphore {
int value;
ListOfProcess L;
};
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
| U. K. Roy | [ ]
Operating Systems 81
Bounded-Buffer Problem
| U. K. Roy | [ ]
Operating Systems 82
Bounded-Buffer Problem(Cont.)
The structure of the producer process The structure of the consumer process
| U. K. Roy | [ ]
Operating Systems 83
Readers-Writers Problem
A data set is shared among a number of concurrent
processes
Readers – only read the data set; they do not perform any
updates
Writers – can both read and write.
Shared Data
Data set
Semaphore mutex initialized to 1.
Semaphore wrt initialized to 1.
Integer readcount initialized to 0.
| U. K. Roy | [ ]
Operating Systems 84
Readers-Writer Problem(Cont.)
// reading is performed
signal (wrt) ;
} wait (mutex) ;
readcount - - ;
if (redacount == 0) signal (wrt) ;
signal (mutex) ;
}
| U. K. Roy | [ ]
Operating Systems 85
Dining-Philosophers Problem
• Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
| U. K. Roy | [ ]
Operating Systems 86
Dining-Philosophers Problem(Cont.)
While (true) {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[ (i + 1) % 5] );
signal ( chopstick[i] );
// think
}
| U. K. Roy | [ ]
Operating Systems 87
Dining-Philosophers Problem(Cont.)
Deadlock free solution
Allow at most four philosophers to be sitting simultaneously
at the table
Allow a philosopher to pick up chopsticks only both are
available
While (true) {
wait(mutex)
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
signal(mutex)
// eat
wait (mutex)
signal (chopstick[ (i + 1) % 5] );
signal ( chopstick[i] );
signal(mutex)
// think
}
Asymmetric solution
• Odd philosopher will pick first left then right while even
philosopher picks up first left and then right chopstick
| U. K. Roy | [ ]
Operating Systems 88
| U. K. Roy | [ ]
Deadlocks
Operating Systems 90
Deadlocks
| U. K. Roy | [ ]
Operating Systems 91
Example 3
Each philosopher picks left chopstick in dining philosopher
problem
| U. K. Roy | [ ]
Operating Systems 92
If two cars approach each other at the crossing, neither can start until
the other has gone(Dead lock)
If a deadlock occurs, it can be resolved if one car backs up (preempt
resources and rollback).
Several cars may have to be backed up if a deadlock occurs.
Starvation is possible.
| U. K. Roy | [ ]
Operating Systems 93
System Model
Resource types R1, R2, . . ., Rm
• CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Example—two CPU, Three printers, 2 disk drives
Process may requests as many resources as it requires to
perform its designated task
Must be less the total number of resources available
Each process utilizes a resource as follows:
Request—If the request can not be granted immediately, the
requesting process must wait
Use
Release
| U. K. Roy | [ ]
Operating Systems 94
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion:
at least one resource must in non-sharable mode i.e. only
one process at a time can use that resource.
Hold and wait:
a process holding at least one resource is waiting to acquire
additional resources held by other processes.
No preemption:
a resource can be released only voluntarily by the process
holding it, after that process has completed its task.
Circular wait:
there exists a set {P0, P1, …, Pn} of waiting processes such
that P0 is waiting for a resource that is held by P1, P1 is
waiting for a resource that is held by P2, …, Pn–1 is waiting
for a resource that is held by Pn, and Pn is waiting for a
resource that is held by P0.
| U. K. Roy | [ ]
Operating Systems 95
Resource-Allocation Graph
Used to visualize resource allocation pattern
Helps to describe deadlock more precisely
Resource-Allocation Graph(Cont.)
Process
Pi requests an instance of Rj Pi
Rj
Pi
Pi is holding an instance of Rj Rj
| U. K. Roy | [ ]
Operating Systems 97
Resource Instances
One instance of resource type R1
Two instances of resource type R2
One instance of resource type R3
Three instances of resource type R3
| U. K. Roy | [ ]
Operating Systems 98
Basic facts
| U. K. Roy | [ ]
Operating Systems 99
| U. K. Roy | [ ]
Operating Systems 100
| U. K. Roy | [ ]
Operating Systems 101
| U. K. Roy | [ ]
Operating Systems 102
Deadlock Prevention
• ensure that one of the necessary conditions can not hold
• Restrict the ways request can be made.
Two possibilities
Require process to request and be allocated all its resources before it
begins execution
allow process to request resources only when the process has none.
| U. K. Roy | [ ]
Operating Systems 103
Deadlock Prevention(Cont.)
No Preemption –
Two possibilities
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.
| U. K. Roy | [ ]
Operating Systems 104
Deadlock Prevention(Cont.)
Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration.
F(tape drive)=1
F(disk drive)=5
F(printer)=12
Facts:I
If a process currently holds resource type Ri , the process can request
another resource type Rj if F(Rj) > F(Ri)
OR
If a process requests a resource type Rj , if it has released any resource
type Ri such the F(Ri) ≥ F(Rj)
Proof(by contradiction):
Set of deadlocked process {P0, P1, P2, …, Pn}
F(R0) < F(R1) < F(R2) <…< F(Rn) < F(R0) which is impossible
So there can be no circular wait—no deadlock
| U. K. Roy | [ ]
Operating Systems 105
Deadlock Avoidance
• Requires that the system has some additional information available.
• With this additional information, system can decide whether a newly arrived
request can be satisfied (served) or not
| U. K. Roy | [ ]
Operating Systems 106
Safe State
state is defined by the number of available and allocated
resources, and the maximum demands of the processes.
That is:
If Pi needs some resources that are not immediately available, then
Pi can wait until all Pj have finished.
When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate.
When Pi terminates, Pi +1 can obtain its needed resources, and so on.
| U. K. Roy | [ ]
Operating Systems 107
Basic Facts
If a system is in safe state ⇒ no deadlocks.
| U. K. Roy | [ ]
Operating Systems 109
| U. K. Roy | [ ]
Operating Systems 110
| U. K. Roy | [ ]
Operating Systems 111
| U. K. Roy | [ ]
Operating Systems 112
| U. K. Roy | [ ]
Operating Systems 113
| U. K. Roy | [ ]
Operating Systems 114
Banker’s Algorithm
Multiple instances.
| U. K. Roy | [ ]
Operating Systems 115
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.
Algorithm requires an order of O(m x n2) operations to detect
whether the system is in safe state.
| U. K. Roy | [ ]
Operating Systems 117
Example(Cont.)
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
| U. K. Roy | [ ]
Operating Systems 120
| U. K. Roy | [ ]
Operating Systems 121
Deadlock Detection
Recovery scheme
| U. K. Roy | [ ]
Operating Systems 122
| U. K. Roy | [ ]
Operating Systems 123
ResourceAllocation Graph Corresponding waitfor graph
| U. K. Roy | [ ]
Operating Systems 124
| U. K. Roy | [ ]
Operating Systems 125
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
For i = 1,2, …, n, if Allocationi ≠ 0, then
Finish[i] = false;otherwise, Finish[i] = true.
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all
i.
| U. K. Roy | [ ]
Operating Systems 127
Example(Cont.)
P2 requests an additional instance of type C.
Request
ABC
P0 000
P1 201
P2 001
P3 100
P4 002
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.
| U. K. Roy | [ ]
Operating Systems 128
| U. K. Roy | [ ]
Operating Systems 129
| U. K. Roy | [ ]
Operating Systems 130
| U. K. Roy | [ ]
Memory
Management
Operating Systems 132
Memory Management
Background
Swapping
Contiguous Memory Allocation
Paging
Structure of the Page Table
Inverted page table
Segmentation
Segmentation with paging
| U. K. Roy | [ ]
Operating Systems 133
Background
Memory
large array of words each with its own address
Main memory and registers are only storage CPU can access
directly
Register access in one CPU clock (or less)—fast
Main memory can take many cycles—slower
Cache sits between main memory and CPU registers
Primary purpose of computer system?
execute programs
Program must be brought (from disk) into memory and placed
within a process for it to be run
Want to improve CPU utilization or speed?
Keep several programs ready in the memory
| U. K. Roy | [ ]
Operating Systems 134
| U. K. Roy | [ ]
Operating Systems 135
Address Binding
Address
Symbolic e.g. “count”
Relocatable e.g. “3 bytes from beginning”
Absolute address e.g 01000000
Address binding of instructions and data to memory
addresses can happen at three different stages
Compile time: If memory location known a priori,
absolute code can be generated; must recompile code if
starting location changes
| U. K. Roy | [ ]
Operating Systems 136
Address Binding
content meaning
00000001 code for addition
4 bytes from start address of x
int x=3, y=4, z; 5 bytes from start address of y
z=x+y; 6 bytes from start address of z
00000011 x=3
00000100 y=4
00000000 z=0 (before execution)
| U. K. Roy | [ ]
Operating Systems 137
| U. K. Roy | [ ]
Operating Systems 138
| U. K. Roy | [ ]
Operating Systems 139
| U. K. Roy | [ ]
Operating Systems 140
Dynamic Loading
Advantages
Better memory-space utilization; unused routine is never
loaded
Useful when large amount of code is needed to handle
infrequently occurring cases
No special support from the operating system is required
implemented through program design
| U. K. Roy | [ ]
Operating Systems 141
Dynamic Linking
Linking postponed until execution time
Basic concept
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 needed to check if routine is in
processes’ memory address
Advantages
Dynamic linking is particularly useful for shared libraries
| U. K. Roy | [ ]
Operating Systems 142
Swapping
A process can be swapped temporarily out of memory to a
backing store, and then brought back into memory for continued
execution
| U. K. Roy | [ ]
Operating Systems 143
| U. K. Roy | [ ]
Operating Systems 144
Contiguous Allocation
Main memory usually into two partitions:
Resident operating system, usually held in low memory
with interrupt vector
User processes then held in high memory
| U. K. Roy | [ ]
Operating Systems 145
| U. K. Roy | [ ]
Operating Systems 146
OS OS OS OS
process 8 process 10
| U. K. Roy | [ ]
Operating Systems 147
How to satisfy a request of size n from a list of free holes
Firstfit and bestfit better than worstfit in terms of speed and
storage utilization
| U. K. Roy | [ ]
Operating Systems 148
Fragmentation
External Fragmentation – total memory space exists to
satisfy a request, but it is not contiguous
| U. K. Roy | [ ]
Operating Systems 149
Fragmentation
Compaction algorithm
1000K P4 1000K
P3 P3 900k
1200K 1200K
300k
1500K 1500K
900k 900k
P4 P4
1900K 1900K
200k P3
2100K 2100K 2100K 2100K
| U. K. Roy | [ ]
Operating Systems 150
Paging
Logical address space of a process can be noncontiguous; process
is allocated physical memory whenever the latter is available
Divide physical memory into fixed-sized blocks called frames
(size is power of 2, between 512 bytes and 8,192 bytes)
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
Advantage
Address space need not be contiguous
Problem
Internal fragmentation
| U. K. Roy | [ ]
Operating Systems 151
page number page offset
p d
m n n
| U. K. Roy | [ ]
Operating Systems 152
Paging Hardware
| U. K. Roy | [ ]
Operating Systems 153
| U. K. Roy | [ ]
Operating Systems 154
Paging Example
32byte memory and 4byte pages
| U. K. Roy | [ ]
Operating Systems 155
Free Frames
Before allocation After allocation
| U. K. Roy | [ ]
Operating Systems 156
Problem
In this scheme every data/instruction access requires two memory
accesses. One for the page table and one for the data/instruction.
Solution
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)
TLB stores a fraction of total information of page table
| U. K. Roy | [ ]
Operating Systems 157
Associative Memory
| U. K. Roy | [ ]
Operating Systems 158
| U. K. Roy | [ ]
Operating Systems 159
Temporal locality
Spatial locality
| U. K. Roy | [ ]
Operating Systems 160
| U. K. Roy | [ ]
Operating Systems 161
Memory Protection
Memory protection implemented by associating
protection (read/write) bit with each frame
| U. K. Roy | [ ]
Operating Systems 163
Shared Pages
Shared code
One copy of read-only (reentrant) code shared among
processes (i.e., text editors, compilers, window systems).
The pages for the private code and data can appear
anywhere in the logical address space
| U. K. Roy | [ ]
Operating Systems 164
| U. K. Roy | [ ]
Operating Systems 165
| U. K. Roy | [ ]
Operating Systems 166
10 10 12
| U. K. Roy | [ ]
Operating Systems 167
| U. K. Roy | [ ]
Operating Systems 168
Address-Translation Scheme
| U. K. Roy | [ ]
Operating Systems 169
| U. K. Roy | [ ]
Operating Systems 170
| U. K. Roy | [ ]
Operating Systems 171
| U. K. Roy | [ ]
Operating Systems 172
| U. K. Roy | [ ]
Operating Systems 173
| U. K. Roy | [ ]
Operating Systems 174
Segmentation
Paging does not support user’s view of memory
Segmentation Memory-management scheme supports user
view of memory
User’s view
A program is a collection of segments/
modules. A segment is a logical unit
such as:
• main program,
• procedure,
• function,
• method,
• object,
• local variables, global variables,
• common block,
• stack,
• symbol table, arrays
| U. K. Roy | [ ]
Operating Systems 175
4
1
3 2
4
3
3
user space physical memory space
| U. K. Roy | [ ]
Operating Systems 176
Segmentation Architecture
Logical address consists of a two tuple (for paging a single address is
specified):
<segment-number, offset>,
Segmentation hardware
Segment table – maps two-
dimensional physical
addresses; each table entry is
of the form <base, limit>
• base – contains the starting
physical address where the
segment resides in memory
• limit – specifies the length of
the segment
| U. K. Roy | [ ]
Operating Systems 177
Example of Segmentation
| U. K. Roy | [ ]
Operating Systems 178
Background
Motivation
Programs often handle large error handling code that rarely
occurs
Declare an array of 800x600 elements;most will not be used
Some routines are less frequently used
Virtual memory – separation of logical memory from
physical memory.
Only part of the program needs to be in memory for
execution
Logical address space can therefore be much larger than
physical address space
Allows address spaces to be shared by many processes
Less I/O—allows for more efficient process creation
| U. K. Roy | [ ]
Operating Systems 182
Demand Paging
Bring a page into memory only when it is needed
Less memory needed—size of the program can be
extremely large
Less I/O needed—Faster response
More users
| U. K. Roy | [ ]
Operating Systems 183
| U. K. Roy | [ ]
Operating Systems 184
i
i
page table
| U. K. Roy | [ ]
Operating Systems 185
Example
| U. K. Roy | [ ]
Operating Systems 186
Page Fault
page fault—if referenced page is not in memory
| U. K. Roy | [ ]
Operating Systems 187
| U. K. Roy | [ ]
Operating Systems 188
Page Fault
Restart instruction
block move (MVC instruction in IBM 360/370)
Solution Destination
Block
• Check both ends of both blocks
• Page fault may occur even then Page fault
Source
• Store values of overwritten locations block
• Where to store? Register or memory? If stored in memory,
page fault again?
| U. K. Roy | [ ]
Operating Systems 189
Page Replacement
Page faultThere is no free frame in memorypage
replacement
Page replacement completes separation between logical
memory and physical memory – large virtual memory can
be provided on a smaller physical memory
Include page fault algorithm into page-fault service
routine
find some page in memory, but not really in use, swap
it out
algorithm
performance – want an algorithm which will result in
minimum number of page faults
| U. K. Roy | [ ]
Operating Systems 192
| U. K. Roy | [ ]
Operating Systems 193
Page Replacement
| U. K. Roy | [ ]
Operating Systems 194
| U. K. Roy | [ ]
Operating Systems 195
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 7
| U. K. Roy | [ ]
Operating Systems 196
15 page faults
Implementation:
Use FIFO queue; referenced page will be added at the rear
end
Page at the head will be replaced
| U. K. Roy | [ ]
Operating Systems 197
Belady’s anomaly
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
3 frames (3 pages can be in memory at a time per process)
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 4 4 4 5 5 5
2 2 2 2 1 1 1 3 3 9 page faults
3 3 3 3 2 2 2 4
4 frames
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 5 5 5 5 4 4
2 2 2 2 2 1 1 1 1 5 10 page faults
3 3 3 3 3 2 2 2 2
4 4 4 4 4 3 3 3
| U. K. Roy | [ ]
Operating Systems 198
| U. K. Roy | [ ]
Operating Systems 199
FIFO Page replacement
Optimal Page replacement
9 page faults
| U. K. Roy | [ ]
Operating Systems 200
Optimal Algorithm
4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 4
2 2 2 2 2 2 6 page faults
3 3 3 3 3
4 4 5 5
| U. K. Roy | [ ]
Operating Systems 201
12 page faults
| U. K. Roy | [ ]
Operating Systems 202
1 1 1 1 5
2 2 2 2 2
3 5 5 4 4
4 4 3 3 3
Implementation of LRU
Counter implementation
• Every page entry has a counter; every time page is referenced through
this entry, copy the clock into the counter
• When a page needs to be changed, look at the counters to determine
which are to change
• Problems:
• Write counter to the memory—memory access
• Searching the page with smallest counter value
| U. K. Roy | [ ]
Operating Systems 203
• Remove the page at the bottom/rear(this is the least recently used page)
| U. K. Roy | [ ]
Operating Systems 204
| U. K. Roy | [ ]
Operating Systems 205
| U. K. Roy | [ ]
Operating Systems 206
| U. K. Roy | [ ]
Operating Systems 207
| U. K. Roy | [ ]
Operating Systems 208
Counting Algorithms
Keep a counter of the number of references that
have been made to each page
| U. K. Roy | [ ]
Operating Systems 209
Allocation of Frames
Each process needs minimum number of pages
Two major allocation schemes
fixed allocation
• e.g. Equal allocation – For example, if there are 100
frames and 5 processes, give each process 20
frames.
Proportional allocation – Allocate according to the
size of process
m = 64
si = size of process pi si = 10
S = ∑ si s2 = 127
m = total number of frames 10
a1 = × 64 ≈ 5
s 137
ai = allocation for pi = i × m
S 127
a2 = × 64 ≈ 59
137
| U. K. Roy | [ ]
Operating Systems 210
Priority Allocation
Use a proportional allocation
scheme using priorities rather
than size
| U. K. Roy | [ ]
Operating Systems 211
| U. K. Roy | [ ]
Operating Systems 212
Thrashing
Thrashing ≡ a process is busy swapping pages in and out
If a process does not have “enough” pages, the page-fault
rate is very high. This leads to:
low CPU utilization
operating system thinks that
it needs to increase the
degree of multiprogramming
another process added to the
system
| U. K. Roy | [ ]
Operating Systems 214
Working-Set Model
∆ ≡ working-set window ≡ a fixed number of page references
Example: 10,000 instruction
WSSi (working set of Process Pi) =
total number of pages referenced in the most recent ∆ (varies in time)
D = Σ WSSi ≡ total demand frames
if D > m ⇒ Thrashing
Policy if D > m, then suspend one of the processes
Selection of ∆
if ∆ too small will not encompass entire locality
if ∆ too large will encompass several localities
if ∆ = ∞ ⇒ will encompass entire program
| U. K. Roy | [ ]
Operating Systems 215
| U. K. Roy | [ ]
Operating Systems 216
| U. K. Roy | [ ]
Operating Systems 217
Program 1
Program 2
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i,j] = 0;