0% found this document useful (0 votes)
2 views

18-ch8_part3__ch9_part1

The document discusses deadlock recovery methods in operating systems, including process termination and resource preemption. It outlines the advantages and disadvantages of each method, emphasizing the economic considerations in choosing which processes to abort. Additionally, it introduces concepts related to memory management, including address binding, logical vs. physical address spaces, and the role of the memory-management unit (MMU).

Uploaded by

otgameryt0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

18-ch8_part3__ch9_part1

The document discusses deadlock recovery methods in operating systems, including process termination and resource preemption. It outlines the advantages and disadvantages of each method, emphasizing the economic considerations in choosing which processes to abort. Additionally, it introduces concepts related to memory management, including address binding, logical vs. physical address spaces, and the role of the memory-management unit (MMU).

Uploaded by

otgameryt0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Year: 2024-2025

Fall Semester

Operating Systems
Dr. Wafaa Samy
Dr. Hanaa Eissa

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.1 Modified by Dr. Wafaa Samy
Chapter 8: Deadlocks
(Part 3)

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 Modified by Dr. Wafaa Samy
Outline
 System Model
 Deadlock Characterization
 Methods for Handling Deadlocks
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection
 Recovery from Deadlock

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.3 Modified by Dr. Wafaa Samy
Recovery from Deadlock

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.4 Modified by Dr. Wafaa Samy
Recovery from Deadlock
 When a deadlock detection algorithm determines that a deadlock
exists, several alternatives are available.
• One possibility is to inform the operator that a deadlock has
occurred and to let the operator deal with the deadlock manually.

• Another possibility is to let the system recover from the deadlock


automatically. There are two options for breaking a deadlock.
1. Process and Thread Termination: One is simply to abort one
or more threads to break the circular wait.
2. Resource Preemption: The other is to preempt some
resources from one or more of the deadlocked threads.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.5 Modified by Dr. Wafaa Samy
Recovery from Deadlock: Process Termination
 To eliminate deadlocks by aborting a process or thread, we use
one of two methods:
1. Abort all deadlocked processes.
2. Abort one process at a time until the deadlock cycle is
eliminated.

 In both methods, the system reclaims all resources allocated to the


terminated processes.

 If the first method is used: Abort all deadlocked processes.


• Advantage: this method clearly will break the deadlock cycle.
• Disadvantage: great expense.
 The deadlocked processes may have computed for a long
time, and the results of these partial computations must be
discarded and probably will have to be recomputed later.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.6 Modified by Dr. Wafaa Samy
Recovery from Deadlock: Process Termination (Cont.)
 If the second method (partial termination) is used, then we
must determine which deadlocked process (or processes)
should be terminated.
 In which order should we choose to abort?
Many factors may affect which process is chosen, including:
1. Priority of the process.
2. How long process has computed, and how much longer to
completion.
3. Resources the process has used.
4. Resources process needs to complete.
5. How many processes will need to be terminated.
6. Is process interactive or batch?
The question is basically an
economic one; we should abort
those processes whose termination
will incur the minimum cost.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.7 Modified by Dr. Wafaa Samy
Recovery from Deadlock: Process Termination (Cont.)
 Disadvantages of the second method:
• It incurs considerable overhead, since after each process is
aborted, a deadlock-detection algorithm must be invoked to
determine whether any processes are still deadlocked.
• Aborting a process may not be easy. If the process was in the
midst of updating a file, terminating it may leave that file in an
incorrect state.
• Similarly, if the process was in the midst of updating shared data
while holding a mutex lock, the system must restore the status
of the lock as being available, although no guarantees can be
made regarding the integrity of the shared data.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.8 Modified by Dr. Wafaa Samy
Recovery from Deadlock: Resource Preemption
 To eliminate deadlocks using resource preemption, we successively
preempt some resources from processes and give these resources to other
processes until the deadlock cycle is broken.

 If preemption is required to deal with deadlocks, then three issues need to be


