0% found this document useful (0 votes)
4 views73 pages

Unit 2 - Os-Psna - Part 2

The document covers various aspects of process management, including CPU scheduling algorithms such as FCFS, SJF, and RR, as well as process synchronization and deadlock handling. It provides detailed explanations of scheduling criteria, examples, and homework problems to illustrate the concepts. Additionally, it discusses priority scheduling and its implications, including issues like starvation and solutions like aging.

Uploaded by

vasanthkv1982004
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)
4 views73 pages

Unit 2 - Os-Psna - Part 2

The document covers various aspects of process management, including CPU scheduling algorithms such as FCFS, SJF, and RR, as well as process synchronization and deadlock handling. It provides detailed explanations of scheduling criteria, examples, and homework problems to illustrate the concepts. Additionally, it discusses priority scheduling and its implications, including issues like starvation and solutions like aging.

Uploaded by

vasanthkv1982004
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/ 73

UNIT II PROCESS MANAGEMENT

Processes : Operation on Process , Process Scheduling, Inter Process


Communication
CPU Scheduling-Algorithms
Multithreading: overview – Multithreading models
Process Synchronization: Critical Section Problem-Semaphore-Dining
Philosopher Problem
Deadlock: Deadlock characterization, Methods for handling deadlocks,
Deadlock Prevention, Deadlock Avoidance, Deadlock Detection and
Deadlock Recovery.
CASE STUDY - Linux Process Management
CPU Scheduling-Algorithms
Scheduling Criteria
• 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 a process has been
waiting in the ready queue
• Response time – amount of time it takes from when a
request was submitted until the first response is
produced, not output (for time-sharing environment)
CPU Scheduling Algorithms
• FCFS
• SJFS
• Priority Scheduling
• RR
• Multilevel Queue
• Multilevel Feedback Queue
Terms
• Arrival Time – Time at which process enter into Ready state.
• Burst Time
• Completion Time
• Turnaround Time :
Completion Time – Arrival Time
• Waiting Time
Turnaround Time – Burst Time
First-Come, First-Served (FCFS) Scheduling
• The process that requests CPU first is allocated the CPU first.
• Simple to write and understand
• FCFS is nonpreemptive
First-Come, First-Served (FCFS) Scheduling
• 1. Process Burst Time Gantt Chart
P1 24
P1 P2 P3
P2 3
P3 3 0 24 27 30

Process AT BT CT TAT WT

P1 0 24 24 24 0

P2 0 3 27 27 24

P3 0 3 30 30 27

AVG (24+27+30)/3 = (0+24+27)/3 =


27 17
First-Come, First-Served (FCFS) Scheduling
• 2. Process Burst Time
P2 3
P3 3
P1 24
Process AT BT CT TAT WT

P2 0 3 3 3 0

P3 0 3 6 6 3

P1 0 24 30 30 6

AVG (3+6+30)/3 = 13 (0+3+6)/3 = 3


First-Come, First-Served (FCFS) Scheduling
• Convey Effect - short process behind long process
• Disadvantage
First-Come, First-Served (FCFS) Scheduling
• 3. Process Arrival Time Burst Time
P1 0 20
P2 1 2
P3 1 1
Process AT BT CT TAT WT

P1 0 20 20 20 0

P2 1 2 22 21 19

P3 1 1 23 22 21

AVG 21 13.3
First-Come, First-Served (FCFS) Scheduling

• 3.
Process Arrival Burst
Time Time
P1 0 21
P2 0 3
P3 0 6
P4 0 2
FCFS
Proces AT BT CT TAT WT
s
P1 0 21 21 21 0
P2 0 3 24 24 21
P3 0 6 30 30 24
P4 0 2 32 32 30
Avg 26.75 18.75
4 . Example - FCFS
Answer - FCFS
Proces AT BT CT TAT WT
s
P1 0 2 2 2 0
P2 1 3 5 4 1
P3 2 5 10 8 3
P4 3 4 14 11 7
P5 4 6 20 16 10
8.2 4.2
Home work - FCFS
Process Arrival Time Burst Time
P1 0 80
P2 0 20
P3 0 10
P4 0 20
P5 0 50
FCFS -Home work 1 -Answer
• Average Waiting Time - 84 ms
• Average Turn Around time - 120 ms
FCFS -Home work 2
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
FCFS - Home work 2 -Answer
• Average Waiting Time – 8.75 ms
• Average Turn Around time – 15.25 ms
Shortest-Job-First (SJR) Scheduling
• Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with
the shortest time.
• Two schemes:
• nonpreemptive – once CPU given to the process it cannot
be preempted until completes its CPU burst.
• Preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waiting time
for a given set of processes.
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16

