Download as pdf or txt
Download as pdf or txt
You are on page 1of 93

Noida Institute of Engineering and Technology,

Greater Noida

Operating System
KCS401

Unit: 3

CPU Scheduling
Dr. C S Yadav
Professor and Head CSE
B Tech 4th Sem

Dr C S Yadav KCS401 OS Unit Number:3 1


13/05/2020
Course Objectives
• To learn the fundamentals of Operating Systems.

• To understand what a process is and how processes are


synchronized and scheduled.

• To understand different approaches to memory


management.

• Students should be able to use system calls for managing


processes, memory and the file system.

• To understand the structure and organization of the


file system.
Dr C S Yadav KCS401 OS Unit Number:3
13/05/2020 2
Course Outcomes
At the end of semester, students will be able to

CO1: Understand the structure and functions of OS

CO2: Learn about Processes, Threads and Scheduling


algorithms

CO3: Understand the principles of concurrency and Deadlocks

CO4: Learn various memory management scheme


13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 3
CO-PO Mapping

OPERATING SYSTEM(KCS-401)
CODE PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12

KCS401.1 3 3 2 2 1 2 - 2 3 2 2 3

KCS401.2 3 3 3 2 2 3 2 2 3 - 1 3

KCS401.3 3 3 2 2 2 2 2 2 2 3 1 3

KCS401.4 3 2 2 3 1 2 2 - 2 - 2 3

KCS401.5 3 1 2 2 2 2 - - 2 2 2 3

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 4


CO- PSO Mapping

Program Specific Outcomes


Course
Outcomes PSO1 PSO2 PSO3 PSO4

KCS401.1 2 1 2 2
KCS401.2 2 2 1 2
KCS401.3 2 3 3 2
KCS401.4 2 2 1 2
KCS401.5 2 2 2 2

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 5


Prerequisite and Recap

• Basic knowledge of computer fundamentals.

• Basic knowledge of computer organization.

• Memory hierarchy

• Cache Organization

• Interrupt

• Registers

• Associative memory

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 6


Unit-3 Content
• Process Concept
• Process State
• Process Transition Diagram
• Process Control Block (PCB)
• Thread
User thread
Kernel thread
• Single and Multithreaded Processes
• Multithreading Models
Many-to-One
One-to-One
Many-to-Many Model
• Types of Schedulers
• Context Switching

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 7


Unit-3 Content
• CPU Scheduling Algorithms
First Come First Serve(FCFS) Scheduling.
Shortest-Job-First(SJF) Scheduling.
Priority Scheduling.
Round Robin(RR) Scheduling.
Multilevel Queue Scheduling.
Multilevel Feedback Queue Scheduling.
Multiple-Processor Scheduling
Real time scheduling
• Deadlock
• Deadlock characterization
• Deadlock Prevention
• Deadlock Avoidance
• Recovery from Deadlock
13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 8
Process Concept(CO2)

• An operating system executes a variety of programs:


Batch system – jobs
Time-shared systems – user programs or tasks
• We uses the terms job and process almost interchangeably.
• Process – a program in execution; process execution must
progress in sequential fashion.
• A process includes:

program counter
stack
data section

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 9


Process State(CO2)

As a process executes, it changes state


• New: The process is being created.
• Running: Instructions are being executed.
• Waiting: The process is waiting for some event to occur.
• Ready: The process is waiting to be assigned to a processor
• Terminated: The process has finished execution.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 10


