operating systems cs3013 / cs502
DESCRIPTION
Operating Systems CS3013 / CS502. WEEK 3 SCHEDULING POLICIES SYNCHRONIZATION DEADLOCKS. Agenda. Scheduling Policies Synchronization Deadlock. Objectives. Identify Scheduling Criteria Explain Different Scheduling Policies Describe Measurements for Evaluation - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/1.jpg)
WEEK 3
SCHEDULING POLICIESSYNCHRONIZATION
DEADLOCKS
Operating SystemsCS3013 / CS502
![Page 2: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/2.jpg)
Agenda
Scheduling Policies
Synchronization
Deadlock
2
![Page 3: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/3.jpg)
Objectives3
Identify Scheduling Criteria
Explain Different Scheduling Policies
Describe Measurements for Evaluation
Give Examples of Evaluation Methods
![Page 4: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/4.jpg)
CPU Burst Duration4
![Page 5: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/5.jpg)
Scheduler5
Invoked whenever the operating system must select a user-level process to execute After process creation/termination A process blocks on I/O I/O interrupt occurs Clock interrupt occurs
Type of processes Interactive jobs CPU bound jobs Somewhere inbetween
![Page 6: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/6.jpg)
Scheduling Criteria6
What should we consider in developing a scheduling policy?
![Page 7: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/7.jpg)
Scheduling Criteria7
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 process has been waiting in the ready queue
Response time – amount of time from request submission until first response is produced
![Page 8: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/8.jpg)
Scheduling Issues8
Fairness – don’t starve process
Priorities – most important first
Deadlines – task X must be done by time t
Optimization – throughput, response time, etc.
![Page 9: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/9.jpg)
Scheduling Evaluation9
Simplicity – easy to implementJob latency – time from start to completionInteractive latency – time from action start to
expected system responseThroughput – number of jobs completedUtilization – keep processor and/or subset of
I/O devices busyDeterminism – insure that jobs get done
before some time or eventFairness – every job makes progress
![Page 10: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/10.jpg)
Policies10
First-Come-First-Serve (FCFS)
Round Robin
Shorted Process/Job First (SPF/SJF)
Pre-emptive Shorted Process/Job First (PSPF/PSJF)
Multiple-level Feedback (FB)
![Page 11: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/11.jpg)
First-Come-First-Serve (FCFS)11
Process Burst Time
A 5
B 7
C 3
A CB
0 5 12 15
![Page 12: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/12.jpg)
Shortest Process/Job First12
Process Burst Time
A 5
B 7
C 3
AC B
0 3 8 15
![Page 13: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/13.jpg)
Exponential Average13
b(t+1) = a b(t) + (1-a) b(t)
a=0 Current value has no effect
a=1 Only current value matters
b(0) = system average, a=0.5
![Page 14: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/14.jpg)
Priority Scheduling14
Process Burst Time Priority
A 5 3
B 7 1
C 3 2
ACB
0 7 10 15
![Page 15: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/15.jpg)
Priority Scheduling Issues15
Starvation – low priority processes may never execute Use of aging
Priority Inversion – some processes may never run to release resources required by other processes Increase the priority to match level of resource of
level of waiting process
![Page 16: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/16.jpg)
Round Robin16
Process Burst Time
A 5
B 7
C 3
A A A A AC C CB B B B BB B
0 9 12 15
![Page 17: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/17.jpg)
Round Robin17
Play with Quantum q = 1 q = 2 q = 5
Process Burst Time
A 5
B 7
C 3
![Page 18: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/18.jpg)
Round Robin18
Avg. Turnaround Time Quantum from 1 to 5 How does it change?
Process Burst Time
A 5
B 3
C 1
D 7
![Page 19: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/19.jpg)
Exercise19
Process Burst Time Priority
A 10 2
B 1 1
C 2 3
![Page 20: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/20.jpg)
Exercise20
Turnaround time FCFS SJF (Preemptive vs. Non-preemptive) RR q= 1 RR q = 0.5
Process Arrival Time Burst Time
A 0.0 8
B 0.4 4
C 1.0 1
![Page 21: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/21.jpg)
Multilevel Queues21
Multilevel Ready Queue Foreground (interactive) Background (batch)
Each queue has its own scheduling algorithm Foreground – RR Background – FCFS
Scheduling between queues is required Fixed priority scheduling Time slice
![Page 22: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/22.jpg)
Multilevel Feedback Queue22
Process can move between queues
Parameters # of queues Scheduling algorithm for each queue Method to determine when to upgrade a process Method to determine when to demote a process Method to determine which queue a process will enter
when that process needs service
![Page 23: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/23.jpg)
Objectives23
Identify Scheduling Criteria
Explain Different Scheduling Policies
Describe Measurements for Evaluation
Give Examples of Evaluation Methods
![Page 24: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/24.jpg)
Agenda24
![Page 25: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/25.jpg)
Objectives25
Explain reasons for synchronization
Describe busy waiting solutions
Describe a semaphore and its use
Describe a sonitor and its use
![Page 26: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/26.jpg)
Why synchronization?26
Time John Jane
3:00 Look in fridge. Oh no, no pizza!
3:05 Leave for store. Look in fridge. Oh no, no pizza!
3:10 Arrive at store. Leave for store.
3:15 Buy pizza. Arrive at store.
3:20 Arrive home. Buy pizza.
3:25 Put away pizza. Arrive home.
3:30 Put away pizza.
Oh no!
![Page 27: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/27.jpg)
Cooperating Processes27
Consider a printer spooler Enter a file name into the spooler queue Printer daemon checks the queue and prints the file.
“Race Conditions”
Free 9
letter hw1 proj1 lecture1
5 76 8 9
A B
![Page 28: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/28.jpg)
Race Condition Demo28
Shared variableOne program increments the shared variable.The other decrements the shared variable.
// reads the value of shared memorysscanf(counter, "%d", &num);// incrememt/decrement the numbernum = num + incr;// writes the new value to the shared memorysprintf(counter, "%d\n", num);
![Page 29: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/29.jpg)
Producer / Consumer29
Model for cooperating processes
Producer “produces” an item that consumer “consumes”
Bounded buffer (shared memory)
item buffer[MAX]; /* queue */int counter; /* number of items */
![Page 30: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/30.jpg)
Producer30
item i; /* item produced */int in; /* put next item */while (1) {
produce an item;while (counter == MAX) {/* no-op */}buffer[in] = item;in = (in + 1) % MAX;counter = counter + 1;
}
![Page 31: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/31.jpg)
Consumer31
item i; /* item consumed */int out; /* take next item */while (1) {
while (counter == 0) {/* no-op */}item = buffer[out]out = (out + 1) % MAX;counter = counter - 1;
}
![Page 32: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/32.jpg)
Producer Consumer Trouble!32
P R1 = counter R1 = 5
P R1 = R1 + 1 R1 = 6
C R2 = counter R2 = 5
C R2 = R2 – 1 R2 = 4
C counter = R2 counter = 4
P counter = R1 counter = 6
![Page 33: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/33.jpg)
Critical Section33
Mutual Exclusion Only one process inside critical region
Progress No process outside critical region may block other
processes wanting in
Bounded Waiting No process should have to wait forever (starvation)
![Page 34: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/34.jpg)
First Try34
Shared variable turn is initialized to either A or B and both processes start execution concurrently.
…While (TRUE) {
…while (turn == B) ;// critical sectionturn = B;…
}
…While (TRUE) {
…while (turn == B) ;// critical sectionturn = B;…
}
…While (TRUE) {
…while (turn == A) ;// critical sectionturn = A;…
}
…While (TRUE) {
…while (turn == A) ;// critical sectionturn = A;…
}
Process A Process B
![Page 35: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/35.jpg)
Second Try35
Shared variables pAinside and pBinside are initialized to FALSE.
…While (TRUE) {
…while (pBinside) ;pAinside = TRUE;// critical sectionpAinside = FALSE;…
}
…While (TRUE) {
…while (pBinside) ;pAinside = TRUE;// critical sectionpAinside = FALSE;…
}
…While (TRUE) {
…while (pAinside) ;pBinside = TRUE;// critical sectionpBinside = FALSE;…
}
…While (TRUE) {
…while (pAinside) ;pBinside = TRUE;// critical sectionpBinside = FALSE;…
}
Process A Process B
![Page 36: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/36.jpg)
Third Try36
Shared variables pAtrying and pBtrying are initialized to FALSE.
…While (TRUE) {
…pAtrying = TRUE;while (pBtrying) ;// critical sectionpAtrying = FALSE;…
}
…While (TRUE) {
…pAtrying = TRUE;while (pBtrying) ;// critical sectionpAtrying = FALSE;…
}
…While (TRUE) {
…pBtrying = TRUE;while (pAtrying) ;// critical sectionpBtrying = FALSE;…
}
…While (TRUE) {
…pBtrying = TRUE;while (pAtrying) ;// critical sectionpBtrying = FALSE;…
}
Process A Process B
![Page 37: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/37.jpg)
Dekker’s Algorithm37
turn needs to be initialized to A or B.
…While (TRUE) { … pAtrying = TRUE; while (pBtrying)
if (turn == B) { pAtrying = FALSE; while (turn == B) ; pAtrying = TRUE;}
// critical section turn = B; pAtrying = FALSE; …}
…While (TRUE) { … pAtrying = TRUE; while (pBtrying)
if (turn == B) { pAtrying = FALSE; while (turn == B) ; pAtrying = TRUE;}
// critical section turn = B; pAtrying = FALSE; …}
Process A Process B…While (TRUE) { … pBtrying = TRUE; while (pAtrying)
if (turn == A) { pBtrying = FALSE; while (turn == A) ; pBtrying = TRUE;}
// critical section turn = A; pBtrying = FALSE; …}
…While (TRUE) { … pBtrying = TRUE; while (pAtrying)
if (turn == A) { pBtrying = FALSE; while (turn == A) ; pBtrying = TRUE;}
// critical section turn = A; pBtrying = FALSE; …}
![Page 38: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/38.jpg)
Peterson’s Algorithm38
turn does not need any initialization.
…While (TRUE) { … pAtrying = TRUE; turn = B; while (pBtrying && turn == B); // critical section pAtrying = FALSE; …}
…While (TRUE) { … pAtrying = TRUE; turn = B; while (pBtrying && turn == B); // critical section pAtrying = FALSE; …}
Process A Process B…While (TRUE) { … pBtrying = TRUE; turn = A; while (pAtrying && turn == A); // critical section pBtrying = FALSE; …}
…While (TRUE) { … pBtrying = TRUE; turn = A; while (pAtrying && turn == A); // critical section pBtrying = FALSE; …}
![Page 39: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/39.jpg)
Dekker’s and Peterson’s Algorithm39
Both of them are correct.
Limited to 2 processes
Can be generalized to N processes N must be known and fixed. Algorithm becomes too complicated and expensive.
![Page 40: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/40.jpg)
Synchronization Hardware40
Test-and-Set: returns and modifies atomically
int Test_and_Set(int &target) {int temp;temp = target;target = true;return temp;
}
![Page 41: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/41.jpg)
Using Test_and_Set41
All solutions so far requires “busy waiting”.
while (1) {while (Test_and_Set(lock)) ;// critical sectionlock = false;// remainder section
}
![Page 42: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/42.jpg)
Semaphore42
![Page 43: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/43.jpg)
Semaphore43
Do not require “busy waiting”Semaphore S (shared, often initially = 1)
Integer variable Accessed via two (indivisible) atomic operations
wait(S) : S = S – 1if (S < 0) then block(S)
signal(S) : S = S + 1;if (S <= 0) then wakeup(S)
![Page 44: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/44.jpg)
Critical Section with Semaphore44
semaphore mutex; /* shared */
while (1) {wait(mutex);// critical sectionsignal(mutex);// remainder section
}
![Page 45: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/45.jpg)
Classical Synchronization Problems45
Bounded Buffers
Readers/Writers
Dining Philosophers
![Page 46: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/46.jpg)
Readers/Writers46
Readers can only read the content
Writers read and write the object
Critical Region One or more readers (no writers) One writer (nothing else)
Solutions favor Reader or Writer
![Page 47: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/47.jpg)
Readers/Writers47
Shared
Writer
semaphore mutex, wrt;int readcount;
wait(wrt);// write stuffsignal(wrt)
![Page 48: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/48.jpg)
Reader
Readers-Writers48
wait(mutex);readcount = readcount + 1;if (readcount == 1) wait(wrt);signal(mutex);// read stuffwait(mutex);readcount = readcount – 1;if (readcount == 0) signal(wrt);signal(mutex);
![Page 49: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/49.jpg)
Dining Philosophers49
Philosophers Think Eat
Need 2 chopsticks to eat
![Page 50: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/50.jpg)
Dining Philosophers50
Philosopher i
while (1) {// thinkwait(chopstick[i]);wait(chopstick[i+1 % 5]);// eatsignal(chopstick[i]);signal(chopstick[i+1 % 5]);
}
![Page 51: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/51.jpg)
Dining Philosophers51
Other solutions
Allow at most N-1 to go hungry at a time
Allow to pick up chopsticks only if both are available
Asymmetric solution
![Page 52: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/52.jpg)
Monitors52
High-level constructCollection of
Variables Data structures Functions Like C++ class
One process active inside“Condition” variable
Not like semaphore
![Page 53: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/53.jpg)
Producer-Consumer Monitor53
monitor ProducerConsumer {condition full, empty;integer count;
// functionsvoid enter(item i);item remove();
}
void producer();void consumer();
![Page 54: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/54.jpg)
Producer-Consumer Monitor54
void producer() {item i;while (1) {
// produce item iProducerConsumer.enter(i);
}}void consumer() {
item i;while (1) {
i = ProducerConsumer.remove();// consume item i
}}
![Page 55: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/55.jpg)
Producer-Consumer Monitor55
void enter(item i) {if (count == N) sleep(full);// add item icount = count + 1;if (count == 1) then wakeup(empty);
}void remove() {
if (count == 0) then sleep(empty);// remove item into icount = count – 1;if (count == N-1) then wakeup(full);return i;
}
![Page 56: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/56.jpg)
Objectives56
Explain reasons for synchronization
Describe busy waiting solutions
Describe a semaphore and its use
Describe a sonitor and its use
![Page 57: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/57.jpg)
Agenda57
![Page 58: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/58.jpg)
Objectives58
Identify deadlocks
Describe ways to prevent deadlocks
Describe ways to detect deadlocks
Describe ways to recover from deadlocks
![Page 59: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/59.jpg)
Deadlock59
Examples “It takes money to make money” You can’t get a job without experience; you can’t get
experience without a job.
Background The cause of deadlocks: Each process needing what
another process has. This results from sharing resources such as memory, devices, links.
Under normal operation, a resource allocation proceeds like this: Request a resource (suspend until available if necessary) Use the resource Release the resource
![Page 60: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/60.jpg)
Conditions for Deadlock60
Mutual Exclusion Each resource is assigned to a process or is available
Hold and Wait Processes holding resources can request new
resources
No Preemption Resources cannot be taken from a process
Circular Wait A set of processes are in a circular wait
![Page 61: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/61.jpg)
Resource Allocation Graph61
G = (V, E)
V : Nodes consist of processes and resource types
E : Edges are pairs of processes and resources
PiRj
PiRj
Pi
![Page 62: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/62.jpg)
No cycle No deadlock Cycle
If resource types have multiple instances, then deadlock MAY exist
If each resource type has 1 instance, then deadlock has occurred
Resource Allocation Graph62
P2 Requests P3
R3 Assigned to P3
![Page 63: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/63.jpg)
Resource Allocation Graph63
![Page 64: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/64.jpg)
Handling Deadlocks64
Ignore Deadlocks
Ensure deadlock never occurs Prevention Avoidance
Allow deadlock to happen Detection Recovery
![Page 65: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/65.jpg)
Deadlock Prevention65
Mutual Exclusion Not really possible
Hold and Wait Collect all resources before execution Only request resource when no others are being held
No Preemption Release any resource being held if the process can’t
get additional resource Allow preemption
Circular Wait Number resources and only request in ascending
order
![Page 66: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/66.jpg)
Deadlock Avoidance66
Possible States Deadlock: No forward progress can be made Unsafe: A state that may cause deadlock Safe: A state is safe if a sequence of processes exist
such that there are enough resources for the first to finish, and as each finishes and releases its resources there are enough for the next to finish.
If a request allocation would cause an unsafe state, do not honor that request.
![Page 67: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/67.jpg)
Deadlock Detection67
Resource Allocation Graph Wait-for Graph
![Page 68: Operating Systems CS3013 / CS502](https://reader036.vdocument.in/reader036/viewer/2022062518/56814453550346895db0ef0c/html5/thumbnails/68.jpg)
Deadlock Recovery68
PROCESS TERMINATION: Could delete all the processes in the deadlock -- this is expensive. Delete one at a time until deadlock is broken ( time consuming ). Select who to terminate based on priority, time executed, time to
completion, needs for completion, or depth of rollback In general, it's easier to preempt the resource, than to terminate
the process.
RESOURCE PREEMPTION: Select a victim - which process and which resource to preempt. Rollback to previously defined "safe" state. Prevent one process from always being the one preempted
( starvation ).