• Average waiting time = (0 + 6 + 3 + 7)/4 = 4


SJF (Non Preemptive)
• 2. Process Burst Time
P1 24
P2 3
P3 3
Process AT BT CT TAT WT

P2 0 3 3 3 0

P3 0 3 6 6 3

P1 0 24 30 30 6

AVG 13 3
Home work1 – SJF (Non Preemptive)
Process Arrival Time Burst Time
P1 0 80
P2 0 20
P3 0 10
P4 0 20
P5 0 50
SJF -Home work 1 -Answer
• Average Waiting Time - 38 ms
• Average Turn Around time - 74 ms
SJF -Home work 2
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
SJF - Home work 2 -Answer
• Average Waiting Time – 7.75 ms
• Average Turn Around time – 14.25 ms
Example of Preemptive SJF
(Shortest Remaining Time First)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

• Average waiting time = (9 + 1 + 0 +2)/4 - 3


2. SJF –(Preemptive)
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
2. SJF (Pre emptive)
Proce AT BT CT TAT WT
ss
P1 0 8 17 17 9

P2 1 4 5 4 0

P3 2 9 26 24 15

P4 3 5 10 7 2
AVG 13 6.5

1 1 1 2 5 7 9
P1 P2 P2 P2 P4 P1 P3
Homework 1- SJF –(Preemptive)
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
SJF (Pre emptive) - Home work 1 -Answer
• Average Waiting Time – 3 ms
• Average Turn Around time – 7 ms
Homework 2- SJF –(Preemptive)
Process Arrival Time Burst Time
P1 0 7
P2 2 6
P3 4 1
P4 5 2
SJF (Pre emptive) - Home work 2 -Answer
• Average Waiting Time – 3.75 ms
• Average Turn Around time – 7 ms
Round Robin (RR)
• Each process gets a small unit of CPU time (time quantum), usually
10-100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.
• If there are n processes in the ready queue and the time quantum is
q, then each process gets 1/n of the CPU time in chunks of at most q
time units at once. No process waits more than (n-1)q time units.
• Performance
• q large  FIFO
• q small  q must be large with respect to context switch, otherwise overhead
is too high.
Example 1: RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

• Typically, higher average turnaround than SJF,


but better response.
How a Smaller Time Quantum Increases Context Switches
Turnaround Time Varies With The Time Quantum
Example2. RR
Q=2
Process Arrival Time Burst Time
P1 0 3
P2 0 4
P3 0 3
Example 2. RR

Process AT BT CT TAT WT

P1 0 3 7 7 4

P2 0 4 10 10 6

P3 0 3 9 9 6

AVG 8.66 5.33


Homework 1- RR
Q=4
Process AT BT

P1 0 6
P2 0 5
P3 0 2
P4 0 3
P5 0 7
Homework 1- RR - Answer
Process AT BT CT TAT WT

P1 0 6 19 19 13
P2 0 5 20 20 15
P3 0 2 10 10 8
P4 0 3 13 13 10
P5 0 7 23 23 16
AVG 17 12.4
Homework 2- RR (complicated) -Q-3
Process AT BT

P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
Homework 2- RR (complicated) -
Answer
Process AT BT CT TAT WT

