Docsity

Operating Systems Class Notes: Deadlock, Memory Management, and Virtual Memory - Prof. Mar, Study notes of Operating Systems

Toggle sidebar
bg1
1
CS414: Operating Systems
Today’s Class
•Last time
Deadlock (chapt 8)
Midterm back
•This time
Deadlock (chapt 8)
Memory management (chapt 9)
PA#3 Discussion
PA#5 out later today
•Next time (Thurs Nov 1)
Virtual memory (chapt 10)
PA#3 back
CS414: Operating Systems
Schedule
Sun Tues Thurs Fri
30 1
Deadlock
Memory management
PA#5 out
Memory management
Virtual memory
No class – marty
at conference
Virtual memory
6 8
13 15
20 22
protection, security
PA#6 due, PA#7 out
27 29
protection, security
4
protection,
security
File systems
File sys, I/O sys
File sys
I/O sys 9
No class – marty
at conference
I/O sys,Mass-storage
PA#5 due, PA#6 out No class –
Thanksgiving
14
Final exam
10:00-noon
6
Wrap-up
PA#7 due
protection, security
protection,
security
30
CS414: Operating Systems
Before we start: PA#3 Questions
1. What is the efficiency of the Virtual Machine as
compared to the physical machine on the test
program?
2. What is the complexity of your algorithm? O(1)? O(n)?
Worse than O(n)?
3. What is the (measured) overhead of your approach?
What’s your experimental design to determine this
measured overhead?
4. What is the accuracy of your approach /
implementation?
CS414: Operating Systems
From last time: Deadlock
•4 necessary conditions
•4 approaches
Ignore – it’s an “application-level concern”
“prevention” – design-time (off- line)
“avoidance” – dynamically allocate resources carefully (run-time)
Detect and recover
CS414: Operating Systems
Resource Allocation Graph
•We define a graph with
vertices that represent both
resources R1.. Rm and threads
T1…Tn
A directed edge from a thread to a
resource Ti
Rj indicates that Ti has
requested that resource, but has not
yet acquired it.
A directed edge from a resource to a
thread Rj
Ti indicates that the OS
has allocated Rj to Ti.
•Note:
If the graph has no cycles, no
deadlock exists.
If the graph has a cycle, deadlock
may exist (if there is only one resource
instance of each type, then it HAS
occurred).
1
TR
2
T3
T2
1
R
CS414: Operating Systems
Deadlock Avoidance: Banker’s Algorithm
•Think Jimmy Stewart – “it’s a wonderful life”
•Force threads to provide advance information about what resources
they may need for the duration of the execution.
•Def: system is “safe” if the system can allocate resources to each
process in some order and still avoid a deadlock
Issue: how do we find this order (“safe sequence”)? Must we adhere to this
order?
•Def: a sequence of threads T1, T2, … Tn is “safe” (deadlock free) if
for each Ti the resources that Ti can still request can be satisfied by
the currently available resources plus the resources held by all Tj, j<i
•An unsafe state is not equivalent to deadlock; it just may lead to
deadlock.
•When a thread requests a resource:
would NOT result in unsafe state: grant it.
would result in unsafe state: do not grant it.
•This algorithm ensures no circular-wait condition ever exists.
safe
unsafe
deadlock
pf3
pf4

Partial preview of the text

Download Operating Systems Class Notes: Deadlock, Memory Management, and Virtual Memory - Prof. Mar and more Study notes Operating Systems in PDF only on Docsity!

CS414: Operating Systems

Today’s Class

  • Last time

Deadlock (chapt 8)

Midterm back

  • This time

Deadlock (chapt 8)

Memory management (chapt 9)

PA#3 Discussion

PA#5 out later today

  • Next time (Thurs Nov 1)

Virtual memory (chapt 10)

PA#3 back

CS414: Operating Systems

Schedule

Sun Tues Thurs (^) Fri

30 1

Deadlock Memory management PA#5 out

Memory management Virtual memory

No class – marty at conference

Virtual memory 6 8

13 15

(^20 ) protection, security PA#6 due, PA#7 out 27 29

protection, security

4

protection, security

File systems

File sys, I/O sys

File sys I/O sys 9 No class – marty at conference I/O sys,Mass-storage PA#5 due, PA#6 out

No class – Thanksgiving

14

Final exam 10:00-noon

6

Wrap-up PA#7 due

protection, security

protection, security 30

CS414: Operating Systems

Before we start: PA#3 Questions

1. What is the efficiency of the Virtual Machine as

compared to the physical machine on the test program?

2. What is the complexity of your algorithm? O(1)? O(n)?

Worse than O(n)?

3. What is the (measured) overhead of your approach?

What’s your experimental design to determine this measured overhead?

4. What is the accuracy of your approach /

implementation?

CS414: Operating Systems

From last time: Deadlock

  • 4 necessary conditions
  • 4 approaches

Ignore – it’s an “application-level concern”

“prevention” – design-time (off-line)

“avoidance” – dynamically allocate resources carefully (run-time)

Detect and recover

CS414: Operating Systems

Resource Allocation Graph

  • We define a graph with

vertices that represent both

resources R1.. Rm and threads

T1…Tn

 A directed edge from a thread to a resource Ti  Rj indicates that Ti has requested that resource, but has not yet acquired it.  A directed edge from a resource to a thread Rj  Ti indicates that the OS has allocated Rj to Ti.

  • Note:  If the graph has no cycles, no deadlock exists.  If the graph has a cycle, deadlock may exist (if there is only one resource instance of each type, then it HAS occurred).

T 1

R

T

T 2

R

CS414: Operating Systems