addressed:
1. Selecting a victim – minimize cost.
 Which resources and which processes are to be preempted?
2. Rollback – return to some safe state, restart process for that state.
 If we preempt a resource from a process, what should be done with
that process?
 Since, in general, it is difficult to determine what a safe state is, the
simplest solution is a total rollback: abort the process and then
restart it.
3. Starvation – same process may always be picked as victim, so
include number of rollbacks in the cost factor as a solution.
 How can we guarantee that resources will not always be preempted
from the same process?
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.9 Modified by Dr. Wafaa Samy
Chapter 9: Main Memory
(Part 1)

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 Modified by Dr. Wafaa Samy
Chapter 9: Memory Management

 Background
 Contiguous Memory Allocation
 Segmentation
 Paging
 Swapping

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.11 Modified by Dr. Wafaa Samy
Background
 Program must be brought (from disk) into memory and placed within a
process for it to be run.
 Main memory and registers are the only general-purpose storage that the
CPU can access directly.
• Any instructions in execution, and any data being used by the instructions, must be
in one of these direct-access storage.
• If the data are not in memory, they must be moved there before the CPU can
operate on them.
 Register access is done in one cycle of the CPU clock (or less).
 Main memory access can take many cycles of the CPU clock, causing a stall
to the CPU.
• The remedy is to add fast memory “Cache” between the CPU registers
and main memory.
 Memory consists of a large array of bytes, each with its own address.
 Memory unit only sees a stream of:
• Addresses + read requests, or
• Address + data and write requests.
 Protection of memory required to ensure correct operation.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.12 Modified by Dr. Wafaa Samy
Protection
 For proper system operation, we
must protect the OS from access
by user processes, as well as
protect user processes from one
another.
 Need to ensure that a process
can only access those
addresses in its address space.
• We can provide this protection by
using a pair of base and limit
registers define the logical
address space of a process.
 The base register holds the For example, if the base register
smallest legal physical memory holds 300040 and the limit
address. register is 120900, then the
 The limit register specifies the program can legally access all
size of the range. addresses from 300040 through
Operating System Concepts – 10th Edition
420939 (inclusive).
Silberschatz, Galvin and Gagne ©2018 8.13 Modified by Dr. Wafaa Samy
Hardware Address Protection
 CPU must check every memory access generated in user mode to be sure
it is between base and limit for that user.
 Protection of memory space is accomplished by having the CPU hardware
compare every address generated in user mode with the registers (base & limit).
• Any attempt by a program executing in user mode to access OS memory or other
users’ memory results in a trap to the OS, which treats the attempt as a fatal
error.

 The base and limit registers can be loaded only by the operating system.
• The instructions to loading the base and limit registers are privileged
(i.e. can be executed only in kernel mode).
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.14 Modified by Dr. Wafaa Samy
Address Binding
 Programs on disk as a binary executable files, 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.
• Most systems allow a user process to reside in any part
of the physical memory. The first address of the user
process need not be 0000.
• How can it not be? How the OS places a process in
physical memory?

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.15 Modified by Dr. Wafaa Samy
Address Binding (Cont.)
 A user program goes through several steps—some of which
may be optional—before being executed. Addresses
represented in different ways at different stages of a
program’s life:
1. Source code addresses usually symbolic (e.g. the variable
count).

2. A compiler typically binds these symbolic addresses to


relocatable addresses. Compiled code addresses bind to
relocatable addresses. (i.e. “14 bytes from beginning of this
module”).

3. Linker or loader will bind relocatable addresses to absolute


addresses. (i.e. 74014).

 Each binding is a mapping from one address space to


another.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.16 Modified by Dr. Wafaa Samy
Binding of Instructions and Data to Memory
 Address binding of instructions and data to memory addresses can happen at
three different stages:
1. Compile time: If you know at compile time where the process will reside
in memory.
 If memory location known a priori, absolute code can be generated.
 For example, if you know that a user process will reside starting at location
