UNIT 3 OS
UNIT 3 OS
• Process Concept
• Process Scheduling
• Operations on Processes
• Cooperating Processes
• Interprocess Communication
• Communication in Client-Server Systems
Process Concept
Process in Memory
Process State
Diagram of Process State
Process Control Block (PCB)
Process Control Block (PCB)
CPU Switch From Process to Process
Process Scheduling Queues
Long-term scheduler (or job scheduler) – selects which processes should be brought
into the ready queue
Short-term scheduler (or CPU scheduler) – selects which process should be executed
next and allocates CPU
Addition of Medium Term Scheduling
Schedulers (Cont.)
● Basic Concepts
● Scheduling Criteria
● Scheduling Algorithms
● Multiple-Processor Scheduling
● Real-Time Scheduling
● Thread Scheduling
● Operating Systems Examples
● Algorithm Evaluation
Basic Concepts
Alternating Sequence of CPU And I/O Bursts
CPU Scheduler
• It is a non-preemptive, pre-emptive
scheduling algorithm.
P0 0 5 0
P1 1 3 5
P2 2 8 8
P3 3 6 16
• Two schemes:
• Advantages of round-robin:
No starvation will be there in round-robin because
every process will get chance for its execution.
Used in time-sharing systems.
• Disadvantages of round-robin:
We have to perform a lot of context switching here,
which will keep the CPU idle.
Multilevel Queue
available.
Example
-System has 2 disk drives
-P1 and P2 process each hold one disk drive and each
1 Aprilneeds
2024 another one 62
Deadlock Characterization
Deadlock can arise if four conditions hold
simultaneously
1 April 2024 64
Resource-Allocation Graph
• Process
• Pi requests instance of Rj P
i
• Pi is holding an instance of Rj
1 April 2024 65
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Resource Allocation Graph With A Cycle But No Deadlock
Safe, unsafe , deadlock state spaces
Resource Allocation Graph With A Deadlock
Before P3 requested an After P 3 requested an
instance of R2 instance of R2
1 April 2024 70
Methods for Handling Deadlock
⚫ Prevention
◦ Ensure that the system will never enter a
deadlock state
⚫ Avoidance
◦ Ensure that the system will never enter an
unsafe state
⚫ Detection
◦ Allow the system to enter a deadlock state and
then recover
⚫ Do Nothing
◦ Ignore the problem and let the user or system
administrator respond to the problem; used by
most operating systems, including Windows and
1 April 2024 UNIX 71
Deadlock Prevention
• To prevent deadlock, we can restrain the ways that
a request can be made
• Do not allow one of the four conditions to occur
• Mutual Exclusion :
1 April 2024 73
Deadlock Prevention
No Preemption:
• 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
• Pi Rj Pj Rk
• A process will be restarted only when it can
regain its old resources, as well as the new ones
that it is requesting.
• Allow preemption - if a needed resource is held
by another process, which is also waiting on
some resource, steal it. Otherwise wait.
Circular Wait :
• Impose a total ordering of all resource types,
and require that each process requests resources
1 April 2024 74
Deadlock Avoidance
• Requires that the system has some additional a
priori information available
• Simplest and most useful model requires that each
process declare the maximum number of resources
of each type that it may need
• The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to ensure that
there can never be a circular-wait condition
• Resource-allocation state is defined by the number
of available and allocated resources, and the
maximum demands of the processes
1 April 2024 75
Safe State
• When a process requests an available resource, system
must decide if immediate allocation leaves the system in
a safe state
1 April 2024 77
Safe State
1 April 2024 78
Avoidance algorithms
• For a single instance of a resource type, use a
resource-allocation graph
• For multiple instances of a resource type, use the
banker’s algorithm
1 April 2024 79
Resource-Allocation Graph with Claim Edges
Assignme
nt Reque
edge st
edge
Clai Claim
m edge
edge
1 April 2024 80
Unsafe State In Resource-Allocation Graph
Assignme
nt Reque
edge st
edge
Assignme
Clai nt
m edge
edge
1 April 2024 81
Resource-Allocation Graph Algorithm
• Suppose that process Pi requests a resource Rj
1 April 2024 82
Banker’s Algorithm
• Multiple instances
• Each process must a priori claim maximum use
• When a process requests a resource it may
have to wait
• When a process gets all its resources it must
return them in a finite amount of time
1 April 2024 83
Data Structures for the Banker’s Algorithm
4.If Finish [i] == true for all i, then the system is in a safe
state
1 April 2024 85
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj
1 April 2024 86
Example of Banker’s Algorithm
• 5 processes P0 through P4;
3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
1 April 2024 87
Example (Cont.)
• The content of the matrix Need is defined to be Max –
Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4,
P2, P0> satisfies safety criteria
1 April 2024 88
Example
P1 Request (1,0,2)
• Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true
Allocation Need Available
ABC ABC ABC
P0 0 1 0 7 4 3 2 3 0
P1 302 020
P2 3 0 2 600
P3 2 1 1 0 1 1
P4 0 0 2 431
• Executing safety algorithm shows that sequence < P1, P3, P4, P0,
P2> satisfies safety requirement
1 April 2024 89
Deadlock Detection and Recovery
• Allow system to enter deadlock state
• Detection algorithm
• Recovery scheme
1 April 2024 90
Single Instance of Each Resource Type
• Maintain wait-for graph
• Nodes are processes
• Pi → Pj if Pi is waiting for Pj
1 April 2024 91
Single Instance of Each Resource Type
Corresponding wait-for
Resource-Allocation Graph
graph
1 April 2024 92
Several Instances of a Resource Type
• Available: A vector of length m indicates the number of
available resources of each type
• Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each process
• Request: An n x m matrix indicates the current request of
each process. If Request [i][j] = k, then process Pi is
requesting k more instances of resource type Rj.
1 April 2024 93
Detection Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi ≠ 0, then
Finish[i] = false; otherwise, Finish[i] = true
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1 ≤ i ≤ n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then Pi is
deadlocked
1 April 2024 94
Recovery from Deadlock
Process Termination :
1 April 2024 95
Recovery from Deadlock
Resource Preemption :
1 April 2024 96
THANK YOU