Professional Documents
Culture Documents
Chapter 4 Notes
Chapter 4 Notes
Chapter 4 Notes
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -1-
Operating System (22516)
CPU Scheduler:
• Whenever the CPU becomes idle, the operating system must select one of
the processes in the ready queue to be executed. The selection process is
carried out by the short-term scheduler (or CPU scheduler).
• The scheduler selects a process from the processes in memory that are ready
to execute and allocates the CPU to that process.
• Note that the ready queue is not necessarily a first-in, first-out (FIFO) queue.,
a ready queue can be implemented as a FIFO queue, a priority queue, or
simply an unordered linked list. however, all the processes in the ready
queue are lined up waiting for a chance to run on the CPU.
• The records in the queues are generally process control blocks (PCBs) of the
processes.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -2-
Operating System (22516)
Preemptive Scheduling
CPU-scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state (for
example, as the result of an I/O request or an invocation of wait for the
termination of one of the child processes)
2. When a process switches from the running state to the ready state (for
example, when an interrupt occurs)
3. When a process switches from the waiting state to the ready state (for
example, at completion of I/O)
4. When a process terminates
For situations 1 and 4, there is no choice in terms of scheduling. A new process
(if one exists in the ready queue) must be selected for execution. There is a
choice, however, for situations 2 and 3.
When scheduling takes place only under circumstances 1 and 4, we say that the
scheduling scheme is non preemptive or cooperative; otherwise, it is preemptive.
Under non preemptive scheduling, once the CPU has been allocated to a process,
the process keeps the CPU until it releases the CPU either by terminating or by
switching to the waiting state. This scheduling method was used by Microsoft
Windows 3.x; Windows 95 introduced preemptive scheduling, and all subsequent
versions of Windows operating systems have used preemptive scheduling.
The Mac OS X operating system for the Macintosh also uses preemptive scheduling;
previous versions of the Macintosh operating system relied on cooperative
scheduling.
Cooperative scheduling is the only method that can be used on certain hardware
platforms, because it does not require the special hardware (for example, a timer)
needed for preemptive scheduling.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -3-
Operating System (22516)
Dispatcher
Another component involved in the CPU-scheduling function is the dispatcher. The
dispatcher is the module that gives control of the CPU to the process selected by
the short-term scheduler. This function involves the following:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that
program. The dispatcher should be as fast as possible, since it is invoked
during every process switch. The time it takes for the dispatcher to stop
one process and start another running is known as the dispatch latency.
Scheduling Criteria
Many criteria have been suggested for comparing CPU-scheduling algorithms.
Which characteristics are used for comparison can make a substantial difference in
which algorithm is judged to be best. The criteria include the following:
CPU utilization: We want to keep the CPU as busy as possible. Conceptually, CPU
utilization can range from 0 to 100 percent. In a real system, it should range from
40 percent (for a lightly loaded system) to 90 percent (for a heavily used system).
Throughput: If the CPU is busy executing processes, then work is being done. One
measure of work is the number of processes that are completed per time unit,
called throughput. For long processes, this rate may be one process per hour; for
short transactions, it may be ten processes per second.
Turnaround time: From the point of view of a particular process, the important
criterion is how long it takes to execute that process. The interval from the time of
submission of a process to the time of completion is the turnaround time.
Turnaround time is the sum of the periods spent waiting to get into memory,
waiting in the ready queue, executing on the CPU, and doing I/O.
Waiting time: The CPU-scheduling algorithm does not affect the amount of time
during which a process executes or does I/O; it affects only the amount of time that
a process spends waiting in the ready queue. Waiting time is the sum of the periods
spent waiting in the ready queue.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -4-
Operating System (22516)
Response time: In an interactive system, turnaround time may not be the best
criterion. Often, a process can produce some output fairly early and can continue
computing new results while previous results are being output to the user. Thus,
another measure is the time from the submission of a request until the first
response is produced. This measure, called response time, is the time it takes to
start responding, not the time it takes to output the response. The turnaround time
is generally limited by the speed of the output device. It is desirable to maximize
CPU utilization and throughput and to minimize turnaround time, waiting time, and
response time.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -5-
Operating System (22516)
Let us consider an example where 4 processes with different burst times arrive at
different times.
Process Burst Time Arrival time
P1 6 2
P2 8 1
P3 3 0
P4 4 4
• At arrival time 0
Gantt chart:
P3
0
The process P2 arrives at time 1, but the process P3 is still executing. The
process P2 will be kept in a queue which will be executed after the existing
process finishes its execution. Also at time 2, Process P1 arrives and waits in the
queue as there is another process being executed.
Gantt chart:
P3 P2 P1
0 3 11
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -6-
Operating System (22516)
Advantages of FCFS:
• Simple
• Easy
• First come, First served
Dis-advantages of FCFS:
• The scheduling method is non preemptive, the process will run to the
completion.
• Due to the non-preemptive nature of the algorithm, the problem of
starvation may occur.
• Although it is easy to implement, but it is poor in performance since the
average waiting time is higher as compare to other scheduling algorithms.
SJF scheduling algorithm, schedules the processes according to their burst time. In
SJF scheduling, the process with the lowest burst time, among the list of available
processes in the ready queue, is going to be scheduled next. This scheduling
method can be preemptive or non-preemptive. It significantly reduces the average
waiting time for other processes awaiting execution.
However, it is very difficult to predict the burst time needed for a process hence
this algorithm is very difficult to implement in the system.
Non-Preemptive SJF:
In non-preemptive scheduling, once the CPU cycle is allocated to process, the
process holds it till it reaches a waiting state or terminated.
Consider the following five processes each having its own unique burst time and
arrival time.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -7-
Operating System (22516)
Gantt chart
P4
0
At time 4, process P5 arrives and is added to the waiting queue. P1 will continue
execution.
At time 5, process P2 arrives and is added to the waiting queue. P1 will continue
execution.
At time 9, process P1 will finish its execution. The burst time of P3, P5, and P2 is
compared. Process P2 is executed because its burst time is the lowest.
P4 P1 P2
0 3 9
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -8-
Operating System (22516)
At arrival time 10, P2 is executing and P3 and P5 are in the waiting queue.
At arrival time 11, process P2 will finish its execution. The burst time of P3 and P5
is compared. Process P5 is executed because its burst time is lower.
P4 P1 P2
0 3 9 11
Preemptive SJF
In Preemptive SJF Scheduling, jobs are put into the ready queue as they come. A
process with shortest burst time begins execution. If a process with even a shorter
burst time arrives, the current process is removed or preempted from execution,
and the shorter job is allocated CPU cycle.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -9-
Operating System (22516)
At time 1, Process P3 arrives. But, P4 has a shorter burst time. It will continue
execution.
At time 2, process P1 arrives with burst time 6. The burst time is more than that
of P4. Hence, P4 will continue execution.
At time 3, process P4 will finish its execution. The burst time of P3 and P1 is
compared. Process P1 is executed because its burst time is lower.
P4 P1
0 3 4
At time 4, process P5 will arrive. The burst time of P3, P5, and P1 is compared.
Process P5 is executed because its burst time is lowest. Process P1 is preempted.
P4 P1 P5
0 3 4 5
At time 5, process P2 will arrive. The burst time of P1, P2, P3, and P5 is compared.
Process P2 is executed because its burst time is least. Process P5 is preempted.
P4 P1 P5 P2
0 3 4 5
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -10-
Operating System (22516)
At time 10, P5 will finish its execution. The burst time of P1 and P3 is compared.
Process P1 is executed because its burst time is less.
P4 P1 P5 P2 P5 P1
0 3 4 5 7 10
At time 15, P1 finishes its execution. P3 is the only process left. It will start
execution.
P4 P1 P5 P2 P5 P1 P3
0 3 4 5 7 10 15
Advantages of SJF
• SJF is frequently used for long term scheduling.
• It reduces the average waiting time over FIFO (First in First Out) algorithm.
• SJF method gives the lowest average waiting time for a specific set of
processes.
• It is appropriate for the jobs running in batch, where run times are known
in advance.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -11-
Operating System (22516)
• For the batch system of long-term scheduling, a burst time estimate can be
obtained from the job description.
• For Short-Term Scheduling, we need to predict the value of the next burst
time.
• Probably optimal with regard to average turnaround time.
Dis-advantages of SJF:
• Job completion time must be known earlier, but it is hard to predict.
• It is often used in a batch system for long term scheduling.
• SJF can’t be implemented for CPU scheduling for the short term. It is
because there is no specific method to predict the length of the upcoming
CPU burst.
• This algorithm may cause very long turnaround times or starvation.
• Requires knowledge of how long a process or job will run.
• It leads to the starvation that does not reduce average turnaround time.
• It is hard to know the length of the upcoming CPU request.
• Elapsed time should be recorded, that results in more overhead on the
processor.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -12-
Operating System (22516)
Time Quantum : 3
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -13-
Operating System (22516)
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -14-
Operating System (22516)
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -15-
Operating System (22516)
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -16-
Operating System (22516)
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -17-
Operating System (22516)
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -18-
Operating System (22516)
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -19-
Operating System (22516)
http://vlabs.iitb.ac.in/vlabs-
dev/vlab_bootcamp/bootcamp/CRUX/labs/exp1/simulation.html
4. Priority Scheduling:
In Priority scheduling, there is a priority number assigned to each process. In
some systems, the lower the number, the higher the priority. While, in the
others, the higher the number, the higher will be the priority. The Process with
the higher priority among the available processes is given the CPU. There are
two types of priority scheduling algorithm exists. One is Preemptive priority
scheduling while the other is Non-Preemptive Priority scheduling.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -20-
Operating System (22516)
The priority number assigned to each of the process may or may not vary. If the
priority number doesn't change itself throughout the process, it is called static
priority, while if it keeps changing itself at the regular intervals, it is called dynamic
priority.
Example
In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities,
Arrival Time and burst time are given in the table.
1 2 0 3
2 6 2 5
3 3 1 4
4 5 4 2
5 7 6 9
6 4 5 4
7 10 7 10
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -21-
Operating System (22516)
The Process P1 arrives at time 0 with the burst time of 3 units and the priority
number 2. Since No other process has arrived till now hence the OS will schedule it
immediately.
P1
0 3
Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since
the priority of P3 is 3 hence the CPU will execute P3 over P2.
P1 P3
0 3 7
Meanwhile the execution of P3, All the processes get available in the ready queue.
The Process with the lowest priority number will be given the priority. Since P6 has
priority number assigned as 4 hence it will be executed just after P3.
P1 P3 P6
0 3 7 11
After P6, P4 has the least priority number among the available processes; it will get
executed for the whole burst time.
P1 P3 P6 P4
0 3 7 11 13
Since all the jobs are available in the ready queue hence All the Jobs will get
executed according to their priorities. If two jobs have similar priority number
assigned to them, the one with the least arrival time will be executed.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -22-
Operating System (22516)
1 2 0 3 3 3 0 0
2 6 2 5 18 16 11 13
3 3 1 4 7 6 2 3
4 5 4 2 13 9 7 11
5 7 6 9 27 21 12 18
6 4 5 4 11 6 2 7
7 10 7 10 37 30 18 27
Once all the jobs get available in the ready queue, the algorithm will behave as non-
preemptive priority scheduling, which means the job scheduled will run till the
completion and no preemption will be done.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -23-
Operating System (22516)
Example
There are 7 processes P1, P2, P3, P4, P5, P6 and P7 given. Their respective priorities,
Arrival Times and Burst times are given in the table below.
1 2(L) 0 1
2 6 1 7
3 3 2 3
4 5 3 6
5 4 4 5
6 10(H) 5 15
7 9 15 8
GANTT chart Preparation
At time 0, P1 arrives with the burst time of 1 units and priority 2. Since no other
process is available hence this will be scheduled till next job arrives or its
completion (whichever is lesser).
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -24-
Operating System (22516)
The Next process P3 arrives at time unit 2, the priority of P3 is higher to P2. Hence
the execution of P2 will be stopped and P3 will be scheduled on the CPU.
During the execution of P3, three more processes P4, P5 and P6 becomes available.
Since, all these three have the priority lower to the process in execution so PS can't
preempt the process. P3 will complete its execution and then P5 will be scheduled
with the priority highest among the available processes.
Meanwhile the execution of P5, all the processes got available in the ready queue.
At this point, the algorithm will start behaving as Non-Preemptive Priority
Scheduling. Hence now, once all the processes get available in the ready queue, the
OS just took the process with the highest priority and execute that process till
completion. In this case, P4 will be scheduled and will be executed till the
completion.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -25-
Operating System (22516)
Since P4 is completed, the other process with the highest priority available in the
ready queue is P2. Hence P2 will be scheduled next.
P2 is given the CPU till the completion. Since its remaining burst time is 6 units
hence P7 will be scheduled after this.
The only remaining process is P6 with the least priority, the Operating System has
no choice unless of executing it. This will be executed at the last.
The Completion Time of each process is determined with the help of GANTT chart.
The turnaround time and the waiting time can be calculated by the following
formula.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -26-
Operating System (22516)
1 2 0 1 1 1 0
2 6 1 7 22 21 14
3 3 2 3 5 3 0
4 5 3 6 16 13 7
5 4 4 5 10 6 1
6 10 5 15 45 40 25
7 9 6 8 30 24 16
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -27-
Operating System (22516)
two sorts of processes that require different scheduling algorithms because they
have varying response times and resource requirements. The foreground
(interactive) and background processes (batch process) are distinguished.
Background processes take priority over foreground processes.
The ready queue has been partitioned into seven different queues using the
multilevel queue scheduling technique. These processes are assigned to one
queue based on their priority, such as memory size, process priority, or type.
The method for scheduling each queue is different. Some queues are utilized for
the foreground process, while others are used for the background process. The
foreground queue may be scheduled using a round-robin method, and the
background queue can be scheduled using an FCFS strategy.
Let's take an example of a multilevel queue-scheduling algorithm with five queues
to understand how this scheduling works:
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Process
Every queue would have an absolute priority over the low-priority queues. No
process may execute until the high-priority queues are empty. In the above
instance, no other process may execute until and unless the queues for system,
interactive, and editing processes are empty. If an interactive editing process enters
the ready queue while a batch process is underway, the batch process will be
preempted.
There are the descriptions of the processes that are used in the above example:
System Process
The OS has its process to execute, which is referred to as the System Process.
Interactive Process
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -28-
Operating System (22516)
1. You can use multilevel queue scheduling to apply different scheduling methods
to distinct processes.
2. It will have low overhead in terms of scheduling.
Disadvantages
1. There is a risk of starvation for lower priority processes.
2. It is rigid in nature.
4.3 Deadlock
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
• The process requests for some resource.
• OS grant the resource if it is available otherwise let the process waits.
• The process uses it and release on the completion.
A Deadlock is a situation where each of the computer process waits for a resource
which is being assigned to some another process. In this situation, none of the
process gets executed since the resource it needs, is held by some other process
which is also waiting for some other resource to be released.
Let us assume that there are three processes P1, P2 and P3. There are three
different resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3
is assigned to P3.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -29-
Operating System (22516)
After some time, P1 demands for R1 which is being used by P2. P1 halts its
execution since it can't complete without R2. P2 also demands for R3 which is being
used by P3. P2 also stops its execution because it can't continue without R3. P3 also
demands for R1 which is being used by P1 therefore P3 also stops its execution.
In this scenario, a cycle is being formed among the three processes. None of the
process is progressing and they are all waiting. The computer becomes
unresponsive since all the processes got blocked.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -30-
Operating System (22516)
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -31-
Operating System (22516)
In these types of systems, the user has to simply restart the computer in the case
of deadlock. Windows and Linux are mainly using this approach.
2. Deadlock Prevention:
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and
circular wait holds simultaneously. If it is possible to violate one of the four
conditions at any time then the deadlock can never occur in the system.
The idea behind the approach is very simple that we have to fail one of the four
conditions but there can be a big argument on its physical implementation in the
system.
3. Deadlock Avoidance:
In deadlock avoidance, the operating system checks whether the system is in safe
state or in unsafe state at every step which the operating system performs. The
process continues until the system is in safe state. Once the system moves to
unsafe state, the OS has to backtrack one step.
In simple words, The OS reviews each allocation so that the allocation doesn't cause
the deadlock in the system.
Deadlock Prevention:
In deadlock, if we can be able to violate one of the four necessary conditions and
don't let them occur together then we can prevent the deadlock.
Let's see how we can prevent each of the conditions.
1. Mutual Exclusion
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -32-
Operating System (22516)
Mutual section from the resource point of view is the fact that a resource can never
be used by more than one process simultaneously which is fair enough but that is
the main reason behind the deadlock. If a resource could have been used by more
than one process at the same time then the process would have never been waiting
for any resource.
However, if we can be able to violate resources behaving in the mutually exclusive
manner then the deadlock can be prevented.
2. Possibility of getting starved will be increases due to the fact that some process
may hold a resource for a very long time.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -33-
Operating System (22516)
3. No Preemption
Deadlock arises due to the fact that a process can't be stopped once it starts.
However, if we take the resource away from the process which is causing deadlock
then we can prevent deadlock.
This is not a good approach at all since if we take a resource away which is being
used by the process then all the work which it has done till now can become
inconsistent.
Consider a printer is being used by any process. If we take the printer away from
that process and assign it to some other process then all the data which has been
printed can become inconsistent and ineffective and also the fact that the process
can't start printing again from where it has left which causes performance
inefficiency.
4. Circular Wait
To violate circular wait, we can assign a priority number to each of the resource. A
process can't request for a lesser priority resource. This ensures that not a single
process can request a resource which is being utilized by some other process and
no cycle will be formed.
Among all methods, Circular wait is the only approach that can be implemented
practically.
Notes Prepared by - Prof Dhanashri Agrawal || SSVPS BSD Polytechnic Dhule -34-