Process Transition Diagram(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 11


Process Control Block (PCB)(CO2)
Information associated with each process.
• Process ID

• Process state

• Program counter

• CPU registers

• CPU scheduling information

• Memory-management information

• Accounting information

• I/O status information

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 12


Process Control Block (PCB)(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 13


Thread(CO2)

•A thread is a basic unit of CPU utilization


•Thread is an execution unit which consists of its own
program counter, a stack, and a set of registers.
•Threads are also known as Lightweight processes
•Threads are popular way to improve application
through parallelism.
•As each thread has its own independent resource for
process execution, multpile processes can be executed
parallely by increasing number of threads.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 14


Single and Multithreaded Processes(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 15


Multithreaded Server Architecture (CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 16


Benefits of Multithreading(CO2)

• Responsiveness – may allow continued execution if part of


process is blocked, especially important for user interfaces

• Resource Sharing – threads share resources of process, easier


than shared memory or message passing

• Economy – cheaper than process creation, thread switching


lower overhead than context switching

• Scalability – process can take advantage of multiprocessor


architectures

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 17


User Threads(CO2)

User Threads

• Thread management done by user-level threads library

• No need for kernel intervention

• Drawback : all may run in single process. If one blocks, all


block.

• Examples

•POSIX Pthreads
•Mach C-threads
•Solaris threads
13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 18
Kernel Threads(CO2)
Kernel Threads
• Supported by the Kernel

• Generally slower to create than user threads

• If one blocks another in the application can be run

• Can be scheduled on different CPUs in multiprocessor

• Examples
•Windows 95/98/NT/2000
•Solaris
•Tru64 UNIX
•BeOS
•Linux

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 19


Multithreading Models(CO2)
• Many-to-One
• One-to-One
• Many-to-Many

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 20


Many-to-One(CO2)
• Many user-level threads mapped to single kernel thread.

• Used on systems that do not support kernel threads.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 21


One-to-One(CO2)

• Each user-level thread maps to kernel thread.


• Examples

• Windows 95/98/NT/2000

• OS/2

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 22


Many-to-Many Model(CO2)
• Many user-level threads mapped to single kernel thread

• One thread blocking causes all to block

• Multiple threads may not run in parallel on muticore system


because only one may be in kernel at a time

• Few systems currently use this model

• Examples:

• Solaris Green Threads

• GNU Portable Threads

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 23


Types of Schedulers(CO2)

• 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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 24


Addition of Medium Term Scheduling(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 25


Process Scheduling Queues(CO2)

• Job queue – set of all processes in the system.


• Ready queue – set of all processes residing in main
memory, ready and waiting to execute.
• Device queues – set of processes waiting for an I/O
device.
• Processes migrate between the various queues

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 26


Representation of Process Scheduling(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 27


CPU Scheduler(CO2)

Selects from among the processes in memory (i.e. Ready Queue)


that are ready to execute, and allocates the CPU to one of
them.
1. Preemptive: allows a process to be interrupted in the midst
of its CPU execution, taking the CPU away to another
process

2. Non- Preemptive: ensures that a process relinquishes


control of CPU when it finishes with its current CPU burst

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 28


CPU Scheduler(CO2)
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.

4. Terminates.

• Preemptive: allows a process to be interrupted

• Non- Preemptive: allows a process finishes with its current CPU


burst

• Scheduling under 1 and 4 is nonpreemptive.

• All other scheduling is preemptive.


13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 29
Context Switching(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 30


Dispatcher(CO2)

• Dispatcher module gives control of the CPU to the


process selected by the short-term scheduler; this
involves:

• switching context

• switching to user mode

• jumping to the proper location in the user program to

restart that program

• Dispatch latency – time it takes for the dispatcher to stop


one process and start another running.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 31


Scheduling Criteria(CO2)
• CPU utilization – keep the CPU as busy as possible

• Throughput – Number of processes that complete their


execution per time unit

• Turnaround time – amount of time to execute a particular


process (finishing time – arrival time)

• 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)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 32


Optimization Criteria (CO2)
• Max CPU utilization

• Max throughput

• Min turnaround time

• Min waiting time

• Min response time

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 33


CPU Scheduling algorithms(CO2)

CPU Scheduling Algorithms


1. First Come First Serve(FCFS) Scheduling.
2. Shortest-Job-First(SJF) Scheduling.
3. Priority Scheduling.
4. Round Robin(RR) Scheduling.
5. Multilevel Queue Scheduling.
6. Multilevel Feedback Queue Scheduling.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 34


First-Come, First-Served (FCFS) Scheduling(CO2)

Process Burst Time


P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30

Waiting time for P1 = 0; P2 = 24; P3 = 27


Average waiting time: (0 + 24 + 27)/3 = 17

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 35


FCFS Scheduling(Cont.) (CO2)
Suppose that the processes arrive in the order
P2 , P3 , P1 .
The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

Waiting time for P1 = 6; P2 = 0; P3 =3


Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case.
Convoy effect short process behind long process

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 36


Shortest-Job-First (SJR) Scheduling(CO2)

• 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.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 37


Example of Non-Preemptive SJF(CO2)
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

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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 38


Example of Preemptive SJF(CO2)
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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 39


Priority Scheduling(CO2)
• 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 is Starvation – low priority processes may
never execute.
• Solution is Aging – as time progresses increase the
priority of the process.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 40


Round Robin (RR)(CO2)
• 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.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 41


Example of RR with Time Quantum = 20(CO2)
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.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 42


Multilevel Queue(CO2)
• Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
• Each queue has its own scheduling algorithm,
foreground – RR
background – FCFS
• Scheduling must be done between the queues.
• Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
• Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR
• 20% to background in FCFS

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 43


Multilevel Queue Scheduling(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 44


Multilevel Feedback Queue(C02)
• A process can move between the various queues; aging can be
implemented this way.

• 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

• method used to determine when to demote a process

• method used to determine which queue a process will enter

when that process needs service

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 45


Multilevel Feedback Queues(CO2)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 46


Example of Multilevel Feedback Queues(CO2)

• Three queues:
• Q0 – time quantum 8 milliseconds
• Q1 – time quantum 16 milliseconds
• Q2 – FCFS
• Scheduling
• A new job enters queue Q0 which is served FCFS.
When it gains CPU, job receives 8 milliseconds. If it
does not finish in 8 milliseconds, job is moved to
queue Q1.
• At Q1 job is again served FCFS and receives 16
additional milliseconds. If it still does not
complete, it is preempted and moved to queue Q2.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 47


Multiple-Processor Scheduling(CO2)

• CPU scheduling more complex when multiple CPUs are

available.
• Homogeneous processors within a multiprocessor.

• Load sharing or load balancing

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 48


Real-Time Scheduling(CO2)
• Hard real-time systems – required to complete a critical
task within a guaranteed amount of time.

• Soft real-time computing – requires that critical processes


receive priority over less fortunate ones.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 49


Deadlock(C03)
•In a multiprogramming system, processes request resources. If those
resources are being used by other processes then the process enters a
waiting state. However, if other processes are also in a waiting state,
we have deadlock.

•A set of processes is in a deadlock state if every process in the set is


waiting for an event (release) that can only be caused by some other
process in the same set.
Example
-System has 2 disk drives
-P1 and P2 process each hold one disk drive and each needs
another one
13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 50
Deadlock Characterization(CO3)
Deadlock can arise if four conditions hold simultaneously
•Mutual exclusion: only one process at a time can use a resource .
• Hold and wait: a process holding at least one resource is waiting to
acquire additional resources held by other processes .
•No preemption: a resource can be released only voluntarily by the
process holding it, after that process has completed its task .
•Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes
such that P0 is waiting for a resource that is held by P1, P1 is waiting
for a resource that is held by P2, …, Pn–1 is waiting for a resource that
is held by Pn, and Pn is waiting for a resource that is held by P0.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 51


Resource-Allocation Graph(CO3)
A resource allocation graph is a set of vertices V and a set of edges E
such that:
•V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes in
the system
R = {R1, R2, …, Rm}, the set consisting of all resource types in
the system

•request edge – directed edge Pi → Rj

•assignment edge – directed edge Rj → Pi

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 52


Resource-Allocation Graph(C03)
• Process

• Resource Type with 4 instances

• Pi requests instance of Rj
Pi

Pi

• Pi is holding an instance of Rj Pi

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 53


Resource Allocation Graph With A Deadlock(C03)
Before P3 requested an After P3 requested an
instance of R2 instance of R2

A Cycle In the Graph May cause Deadlock

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 54


Summary(C03)

•If graph contains no cycles  no deadlock


•If graph contains a cycle 
•if only one instance per resource type, then deadlock
•if several instances per resource type, possibility of
deadlock

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 55


Methods for Handling Deadlocks(CO3)
 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 UNIX

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 56


Deadlock Prevention(CO3)
•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 :

• if no resource were ever assigned to a single process exclusively


,we would never have deadlock.

• Shared entities (read only files) don't need mutual exclusion (and
aren’t susceptible to deadlock.)

• Prevention not possible, since some devices are naturally non-


sharable

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 57


Deadlock Prevention(CO3)
Hold And Wait :
we must guarantee that whenever a process requests a
resource, it does not hold any other resources

• Require a process to request and be allocated all its


resources before it begins execution

• allow a process to request resources only when the


process has none

Result: Low resource utilization; starvation possible

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 58


Deadlock Prevention(CO3)
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 in an increasing order of
enumeration.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 59


Deadlock Avoidance(CO3)
•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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 60


Safe State(CO3)
•When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state

•System is in safe state if there exists a sequence <P1, P2, …, Pn> of


all the processes in the systems such that for each P i, the resources
that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j < i
That is:
•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

•When Pi terminates, Pi +1 can obtain its needed resources, and


so on
13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 61
Safe State(CO3)

•If a system is in safe state  no deadlocks

•If a system is in unsafe state  possibility of deadlock

•Avoidance  ensure that a system will never enter an


unsafe state.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 62


Safe State(CO3)

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 63


Avoidance algorithms(CO3)

•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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 64


Resource-Allocation Graph with Claim Edges(CO3)

Assignment
edge Request
edge

Claim Claim
edge edge

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 65


Unsafe State In Resource-Allocation Graph (CO3)

Assignment
edge Request
edge

Assignment
Claim edge
edge

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 66


Resource-Allocation Graph Algorithm(CO3)

•Suppose that process Pi requests a resource Rj


•The request can be granted only if converting the
request edge to an assignment edge does not result in
the formation of a cycle in the resource allocation graph

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 67


Banker’s Algorithm(CO3)

•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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 68


Data Structures for the Banker’s Algorithm (CO3)
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.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 69


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

2.Find an 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
13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 70
Resource-Request Algorithm for Process Pi(CO3)
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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 71


Example of Banker’s Algorithm(CO3)
• 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 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 72


Example (Cont.)(CO3)
• 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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 73


Example: (CO3)
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 743 230
P1 302 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

• Executing safety algorithm shows that sequence < P1, P3, P4, P0,
P2> satisfies safety requirement

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 74


Deadlock Detection(CO3)

•Allow system to enter deadlock state


•Detection algorithm
•Recovery scheme

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 75


Single Instance of Each Resource Type(CO3)
•Maintain wait-for graph
•Nodes are processes
•Pi → Pj if Pi is waiting for Pj

•Periodically invoke an algorithm that searches for a cycle in the


graph. If there is a cycle, there exists a deadlock

•An algorithm to detect a cycle in a graph requires an order of n2


operations, where n is the number of vertices in the graph

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 76


Single Instance of Each Resource Type(CO3)

Resource-Allocation Graph Corresponding wait-for graph

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 77


Several Instances of a Resource Type(CO3)
•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.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 78


Detection Algorithm(CO3)
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

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 79


Recovery from Deadlock(CO3)
Process Termination :

•Abort all deadlocked processes


•Abort one process at a time until the deadlock cycle is eliminated

In which order should we choose to abort?


1. Priority of the process
2. How long process has computed, and how much longer to
completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 80


Recovery from Deadlock(CO3)
Resource Preemption :

•Selecting a victim – minimize cost

•Rollback – return to some safe state, restart process for that


state

•Starvation – same process may always be picked as victim,


include number of rollback in cost factor

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 81


Faculty Video Links, Youtube & NPTEL Video Links
and Online Courses Details

Youtube/other Video Links

• https://www.youtube.com/watch?v=_zOTMOubT1M
• https://www.youtube.com/playlist?list=PLmXKhU9FNesSF
vj6gASuWmQd23Ul5omtD
• https://www.youtube.com/watch?v=x_UpLHXF9dU
• https://www.youtube.com/watch?v=cviEfwtdcEE
• https://nptel.ac.in/courses/106108101

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 82


Daily Quiz
1. A solution to the problem of indefinite blockage of low –
priority processes is ____________
A. Starvation
B. Wait queue
C. Ready queue
D. Aging
2. Round robin scheduling falls under the category of
____________
A. Non-preemptive scheduling
B. Preemptive scheduling
C. All of the mentioned
D. None of the mentioned

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 83


Weekly Assignment
• Differentiate between preemptive and nonpreemptive
scheduling.

• List out the necessary conditions for deadlock to occur..

• Define PCB

• List out the various scheduling criteria for CPU


Scheduling.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 84


Old Question Papers

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 85


Old Question Papers

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 86


Old Question Papers

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 87


Expected Questions for University Exam
1. Draw the labeled process state transition diagram with
describing the various process states.

2. Write a note on Deadlock Prevention.

3. Explain Multi Level Queue Scheduling.

4. Explain Short term, Middle term and Long term Schedulers.

5. Write down the steps of Deadlock Detection

6. Define PCB.

7. List out the various scheduling criteria for CPU Scheduling.

8. List out the necessary conditions for deadlock to occur.

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 88


Expected Questions for University Exam
9. Consider the following set of four processes, with the length of CPU
burst time given in milliseconds
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

Draw Gannt chart and find average waiting time and response time
using
• FCFS
• Round Robin (quantum=2)
• SJF and SRTF

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 89


Expected Questions for University Exam
10. Let us consider the following snapshot
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
• What is the content of matrix need?
• Is the system in a safe state or not?
• If a request from process P1 arrives for (1,0,2) can request be
granted immediately or not?

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 90


Summary
In this module, we have studied the following:

•Process Concept
•Process State
•Process Transition Diagram
•Process Control Block (PCB)
•Thread
User thread
Kernel thread
•Single and Multithreaded Processes
•Multithreading Models
Many-to-One
One-to-One
Many-to-Many Model
•Types of Schedulers
•Context Switching
13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 91
Summary
•CPU Scheduling Algorithms
First Come First Serve(FCFS) Scheduling.
Shortest-Job-First(SJF) Scheduling.
Priority Scheduling.
Round Robin(RR) Scheduling.
Multilevel Queue Scheduling.
Multilevel Feedback Queue Scheduling.
Multiple-Processor Scheduling
Real time scheduling
• Deadlock
• Deadlock characterization
• Deadlock Prevention
• Deadlock Avoidance
• Recovery from Deadlock

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 92


References
Books :
1. Silberschatz, Galvin and Gagne, “Operating Systems
Concepts”, Wiley
2. SibsankarHalder and Alex A Aravind, “Operating Systems”,
Pearson Education
3. Harvey M Dietel, “ An Introduction to Operating System”,
Pearson Education
4. D M Dhamdhere, “Operating Systems : A Concept
basedApproach”, McGraw Hill.
5. Charles Crowley, “Operating Systems: A Design-Oriented
Approach”, Tata McGraw Hill Education”.
6. Stuart E. Madnick & John J. Donovan, “ Operating Systems”,
Tata McGraw

13/05/2020 Dr C S Yadav KCS401 OS Unit Number:3 93

You might also like