OPS-Chapter-4-CPU Scheduling and Algorithms
OPS-Chapter-4-CPU Scheduling and Algorithms
E-Notes
CPU scheduling is the process of selecting a process from the ready queue and assigning the CPU to it. CPU
scheduling algorithms decides which of the processes in the ready queue is to be allocated to the CPU.
Every process in OS that requests CPU service carries out the following sequence of actions:
In first action, join the ready queue and wait for CPU service.
In second action, execute (receive CPU service) for the duration of the current CPU burst or for the
duration of the time slice (timeout).
In third action, join the I/O queue to wait for I/O service or return to the ready queue to wait for more
CPU service.
In fourth action, terminate and exit if service is completed i.e., if there are no more CPU or I/O bursts.
If more service is required, return to the ready queue to wait for more CPU service.
Q. Describe CPU and I/O burst cycle with the help of diagram – 4m
Processes require alternate use of processor and I/O in a repetitive fashion. Each cycle consist of a CPU burst
followed by an I/O burst. A process terminates on a CPU burst. CPU-bound processes have longer CPU bursts
than I/O-bound processes.
I/O bound process: The process which spends more time in I/O operation than computation (time
spends with CPU) is I/O bound process.
CPU bound process: The process which spends more time in computations or with CPU and very
rarely with the I/O devices is called as CPU bound process.
Non pre-emptive scheduling: Once the CPU has been allocated to a process the process keeps the CPU until
releases CPU either by terminating or by switching to waiting state Non-pre-emptive algorithms are designed
so that once a process enters the running state, it cannot be pre-empted until it completes its allotted time,
Circumstances for Non pre-emptive:
When process switches from running to waiting state
When process terminates
Example: FCFS (First Come First Serve)
Pre-emptive Scheduling:
Circumstances for pre-emptive:
• process switch from running to ready state
• process switch from waiting to ready state
Q. Define Dispatcher
Dispatcher is a component which involves in the CPU scheduling. The dispatcher is the module that
actually gives control of the CPU to the process selected by the short term scheduler.
The module of the operating system that performs the function of setting up the execution of the
selected process on the CPU is known as dispatcher.
Q. Explain first come first served (FCFS) algorithm. Give one example. State any one advantages and one
disadvantage. – 8m
First-Come - First-Served (FCFS) Scheduling FCFS scheduling is non-preemptive algorithm.
Once the CPU is allocated to a process, it keeps the CPU until it releases the CPU, either by
terminating or by requesting I/O.
In this algorithm, a process, that a request the CPU first, is allocated the CPU first. FCFS scheduling is
implemented with a FIFO queue.
When a process enters the ready queue, its PCB is linked to the tail of the queue.
When the CPU is available, it is allocated to the process at the head of the queue. Once the CPU is
allocated to a process, that process is removed from the queue.
The process releases the CPU by its own.
Example:
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1, P2, P3
Gantt Chart:
Advantage:
It is simple to implement.
Disadvantage:
This scheduling method is non-preemptive, that is, the process will run until it finishes. Because of this
non-preemptive scheduling, short processes which are at the back of the queue have
to wait for the long process at the front to finish.
It is not suitable for real time systems.
Average waiting time and average turnaround time is more comparatively.
Q. Explain Shortest Remaining Time Next (SRTN) scheduling algorithm with example – 4m
SRTN: Shortest Remaining Time Next or Shortest job first (SJF)
A Shortest remaining Time Next scheduling algorithm is also referred as preemptive SJF scheduling algorithm.
When a new process arrives at ready queue while one process is still executing then SRTN algorithm is
performed to decide which process will execute next. This algorithm compare CPU burst time of newly arrived
process with remaining (left) CPU burst time of currently executing process. If CPU burst time of new process
is less than remaining time of current process, then SRTN algorithm preempts current process execution and
starts executing new process.
Example: Consider four processes with arrival time and burst time mentioned below in table.
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Gantt Chart
As P1 is the only process in ready queue at time 0, P1 will start execution first.
At time 1, process P2 arrives. The Burst time of P2 i.e. 4 ms is less than remaining burst time of process
P1 i.e.7 ms, so process P1 is preempted and process P2 starts executing.
At time 2, process P3 arrives. The Burst time of P3 i.e. 9 ms is greater than remaining burst time of
process P2 i.e. 3 ms, so Process P2 continues its execution.
At time 3, process P4 arrives. The Burst time of P4 i.e. 5 ms is greater than remaining burst time of
process P2 i.e. 2 ms, so Process P2 continues its execution.
When P2 process completes its execution, all remaining processes execute with shortest job first
algorithm.
Waiting time of processes:
P1: 9 ms
P2: 0 ms
P3: 15 ms
P4: 2 ms
Average waiting time= (9+0+15+2)/4=26/4=6.5 ms
Q. Explain how priority scheduling algorithm works with suitable explain, also list advantages and
disadvantages – 8m
Q. Explain Priority Scheduling Algorithm with Example
Priority scheduling algorithm: In priority scheduling algorithm, Number (integer) indicating priority is
associated with each process. The CPU is allocated to a process with the highest priority. A priority algorithm
will preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently
running process. A major problem with priority scheduling is indefinite blocking or starvation. A solution to the
problem of indefinite blockage of the low-priority process is aging. Aging is a technique of gradually increasing
the priority of processes that wait in the system for a long period of time.
Example: Consider the following set of processes assumed to have arrived at time 0, in the order P1, P2, P3,
----- P5, with the length of the CPU burst time given in milliseconds. Calculate the average TAT and Waiting
Time
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
Using priority scheduling, we would schedule these processes according to the following Gantt Chart:
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in
milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds. Since it requires
another 20 milliseconds, it is preempted after the first time quantum. And the CPU is given to the next process
in the queue, process P2. process P2 does not need 4 milliseconds, so it quits before its time quantum expires.
The CPU is then given to the next process, process P3. Once each process has received 1 time quantum, the
CPU is returns to process P1 for an additional time quantum.
The resulting RR schedule is as follows:
Advantages of RR scheduling:
Round robin is particularly effective in a general purpose time sharing system or transaction
processing system.
Disadvantages of RR scheduling:
If there is a mix of processor bound and I/O bound processes then processor bound process tend to
receive an unfair portion of processor time, which result in poor performance for I/O bound
processes, inefficient use of I/O devices and an increase in the variance of response time.
Type of System This scheduling This scheduling This scheduling This scheduling
algorithm is suitable algorithm is also algorithm is based algorithm is suitable
for Batch system. suitable for Batch upon priority. for time sharing
system. system.
Q. Define deadlock – 2m
A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource
held by another process in the set
1. To ensure that deadlocks never occur, the system can be use either a deadlock prevention or a
deadlock-avoidance scheme. Deadlock prevention is a set of methods for ensuring that at least one of
the necessary conditions cannot hold. These methods prevent deadlocks by constraining how requests
for resources can be made.
2. Deadlock avoidance, on the other hand, requires that the operating system be given in advance
additional information concerning which resources a process will request and use during its lifetime.
with this additional knowledge, we can decide for each request whether or not the process, and the
future requests and releases of each process, to decide whether the current request can be satisfied
or must be delayed.
3. If the system does not employ either a deadlock-prevention or deadlock – avoidance algorithm, then a
deadlock situation may occur .in this environment, the system can provide an algorithm that examines
the state of the system to determine whether a deadlock has occurred, and an algorithm to recover
from the deadlock.
4. If a system does not ensure that a deadlock will never occur, and also does not provide a mechanism
for deadlock detection and recovery, then we may arrive at a situation where the system is in a
deadlock state yet has no way of recognizing what has happened
Bankers Algorithm: The resource allocation graph is not much useful if there are multiple instances for a
resource. In such a case, we can use Banker‟s algorithm. In this algorithm, every process must tell upfront the
maximum resource of each type it need, subject to the maximum available instances for each type. Allocation
of resources is made only, if the allocation ensures a safe state; else the processes need to wait. The Banker‟s
algorithm can be divided into two parts: Safety algorithm if a system is in a safe state or not. The resource
request algorithm make an assumption of allocation and see if the system will be in a safe state. If the new
state is unsafe, the resources are not allocated and the data structures are restored to their previous state; in
this case the processes must wait for the resource.
Example:
5 processes P0 through P4;
3 Resources types: A (10 Instances), B(5 Instances) and C(7 Instances)
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system in a safe state since the sequence <P1, P3, P4, P2, P0> satisfies safety criteria.
Step 1: When a process requests for a resource, the OS allocates it on a trial basis.
Step 2: After trial allocation, the OS updates all the matrices and vectors. This updating can be done by the OS
in a separate work area in the memory.
Step 3: It compares F vector with each row of matrix B on a vector to vector basis.
Step 4: If F is smaller than each of the row in Matrix B i.e. even if all free resources are allocated to any
process in Matrix B and not a single process can complete its task then OS concludes that the system is in
unstable state.
Step 5: If F is greater than any row for a process in Matrix B the OS allocates all required resources for that
process on a trial basis. It assumes that after completion of process, it will release all the recourses allocated
to it. These resources can be added to the free vector.
Step 6: After execution of a process, it removes the row indicating executed process from both matrices.
Step 7: This algorithm will repeat the procedure step 3 for each process from the matrices and finds that all
processes can complete execution without entering unsafe state. For each request for any resource by a
process OS goes through all these trials of imaginary allocation and updation. After this if the system remains
in the safe state, and then changes can be made in actual matrices.
Solved Problems
1. Calculate Average locating Time for SJF (Shortest Job First) and Round Robin (RR) algorithm for following
table: (Time Slice 4 msec)
Process Burst Time
P1 10
P2 4
P3 9
P4 6
2. Assume we have the workload shown below. All five jobs arrive at time 0 in the order given.
Job Burst Time
1 10
2 29
3 3
4 7
5 12
Considering FCFS, Shortest Job First and Round Robin (quantum=10) scheduling algorithms for this set of jobs
which algorithms would give the minimum average waiting time?
Solution:
For FCFS:
1 2 3 4 5
0 10 39 42 49 61
3) Five jobs A through E arrives at a computer center at all most same time. They have estimated running
time of 10, 6, 2, 4, 8 minutes. Their priorities are 3, 5, 2, 1, 4 resp. for each of the following scheduling
algorithm determine Turn-Around-Time and Average Waiting Time by: Round Robin Scheduling Algorithm,
Shortest Job First Scheduling, FCFS scheduling and Priority method
Job Running Time Priority
A 10 3
B 6 5
C 2 2
D 4 1
E 8 4
Sol:
Using FCFS Scheduling Algorithm
Gantt chart
Solution:
Using FCFS Scheduling algorithm
Gantt chart
5) Consider the following set of processes with arrival time as shown below:
Process Priority Arrival time CPU Burst Time (in ms)
P1 2 0 8
P2 3 1 4
P3 1 2 9
P4 4 3 5
Find out Average Waiting Time by using FCFS, SJF, Priority Scheduling algorithm (May-12)
Solution:
Using FCFS Scheduling Algorithm
Gantt chart
6) System consists of five processes (P1, P2, P3, P4, P5) and three resources (R1, R2, R3). Resource type R1
has 10 Instances, resource type R2 has 5 instances, and R3 has 7 instances. Is the system in a safe state?
Sol:
Calculate Need=Max-allocation
7) The operating system contains 3 resources, the number of instance of each resource type are 7,7,10. The
current resource allocation state is shown below:
Available=(7,7,10)-(5,4,10)=(2,3,0)
Need matrix is
And available resources is (1,2,0). Need of any process is never satisfied after granting the process P1 request
(1,1,0). So the system will be blocked. Therefore request of process P1(1,1,0) cannot be granted.
8) Consider the following allocation chart. Check whether the system is safe or unsafe?
Sol: Need=Max-allocation
System is in safe state with safe sequence <P2, P4, P1, P3>. System will complete its operation in this
sequence.
9) Consider the following allocation table. Calculate the need matrix and check whether the system is safe
or unsafe?
Sol: Need=Max-allocation
System is unsafe state. Resources are not available for processes P1 and P2 to complete their operation.
VTP-SAV Operating System (22516) Chapter-4
Copy protected with Online-PDF-No-Copy.com 20