Unit-2 Merged File
Unit-2 Merged File
CPU Scheduling
By
Dr Maj R B Kallam
Topics to be covered
• Scheduling Criteria
• Scheduling Algorithms
• Multiple-Processor Scheduling
– The currently running process may be interrupted and moved to the ready
state by the Operating System.
– The decision to preempt may be performed when a new process arrives,
when an interrupt occurs or periodically on the basis of clock interrupt.
A Presentation on Operating
systems by Dr Maj RB 6
Kallam
First-Come, First-Served (FCFS) Scheduling
• It is a non preemptive algorithm ,in this CPU is allocated to the processes
based on FCFS.
• Suppose that the processes arrive in the order: P1 , P2 , P3
• Assume that the arrival time is ‘0’ for all processes, calculate Turnaround time
(Tr), Waiting time (Wt), Average Tr and Average Wt.
P1 0 24 24 24 0
P2 0 3 27 27 24
P3 0 3 30 30 27
ATr=81/3=27 AWt=51/3=17
11
A Presentation on Operating systems by Dr Maj RB Kallam
Process Arrival Burst Finishing Turnaround Waiting Time
ID Time (At) time (Bt) Time (Ft) Time (Tr=Ft-At) (Wt=Tr-Bt)
P1 0 7 7 7 0
P2 2 4 12 10 6
P3 4 1 8 4 3
P4 5 4 16 11 7
ATr=32/4=8 AWt=16/4=4
14
A Presentation on Operating systems by Dr Maj RB Kallam
Process Arrival Burst Finishing Turnaround Waiting Time
ID Time (At) time (Bt) Time (Ft) Time (Tr=Ft-At) (Wt=Tr-Bt)
P1 0 7 16 16 9
P2 2 4 7 5 1
P3 4 1 5 1 0
P4 5 4 11 6 2
ATr=28/4=7 AWt=12/4=3
Disadvantage:
Starvation – low priority processes may never execute if we
continue to accept highest priority processes into the queue.
P1 10 3 16 16 6
P2 1 1 1 1 0
P3 2 4 18 18 16
P4 1 5 19 19 18
P5 5 2 6 6 1
ATr=60/5=12 AWt=41/5=8.2
A Presentation on Operating systems by Dr Maj RB Kallam 19
• Ex Q2: If the burst time and priority for 5 processes are as
given below, apply Non Preemptive Priority scheduling and
calculate Turnaround time (Tr), Waiting time (Wt), Average Tr
and Average Wt.
21
A Presentation on Operating systems by Dr Maj RB Kallam
The Gantt Chart for the schedule is:
P1 P1 P1 P4 P1 P2 P3
0 1 2 3 7 14 17 22
P2 1 3 3 17 16 13
P3 2 5 4 22 20 15
P4 3 4 1 7 4 0
ATr=54/4=13.5 Awt=32/4 =8
P1 20 20 20 0
P2 35 90 90 55
P3 25 95 95 70
P4 15 75 75 60
ATr=280/4=70 AWt=185/4=46.25
A Presentation on Operating
systems by Dr Maj RB 30
Kallam
Multilevel Feedback Queue
• A process can move between the various queues
• Multilevel-feedback-queue scheduler defined by the following
parameters:
– number of queues
– scheduling algorithms for each queue
– method used to determine when to upgrade a process to
higher priority queue.
– method used to determine when to demote a process to
lower priority queue.
– method used to determine which queue a process will
enter when that process needs service
System calls are the only entry points into the kernel system. All
programs needing resources must use system calls.
For example if we need to write a program code to read data from one
file, copy that data into another file. The first information that the
program requires is the name of the two files, the input and output
files.
System call sequence to copy the contents of one file to another file
Typically,
Fig: The handling of a user application invoking the open() system call
Types of System calls
Here are the five types of system calls used in OS:
Process Control
File Management
Device Management
Information Maintenance
Communications
Process Control
The types of system calls provided by an operating system: under process and
job control are:
File Management
The types of system calls provided by an operating system: under File
manipulations are:
Create a file
Delete file
Open and close file
Read, write, and reposition
Get and set file attributes
Device Management
The types of system calls provided by an operating system: under device
management are:
Communication:
get permission
set permission
allow user
deny user
Processes use this system call to create processes that are a copy of
themselves. With the help of this system Call parent process creates a
child process, and the execution of the parent process will be
suspended till the child process executes.
exec()
The exec() system call is used to make the processes. When the exec()
function is used, the currently running process is terminated and
replaced with the newly formed process. In other words, only the new
process persists after calling exec().
kill():
exit():
The exit() system call is used to terminate program execution.
Specially in the multi-threaded environment, this call defines that the
thread execution is complete. The OS reclaims resources that were
used by the process after the use of exit() system call.
Unit – 2 Part - 3
Deadlocks
By
Prof. RB Kallam
Topics to be covered
• Deadlocks
• System Model
• Deadlocks Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
• Necessary Conditions:
• Mutual exclusion: At least one resource must be held in
a non sharable mode; that is only one process at a time
can use the resource.
6
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
• No preemption: Resources cannot be preempted; that
is a resource can be released only voluntarily by the
process holding it, after that process has completed
its task.
P1 R1 P2 R3 P3 R2 P1
P2 R3 P3 R2 P2
P1 P2 P3
R2
R4
Resource allocation graph with a dead lock
13
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
•Mutual exclusion:
•Mutual exclusion means only one process can
use the resource at a time, it means resources
are not sharable by the number of processes at
a time.
•We can deny this condition with simple
protocol i.e. “Convert the all non sharable
resources to sharable resource". So this
condition is not satisfied by the deadlock,
hence we can prevent the deadlock.
•But it is not possible all the time.
16
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Circular wait:
• One way to ensure that the circular wait condition never
holds is to impose a total ordering of all resource types,
and to require that each process request resources in
an increasing order.
• For this we have two protocols:
• Whenever “ a processes request an instance of resource
type Rj, it has released any resource Ri such that
F(Ri)>F(Rj).
• A process can initially request any number of instances of
a resource type, say Ri. After that, the process can
request the instances of resource type Rj, if and only if
• F(Rj)>F(Ri)”.
• If these two protocols are used, then the circular wait
condition cannot hold.
17
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Deadlock avoidance:
• It is a dynamic approach to escape from deadlock .
Safe
19
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
• Allocation:
• An n x m matrix defines the number of resources of
each type currently allocated to each process.
• If Allocation [ i , j ] = k, then process Pi is currently
allocated k instances of resource type Rj.
• Need:
• An n x m matrix indicates the remaining resource need
of each process.
• If Need [ i , j ] = k, then Pi may need k more instances
of resource type Rj to complete its task.
• Note that Need[ i , j ]=Max[ i , j ] –Allocation [i, j ].
20
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Safety Algorithm:
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize work:= Available. and Finish [ i]:=false; For i= 0,1,2,..n-1.
2. Find an i such that both
Finish[i] == false
Need i <= Work.
If If no such exist, go to step4.
3. Work:= Work + Allocation i
Finish[i] := true
go to step 2
4. If Finish [i] == True, for all i, then the system is in safe state.
21
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Algorithm – Example:
• Consider a system with 5 processes P0,P1,P2,P3 and P4, and 3
resources A,B, C. Resource type A has 10 instances, Resources type
B has 5 instances and Resources C has 7 instances. Suppose that, at
time, the following snap shot of the system has been taken.
Allocation MAX
Calculate : A B C A B C
•Available matrix
•Need matrix P0 0 1 0 7 5 3
•And find whether the system P1 2 0 0 3 2 2
is in the safe state or not. P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
22
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Process Allocation Max Need (Max-
ID Allocation)
A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
7 2 5
A B C
Total 10 5 7
Execution sequence for the
Allocated -7 -2 -5
system to be in safe state:
P1, P3,P4,P0,P2
Available 3 3 2
23
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
A B C
Available (X) 3 3 2
Resource allocated to P1(Y) 2 0 0
Available (X+Y) 5 3 2
Resource allocated to P3 2 1 1
Available 7 4 3
Resource allocated to P4 0 0 2
Available 7 4 5
Resource allocated to P0 0 1 0
Available 7 5 5
Resource allocated to P2 3 0 2
Available 10 5 7
24
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Ex Q: Consider the following snapshot of a system:
Proces Available
Allocation Max
s ID
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Answer the following questions using the banker’s algorithm:
a. What is the content of the matrix Need?
b. Is the system in a safe state?
c. If a request from the process P1 arrives for (0,4,2,0) can the request be
granted immediately?
25
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Proces
s ID Allocation Max
Need
Matrix
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 0 0 0 0
P1 1 0 0 0 1 7 5 0 0 7 5 0
P2 1 3 5 4 2 3 5 6 1 0 0 2
P3 0 6 3 2 0 6 5 2 0 0 2 0
P4 0 0 1 4 0 6 5 6 0 6 4 2
2 9 10 12
A B C D
Available 1 5 2 0
Allocated 2 9 10 12
Total Resources 3 14 12 12
26
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
A B C D
Available (X) 1 5 2 0
Available (X+Y) 1 5 3 2
Resource allocated to P2 1 3 5 4
Available 2 8 8 6
Resource allocated to P3 0 6 3 2
Available 2 14 11 8
Resource allocated to P4 0 0 1 4
Available 2 14 12 12
Resource allocated to P1 1 0 0 0
Available
3 14 12 12
28
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
P5
R1 R3 R4 P5
P1 P2 P3 P1 P2 P3
R2 P4 R5 P4
Resource allocation graph Corresponding wait - for graph
29
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
•Several instances of a resource type:
• We need a deadlock detection algorithm with
several time varying data structures that are
similar to those used in banker’s algorithm.
• 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.
30
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Banker's algorithm for Deadlock detection
• 1. Let Work and Finish be vectors of length m and n, respectively.
Initialize work:= Available. For i= 1,2,..n, if Allocation = 0, then Finish [
i]:=false; otherwise, Finish[i]:= true.
• 2. Find an index i such that both
• Finish[i] == false
• Request i <= Work. If no
such exist, go to step4.
• 3. Work:= Work + Allocation i
Finish[i] := true go to step 2
• 4. If Finish [i] = false, for some i, 1<= i <= n, then the system is in a
deadlock state. Moreover, Finish [ i]:=false, then process Pi is
deadlocked.
• Note: This requires an order of mxn2 operations to detect whether the
system is in deadlocked state.
31
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Q: Consider a system with 5 processes P0,P1,P2,P3 and P4, and 3
resources A,B, C. Resource type A has 7 instances, Resources type B
has 2 instances and Resources C has 6 instances. Suppose that, at time
T0, we have the following resource allocation state.
32
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Allocation Request Available Max (Allocation+ Req)
AB C AB C AB C AB C
P0 0 1 0 0 0 0 0 0 0 010
P1 2 0 0 2 0 2 402
P2 3 0 3 0 0 0 303
P3 2 1 1 1 0 0 311
P4 0 0 2 0 0 2 004
7 2 6
A B C
Available 0 0 0
Allocated 7 2 6
Total Resources 7 2 6
33
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
A B C
Available (X) 0 0 0
Resource allocated to P0(Y) 0 1 0
Available (X+Y) 0 1 0
Resource allocated to P2 3 0 3
Available 3 1 3
Resource allocated to P3 2 1 1
Available 5 2 4
Resource allocated to P4 0 0 2
Available 5 2 6
Resource allocated to P1 2 0 0
Available 7 2 6
35
A Presentation on Operating systems by Dr Ravindra Babu Kallam, KITS Singapur
Recovery from Deadlock:
•Process Termination
• Abort all deadlocked processes: This method
clearly will break the deadlock cycle, but at great
expense.
• Abort one process at a time until the deadlock
cycle is eliminated: This method incurs
considerable overhead, since, after each process
is aborted, a deadlock-detection algorithm must
be invoked to determine whether any processes
are still deadlocked.