R, then the generated compiler code will start at that location and extend up
from there.
 Note: Must recompile code if starting location changes.
2. Load time: Compiler must generate relocatable code if memory
location (where the process will reside in memory) is not known at
compile time. So, final binding is delayed until load time.
 Note: If the starting address changes, we need only reload the user code to
incorporate this changed value.
3. Execution time: Binding delayed until run time if the process can be
moved during its execution from one memory segment to another.
 Need hardware support for address maps (e.g. base and limit registers).
 Most operating systems use this method.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.17 Modified by Dr. Wafaa Samy
Multistep Processing of a User Program

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.18 Modified by Dr. Wafaa Samy
Logical vs. Physical Address Space
 The concept of a logical address space that is bound to a separate physical
address space is central to proper memory management.
• Logical address: generated by the CPU; also referred to as virtual
address.
• Physical address: address seen by the memory unit.
 An address seen by the memory unit—that is, the one loaded into the
memory-address register of the memory.

 Logical and physical addresses are the same in compile-time address-


binding scheme and load-time address-binding scheme; logical and
physical addresses differ in execution-time address-binding scheme.

 Logical address space is the set of all logical addresses generated by a


program.
 Physical address space is the set of all physical addresses generated by a
program.
• The set of all physical addresses corresponding to these logical
addresses is a physical address space.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.19 Modified by Dr. Wafaa Samy
Memory-Management Unit (MMU)
 Run-time mapping from logical (virtual) to physical addresses is done by
a hardware device called the memory-management unit (MMU).
• Hardware device that at run time maps virtual to physical address.
In case of execution-time
address-binding scheme

 Two different types of addresses:


• Logical addresses (in the range 0 to max) and
• Physical addresses (in the range R + 0 to R + max for a base value R).
 The user program generates only logical addresses and thinks that the
process runs in memory locations from 0 to max. However, these logical
addresses must be mapped to physical addresses before they are used.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.20 Modified by Dr. Wafaa Samy
Hardware Support for Relocation and Limit Registers
 Memory Protection: Relocation and limit registers used to protect user
processes from each other, and from changing operating-system code and
data.
• Relocation 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 the logical address dynamically by adding the value in the
relocation register.
• Note: Can then allow actions such as kernel code being transient and kernel
changing size.
Because every address
generated by a CPU is
checked against the limit
and relocation registers,
we can protect both the
operating system and the
other users’ programs and (Example:
data from being modified relocation = 100040
by this running process. and limit = 74600)

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.21 Modified by Dr. Wafaa Samy
Memory-Management Unit (Cont.)
 Consider simple MMU scheme. which is a generalization of the base-register
scheme. The base register now called relocation register.
 The value in the relocation register is added to every address generated by a
user process at the time the address is sent to memory.

Dynamic
relocation
using a
relocation
register.

 For example, if the base is at 14000, then an attempt by the user to address
location 0 is dynamically relocated to location 14000; an access to location
346 is mapped to location 14346.
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.22 Modified by Dr. Wafaa Samy
Memory-Management Unit (Cont.)
 The user program never accesses the real physical addresses.
• The program can create a pointer to location 346, store it in memory,
manipulate it, and compare it with other addresses—all as the number
346.
• Only when it is used as a memory address (in an indirect load or store,
perhaps) it is relocated relative to the base register.

 The user program deals with logical addresses; it never sees the real
physical addresses.
• The memory mapping hardware converts logical addresses into physical
addresses. Logical address bound to physical addresses.
 This form of execution-time binding.
• Execution-time binding occurs when reference is made to location in
memory.
 The final location of a referenced memory address is not determined
until the reference is made.

Operating System Concepts – 10th Edition


Silberschatz, Galvin and Gagne ©2018 8.23 Modified by Dr. Wafaa Samy
Operating System Concepts – 10th Edition
Silberschatz, Galvin and Gagne ©2018 8.24 Modified by Dr. Wafaa Samy

You might also like