P1 5 5 32 27 22
P2 4 6 27 23 17
P3 3 7 33 30 23
P4 1 9 30 29 20
P5 2 2 6 4 2
P6 6 3 21 15 12
AVG 21.3 16
Practice examples - 1
• If the CPU scheduling policy is Round Robin with time quantum = 2 unit,
calculate the average waiting time and average turn around time.
Practice examples - 1
Practice examples - 2
• Average Turn Around time = (8 + 17 + 4 + 6 + 17 + 13) / 6 = 65 /
6 = 10.84 unit
• Average waiting time = (4 + 12 + 2 + 5 + 11 + 10) / 6 = 44 / 6 =
7.33 unit
Practice examples - 3
• Average Turn Around time = 21.33 unit
• Average waiting time = 16 unit
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority (smallest
integer  highest priority).
• Preemptive
• nonpreemptive
• SJF is a priority scheduling where priority is the predicted next CPU
burst time.
• Problem  Starvation – low priority processes may never execute.
• Solution  Aging – as time progresses increase the priority of the
process.
Priority Scheduling (Non Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 10 3
P2 0 1 1
P3 0 2 4
P4 0 1 5
P5 0 5 2
Priority Scheduling (Non Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 10 3 16 16 6
P2 0 1 1 1 1 0
P3 0 2 4 18 18 16
P4 0 1 5 19 19 18
P5 0 5 2 6 6 1
AVG 12 8.2
HW :Priority Scheduling (Non Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Priority Scheduling (Non Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 4 2 4 4 0
P2 1 3 3 15 14 11
P3 2 1 4 12 10 9
P4 3 5 5 9 6 1
P5 4 2 5 11 7 5
AVG 8.2 5.2
Priority Scheduling (Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 4 4
P2 1 3 3
P3 2 1 2
P4 3 5 1
P5 4 2 1
Priority Scheduling (Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 4 4 15 15 11
P2 1 3 3 12 11 8
P3 2 1 2 3 1 0
P4 3 5 1 8 5 0
P5 4 2 1 10 6 4
AVG 7.6 4.6
Hw : Priority Scheduling (Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 1 2
P2 1 7 6
P3 2 3 3
P4 3 6 5
P5 4 5 4
P6 5 15 10
P7 15 8 9
Hw: Priority Scheduling (Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 1 2 1 1 0
P2 1 7 6 22 21 14
P3 2 3 3 5 3 0
P4 3 6 5 16 13 7
P5 4 5 4 10 6 1
P6 5 15 10 45 40 25
P7 15 8 9 30 24 16
Avg 9
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.
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.

• Available: Vector of length m. If available [j] = k, there are k instances of


resource type Rj available.
• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj.
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated
k instances of Rj.
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of
Rj to complete its task.

Need [i,j] = Max[i,j] – Allocation [i,j].


Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work := Available
Finish [i] = false for i - 1,3, …, n.

2. Find and i such that both:


(a) Finish [i] = false
(b) Needi  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] = true for all i, then the system is in a safe state.
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. If Requesti  Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim.
2. If Requesti  Available, go to step 3. Otherwise Pi must wait, since
resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available := Available = Requesti;
Allocationi := Allocationi + Requesti;
Needi := Needi – Requesti;;
• If safe  the resources are allocated to Pi.
• If unsafe  Pi must wait, and the old resource-allocation state is restored
Example of Banker’s Algorithm
• 5 processes P0 through P4; 3 resource types A (10 instances),
B (5instances, and C (7 instances).
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 4 2 2
P4 0 0 2 5 3 3
Example (Cont.)
• The content of the matrix. Need is defined to be Max –
Allocation.
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 2 1 1
P4 5 3 1
• The system is in a safe state since the sequence < P1, P3,
P4, P0, P2> satisfies safety criteria.
Consider the system with 5 process P0 to P4 and resource types A,B,C. Resource
type A, B,C has 7,2,6 instances respectively. Find this system is in safe sate or not.

Allocation Request Available


ABC ABC ABC
P0 010 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002
• Request <= Available
• Available = Available + Allocation.
• The safe sequence is < P0, P2, P3, P1, P4>
Consider the following snapshot of a system
Allocation Max Available
ABCD ABCD ABCD
P0 0012 0012 1520
P1 1 0 0 0 1750
P2 1 3 5 4 2356
P3 0 6 3 2 0652
P4 0 0 1 4 0656
a) What is the content of the matrix Need?
b) Is the system in a safe state?
c) c) If a request from process P1 arrives for (0, 4, 2, 0), can
the request be granted immediately?
Need (max-Allocation)
A BCD
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 00 2 0
P4 06 4 2
• Safe sequence < p0,p2,p3,p4,p1>
• Final available (3,14,12,12)
Addition request – p1(0 4 2 0)
• request <=need
(0 4 2 0 ) <= (0 7 5 0)
• request <= available
(0 4 2 0 ) <= (1 5 2 0)
• new available = available – request
(1 5 2 0) - (0 4 2 0 ) = (1 1 0 0)
• New Allocation = Allocation + request
(1 0 0 0) + (0 4 2 0 ) = (1 4 2 0 )
• New Need = Need – Request
(0 7 5 0) - (0 4 2 0 ) = (0 3 3 0)
Allocation Need Available
ABCD ABCD ABCD
P0 0012 0 0 0 0 1100
P1 1 4 2 0 0 33 0
P2 1 3 5 4 1002
P3 0 6 3 2 0020
P4 0 0 1 4 0642
• Safe Sequence < p0, p2,p3,p4,p1> Final Available :
(3,14,12,12)
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.
Detection Algorithm (Cont.)

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.

• Algorithm requires an order of m x n2 operations to detect


whether the system is in deadlocked state.
Example of Detection Algorithm
• Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.

You might also like