Title: Deadlock
1Deadlock
Process 1
Process 2
Resource 1
Resource 2
2Prevention
- Necessary conditions for deadlock
- Mutual exclusion
- Hold and wait
- Circular waiting
- No preemption
- Ensure that at least one of the necessary
conditions is false at all times - Mutual exclusion must hold at all times
3Hold and Wait
- Need to be sure a process does not hold one
resource while requesting another - Approach 1 Force a process to request all
resources it needs at one time - Approach 2 If a process needs to acquire a new
resource, it must first release all resources it
holds, then reacquire all it needs - What does this say about state transition
diagrams?
4Circular Wait
- Have a situation in which there are K processes
holding units of K resources
R
P
Ri
P holds R
Pi
R
P
P requests R
5Circular Wait (cont)
- There is a cycle in the graph of processes and
resources - Choose a resource request strategy by which no
cycle will be introduced - Total order on all resources, then can only ask
for Rj if Ri lt Rj for all Ri the process is
currently holding - This is how we noticed the easy solution for the
dining philosophers
6Avoidance
- Define a model of system states, then choose a
strategy that will guarantee that the system will
not go to a deadlock state - Requires extra information, e.g., the maximum
claim for each process - Allows resource manager to see the worst case
that could happen, then to allow transitions
based on that knowledge
7Bankers Algorithm
- Let maxci, j be the maximum claim for Rj by pi
- Let alloci, j be the number of units of Rj held
by pi - Can always compute
- availj cj - S0?ilt nalloci,j
- Then number of available units of Rj
- Should be able to determine if the state is safe
or not using this info
8Bankers Algorithm
- Copy the alloci,j table to alloci,j
- Given C, maxc and alloc, compute avail vector
- Find pi maxci,j - alloci,j ? availj
for 0 ? j lt m and 0 ? i lt n. - If no such pi exists, the state is unsafe
- If alloci,j is 0 for all i and j, the state is
safe - Set alloci,j to 0 deallocate all resources
held by pi go to Step 2
9Detection Recovery
- Check for deadlock (periodically or
sporadically), then recover - Can be far more aggressive with allocation
- No maximum claim, no safe/unsafe states
- Differentiate between
- Serially reusable resources A unit must be
allocated before being released - Consumable resources Never release acquired
resources resource count is number currently
available
10CACHE MEMORY
11Memory Manager
- Requirements
- Minimize executable memory access time
- Maximize executable memory size
- Executable memory must be cost-effective
- Todays memory manager
- Allocates primary memory to processes
- Maps process address space to primary memory
- Minimizes access time using cost-effective memory
configuration - May use static or dynamic techniques
12Building the Address Space
Source code
C
Reloc Object code
- Compile time Translate elements
13Static Memory Allocation
Operating System
Unused
In Use
Process 3
Process 0
pi
Process 2
Issue Need a mechanism/policy for loading pis
address space into primary memory
Process 1
14Fixed-Partition Memory Mechanism
Operating System
pi needs ni units
Region 0
N0
pi
ni
Region 1
N1
N2
Region 2
Region 3
N3
15Variable Partition Memory Mechanism
Operating System
16Dynamic Memory Allocation
- Could use dynamically allocated memory
- Process wants to change the size of its address
space - Smaller ? Creates an external fragment
- Larger ? May have to move/relocate the program
- Allocate holes in memory according to
- Best- /Worst- / First- /Next-fit
17Dynamic Address Relocation
CPU
Relative Address
0x02010
0x12010
Relocation Register
0x10000
load R1, 0x02010
MAR
- Program loaded at 0x10000 ? Relocation Register
0x10000 - Program loaded at 0x04000 ? Relocation Register
0x04000
We never have to change the load module addresses!
18Memory Mgmt Strategies
- Fixed-Partition used only in batch systems
- Variable-Partition used everywhere (except in
virtual memory) - Swapping systems
- Popularized in timesharing
- Relies on dynamic address relocation
- Now dated
- Dynamic Loading (Virtual Memory)
- Exploit the memory hierarchy
- Paging -- mainstream in contemporary systems
- Segmentation -- the future
19Virtual Memory
Secondary Memory
Virtual Address Space for pi
Virtual Address Space for pj
Virtual Address Space for pk
- Complete virtual address space is stored in
secondary memory
20Virtual Memory (cont)
- Since binding changes with time, use a dynamic
virtual address map, Yt
Virtual Address Space
Yt
21Size of Blocks of Memory
- Virtual memory system transfers blocks of the
address space to/from primary memory - Fixed size blocks System-defined pages are moved
back and forth between primary and secondary
memory - Variable size blocks Programmer-defined segments
corresponding to logical fragments are the
unit of movement - Paging is the commercially dominant form of
virtual memory today
22Paging
- A page is a fixed size, 2h, block of virtual
addresses - A page frame is a fixed size, 2h, block of
physical memory (the same size as a page) - When a virtual address, x, in page i is
referenced by the CPU - If page i is loaded at page frame j, the virtual
address is relocated to page frame j - If page is not loaded, the OS interrupts the
process and loads the page into a page frame
23Address Translation (cont)
g bits
h bits
Virtual Address
Page
Line
page table
Missing Page
Yt
j bits
h bits
Physical Address
Frame
Line
CPU
Memory
MAR
24(No Transcript)
25Demand Paging Algorithm
- Page fault occurs
- Process with missing page is interrupted
- Memory manager locates the missing page
- Page frame is unloaded (replacement policy)
- Page is loaded in the vacated page frame
- Page table is updated
- Process is restarted
26Random Replacement
- Replaced page, y, is chosen from the m loaded
page frames with probability 1/m
Let page reference stream, v 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 1 2
2 1 3
2 1 3
2 1 0
3 1 0
3 1 0
3 1 2
0 1 2
0 3 2
0 3 2
0 6 2
4 6 2
4 5 2
7 5 2
2 0 3
2 0
2
27Beladys Optimal Algorithm
- Replace page with maximal forward distance yt
max xeS t-1(m)FWDt(x)
Let page reference stream, v 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2
2 2 2 2 0 0 0 0 4 4 4 1 0 0 0 0 0 3 3 3 3 3 3
6 6 6 7 2 3 1 1 1 1 1 1 1 1 1 1 1 5 5
10 page faults
- Perfect knowledge of v ? perfect performance
- Impossible to implement
28Least Recently Used (LRU)
- Replace page with maximal forward distance yt
max xeS t-1(m)BKWDt(x)
Let page reference stream, v 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2
2 2 3 2 2 2 2 6 6 6 6 1 0 0 0 0 0 0 0 0 0 0 0 0
4 4 4 2 3 3 3 3 3 3 3 3 3 3 3 3 5 5 3
1 1 1 1 1 1 1 1 1 1 1 1 7
- Backward distance is a good predictor of forward
distance -- locality
29Least Frequently Used (LFU)
- Replace page with minimum use yt
min xeS t-1(m)FREQ(x)
Let page reference stream, v 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2
2 1 0 0 1 1 1 2 3 3 3 0
FREQ7(2) ? FREQ7(1) ? FREQ7(0) ?
30First In First Out (FIFO)
- Replace page that has been in memory the longest
yt max xeS t-1(m)AGE(x)
Let page reference stream, v 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2
1 1 0 0 0 2 3 3
AGE5(1) ? AGE5(0) ? AGE5(3) ?
31Dynamic Paging Algorithms
- The amount of physical memory -- the number of
page frames -- varies as the process executes - How much memory should be allocated?
- Fault rate must be tolerable
- Will change according to the phase of process
- Need to define a placement replacement policy
- Contemporary models based on working set
32Segmentation
- Unit of memory movement is
- Variably sized
- Defined by the programmer
- Two component addresses, ltSeg, offsetgt
- Address translation is more complex than paging
- Yt segments x offsets ? Physical Address ? W
- Yt(i, j) k
33Segment Address Translation
- Yt segments x offsets ? physical address ? W
- Yt(i, j) k
- s segments ? segment addresses
- Yt(s(segName), j) k
- l offset names ? offset addresses
- Yt(s(segName), l(offsetName)) k
- Read implementation in Section 12.5.2
34Address Translation
ltsegmentName, offsetNamegt
s
l
segment
offset
?
Limit
Yt
Relocation
Missing segment
Limit
Base
P
To Memory Address Register