Chapter-IV Process Scheduling
Chapter-IV Process Scheduling
OSY - 22516
UNIT-IV
CPU SCHEDULING & ALGORITHMS
• But they are complex and can lead the system to race
condition.
SCHEDULING TYPES
• B. Non-Preemptive Scheduling
• Once the CPU is assigned to a process then the processor do
not release until the completion of that process.
• They are simple and they cannot lead the system to race
condition.
• When a process enters the ready queue, its PCB is linked onto
the tail of the queue.
P1 P2 P3
0 24 27 30
0 3 6 30
• Example :
– CPU job burst times and ready queue order:
• P1: 20
• P2: 12
• P3: 8
• P4: 16
• P5: 4
P1: 40
P5 P3 P2 P4 P1
P2: 12
P3: 4 0 4 12 24 40 60
P4: 24
P5: 0
Average wait time: (0+4+12+24+40 )= 80/5 = 16
SCHEDULING ALOGORITHMS
• TYPES OF SJF SCHEDULING ALGORITHM
1. 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).
2. Non-preemptive
Once CPU is given to the process it cannot be preempted until
completes its CPU burst.
SCHEDULING ALOGORITHMS
• SJF-PREEMPTIVE ALGORITHM
PROCESS ARRIVAL TIME BURST TIME REMAINING STATUS
TIME
P1 0 7 5 SCHEDULED
P2 2 4 2 SCHEDULED
P3 4 1 0 DONE
P4 5 4 0 DONE
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
P1 P2 P3 P4
0 10 15 23 38
0 1 2 3 4 7 12 18 26
P3-9
35
P1 P2 P3 P4 P5
0 7 11 21 27 35
0 1 5 10 17 26
P1 P2 P3 P4
0 8 12 21 26
P1 P2 P3 P1 P4
0 1 6 14 23 38
P4 P3 P5 P1 P2
0 4 9 12 24 34
• 1. Internal Priority
– Use some measurable quantity or quantities to compute
the priority of a process.
– Like time limits, memory requirement, number of open
files.
• 2. External Priority
– They are set by criteria outside the operating system.
– Like importance of process.
SCHEDULING ALOGORITHMS
• Major disadvantage of priority scheduling algorithm is
indefinite blocking or starvation.
• A process that is ready to run but waiting for the CPU can be
considered blocked.
• The CPU scheduler picks the first process from the ready
queue, sets a timer to interrupt after 1 time quantum, and
dispatches the process.
SCHEDULING ALOGORITHMS
• EXAMPLE
PROCESS BURST TIME TIME QUANTUM
P1 53
P2 17
20
P3 68
P4 24
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
• P1 = 0 + (77-20) + (121-97) = 81
• P2 = 20
• P4 = 57 + (117-77) = 97
• Scheduling
ACCESSING
P1
WAITING FOR P1 TO
RELEASE PRINTER
P2
DEADLOCK CONDITIONS
• HOLD AND WAIT: a process holding at least one resource is
waiting to acquire additional resources held by other
processes.
P1 ACCESSING FAX
P2 ACCESSING PRINTER
P2
DEADLOCK CONDITIONS
• NO PREEMPTION: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task.
ACCESSING PRINTER
P1 I WILL ONLY RELEASE PRINTER ONCE
I FINISH MY TASK
WAITING FOR P1 TO
RELEASE PRINTER
P2
DEADLOCK CONDITIONS
• CIRCULAR WAIT: there exists a set {P0, P1, …, Pn} of waiting
P1 P2
DEADLOCK HANDLING
• A deadlock in operating system can be handled in following 4
different conditions.
• 1. Adopt methods for avoiding the deadlock.
• Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources
that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j<i.
– If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished.
– When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate.
DEADLOCK AVOIDANCE
• If a system is in safe state no deadlocks.
• Max: n x m matrix.
• If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj.
DEADLOCK AVOIDANCE
• 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].
DEADLOCK AVOIDANCE
Process MAX ALLOCATED NEED
A B C A B C A B C
P0 7 5 3 0 1 0 7 4 3
P1 3 2 2 2 0 0 1 2 2
P2 9 0 2 3 0 2 6 0 0
P3 2 2 2 2 1 1 0 1 1
P4 4 3 3 0 0 2 4 3 1
AVAILABLE
A (10) B (5) C (7)
3 3 2
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria.
DEADLOCK AVOIDANCE
• SAFETY ALGORITHM
Tells us whether or not a system is in a safe state
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 1,2, …, n.
4. If Finish [i] == true for all i, then the system is in a safe state.
DEADLOCK AVOIDANCE
Process ALLOCATED MAX NEED
A B C A B C A B C
P0 5 2 1 3 0 1 2 2 0
P1 3 2 2 0 2 1 3 0 1
P2 3 2 2 2 0 0 1 2 2
P3 7 3 1 2 1 0 5 2 1
P4 5 0 3 4 0 2 1 0 1
AVAILABLE
A (12) B (9) C (6)
1 6 2
• The system is in a safe state since the sequence < P2, P1, P0, P4, P3>
satisfies safety criteria.
DEADLOCK AVOIDANCE
• Process P2
• Need < = Available = (1,2,2) <= (1,6,2) is TRUE
• Available = Available – Need = (1,6,2) – (1,2,2) = (0,4,0)
• Total Available = Available + Allocated = (0,4,0) + (3,2,2) = (3,6,2)
• After P2 finishes its job.
• Process P1
• Need < = Available = (3,0,1) <= (3,6,2) is TRUE
• Available = Available – Need = (3,6,2) – (3,0,1) = (0,6,1)
• Total Available = Available + Allocated = (0,6,1) + (3,2,2) = (3,8,3)
• After P1 finishes its job.
• Process P0
• Need < = Available = (2,2,0) <= (3,8,3) is TRUE
• Available = Available – Need = (3,8,3) – (2,2,0) = (1,6,3)
• Total Available = Available + Allocated = (1,6,3) + (5,2,1) = (6,8,4)
• After P0 finishes its job.
DEADLOCK AVOIDANCE
• Process P4
• Need < = Available = (1,0,1) <= (6,8,4) is TRUE
• Available = Available – Need = (6,8,4) – (1,0,1) = (5,8,3)
• Total Available = Available + Allocated = (5,8,3) + (5,0,3) = (10,8,6)
• After P4 finishes its job.
• Process P3
• Need < = Available = (5,2,1) <= (10,8,2) is TRUE
• Available = Available – Need = (10,8,6) – (5,2,1) = (5,6,5)
• Total Available = Available + Allocated = (5,6,5) + (7,3,1) = (12,9,6)
• After P3 finishes its job.
DEADLOCK AVOIDANCE
RESOURCE REQUEST ALGORITHM
Request = 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 + Request;
Needi = Needi – Requesti;
AVAILABLE
A (10) B (5) C (7)
3 3 2