Deadlock Avoidance: Banker’s Algorithm

  • Think Jimmy Stewart – “it’s a wonderful life”
  • Force threads to provide advance information about what resources they may need for the duration of the execution.
  • Def: system is “safe” if the system can allocate resources to each process in some order and still avoid a deadlock Issue: how do we find this order (“safe sequence”)? Must we adhere to this order?
  • Def: a sequence of threads T1, T2, … Tn is “safe” (deadlock free) if for each Ti the resources that Ti can still request can be satisfied by the currently available resources plus the resources held by all Tj, j<i
  • An unsafe state is not equivalent to deadlock; it just may lead to deadlock.
  • When a thread requests a resource:

would NOT result in unsafe state: grant it.

would result in unsafe state: do not grant it.

  • This algorithm ensures no circular-wait condition ever exists.

safe

deadlock unsafe

CS414: Operating Systems

Example of Banker’s Algorithm

  • Threads T1, T2, and T3; 12 different tape drives
  • Current state ----------------------->
  • The current state is safe---there exists a safe sequence T1, T2, T where all processes could obtain the max. number of resources they need without waiting.  [T1] can complete with the current resource allocation (11 tape drives are allocated; T1 can only request one more; there are 12 total)  [T2] can complete with its current resources, plus all of T1’s resources, and then unallocated tape drive.  [T3] can complete with all its current resources, all of T1’s and T2’s resources, and the unallocated tape drive.

Max need

In use Could want

T1 4 3 1

T2 8 4 4

T3 12 4 8

CS414: Operating Systems

Banker’s Algorithm (cont.)

  • If T3 were to requests one more drive, then it must wait because it would result in an unsafe state -----------------------> Implementation: upon request, hypothetically grant it and see if the state is safe, if not don’t grant it

Max need

In use Could want

T1 4 3 1

T2 8 4 4

T3 12 5 7

CS414: Operating Systems

Deadlock Detection

Using a Resource Allocation Graph

  • To detect deadlock: determine if any thread can possibly progress
  • reduction: continually check the RA graph to see if any thread can acquire all the resources it requires and thus eventually terminate and release all the resources it possessed
  • Reduction

A RA graph is reduced by thread Ti (where Ti is not blocked) by removing all edges to and from Ti. A RA graph is irreducible if it cannot be reduced by any thread A RA graph is completely reducible if there exists a sequence of reductions that deletes all edges

  • Results A system state represented by the RA graph G is a deadlock state iff G is not completely reducible A cycle in a RA graph is a necessary but not sufficient condition for deadlock for general RA graphs  no cycle  no deadlock  cycle  nothing (again, multiple resources…)

CS414: Operating Systems

Reduction Example

T 1 T 3 R 2 T 4

R 1 T 2

T 1 T 3 R 2 T 4

T 1 T 3 R 2 T 4

T 1 T 3 R 2 T 4

T 1 T 3 R 2 T 4

R 1 T 2

R 1 T 2 R 1 T 2

R 1 T 2

reduction by T

reduction by T

reduction by T

by T

reduction

CS414: Operating Systems

Recovery from Deadlock

  • Approach: Detect deadlock, then correct it.
  • Scan RA graph for cycles and then fix the cycles:

Kill all the threads in the cycle.

Kill all the threads one at a time, forcing them to give up resources,

but if the thread holds a lock, this will leave the thread in an

inconsistent state.

Roll back the actions of the deadlocked threads (e.g., database

transactions.)

  • Problem: When should we execute this algorithm?
  • Note:

This solution waits for deadlock to occur and then tries to fix it. If

does not prevent it, so deadlock can keep occurring with this

solution.

CS414: Operating Systems

PA#5: synchronization problems

(double points)

  • Three probs

Emmet Street Rope Bridge

“Coolness”: dining hall

Dorm problem: match students

  • Three approaches

.NET and semaphores

Java (1.6) and a monitor

Pthreads (cygwin/windows) and a monitor

  • You are NOT allowed to use ANY code that you find on the Web

CS414: Operating Systems

Overview

  • Driving Memory Designs

 Primary memory access time must be as small as possible

 Primary memory must be as large as possible

 Primary memory must be cost-effective

  • Purpose/Requirements of Memory Manager

1. Allocate physical memory space to processes

2. Map the process address space into the allocated physical

memory

3. Minimize access times using a cost-effective amount of primary

memory

CS414: Operating Systems

Memory Issues / Concerns

  • Where is the executing program?
  • How do we manage multiple programs in memory?
  • What does an address mean?
  • How do we organize executing programs?
  • What is the interaction between the CPU allocator and the memory allocator?

CS414: Operating Systems

Architecture Review

  • Program

executable starts out on disk

  • To execute, the OS loads the program into memory (not necessarily all of the program)
  • Data and instructions migrate into the cache
  •  OS focus: memory

CS414: Operating Systems

Mapping the Address Space to Primary Memory

  • Address Binding:

Each program

generates a string

of addresses.

Where do these

addresses come

from?

CS414: Operating Systems

Basic Memory Management Terminology

  • [Segment] A chunk of memory assigned to a process
  • [Page] Memory may be divided into fixed size pages to which the OS assigns processes
  • [Physical Address] A real address in memory (from 0 to the size of memory)
  • [Virtual Address] A program address that may or may not have an equivalent in memory. Virtual address space may be larger than the actual memory. OS or architecture translates the virtual address to a physical address.
  • Physical vs. Logical Memory is a Crucial Concept!

CS414: Operating Systems

Contiguous Memory Allocation

  • As processes enter the system, execute, and terminate, the OS must keep track of which memory is available and utilized
  • [Allocatable “Chunk”] contiguous memory, either fixed-partition (e.g., 3M process into 4M allocatable unit– internal fragmentation!) or variable-sized (can lead to external fragmentation! )
  • [Holes] pieces of free memory (shaded in the figure)
  • PROBLEM: Given a list of holes the OS must allocate processes to holes