Process Scheduling
Process Scheduling
Process Scheduling
SCHEDULING
Process scheduling is an essential part of a Multiprogramming operating
systems. Such operating systems allow more than one process to be loaded
into the executable memory at a time and the loaded process shares the CPU
using time multiplexing.
A typical process involves both I/O time and CPU time.
In a uniprogramming system like MS-DOS, time spent waiting for I/O is wasted
and CPU is free during this time.
In multiprogramming systems, one process can use CPU while another is
waiting for I/O. This is possible only with process scheduling.
Process execution begins with a CPU burst. That is followed
by an I/O burst, which is followed by another CPU burst,
then another I/O burst, and so on. Eventually, the final CPU
burst ends with a system request to terminate execution.
Algorithms
First-Come, First-Served Scheduling
The first-come, first-served(FCFS) is the simplest scheduling algorithm.
The process that requests the CPU first is allocated the CPU first. The
implementation of the FCFS policy is easily managed with a FIFO queue.
When a process enters the ready queue, its PCB is linked onto the tail of
the queue. When the CPU is free, it is allocated to the process at the head
of the queue.
The running process is then removed from the queue.
On the negative side, the average waiting time under the FCFS policy is
often quite long.
Consider the
snapshot of a system
given here
E
A Gantt chart is a horizontal bar chart developed as a production control tool in 1917 by Henry L. Gantt, an American
engineer and social scientist.
X
A
P
L
Consider the following three processes
E In the first Gantt chart below, process P1 arrives first. The average waiting time for the three
processes is ( 0 + 24 + 27 ) / 3 = 17.0 ms.
X
M In the second Gantt chart below, the same three processes have an average wait
A
P + 3 + 6 ) / 3 = 3.0 ms. This reduction is
timesubstantial.
of ( 0
L
Thus, the average waiting time under an FCFS policy is generally not minimal and may
vary substantially if the processes’ CPU burst times vary greatly.
E
Process Arrival Time Burst Time
P0 0 10
P1 1 6
P2 3 2
P3 5 4
P0 P1 P2 P3
Consider the set of 5 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is FCFS, calculate the average waiting time and average turn around time.
Now,
Useless time / Wasted time = 6 x δ = 6 x 1 = 6
unit
Total time = 23 unit
Useful time = 23 unit – 6 unit = 17 unit
Efficiency (η)
= Useful time / Total Time
= 17 unit / 23 unit
= 0.7391 ---- 73%
FCFS can also block the system in a busy dynamic system in another way, known as the
convoy effect.
§ When one CPU intensive process blocks the CPU, a number of I/O intensive processes can
get backed up behind it, leaving the I/O devices idle.
§ When the CPU hog finally relinquishes the CPU, then the I/O processes pass through the
CPU quickly, leaving the CPU idle while everyone queues up for I/O, and then the cycle
repeats itself when the CPU intensive process gets back to the ready queue.
E
Gantt Chart representation is:
X
A
P
L The average waiting time is (3 + 16 + 9 + 0) / 4 = 7 milliseconds
The SJF algorithm can be either preemptive or nonpreemptive.
The choice arises when a new process arrives at the ready queue
while a previous process is still executing.
E
Gantt Chart representation is:
X
A • Process P1 is started at time 0, since it is the only process in the queue. Process P2 arrives at
time 1. The remaining time for process P1 (7 milliseconds) is larger than the time required by
process P2 (4 milliseconds), so process P1 is preempted, and process P2 is scheduled.
M • The average waiting time for this example is ((10 - 1) + (1 - 1) + (17 - 2) + (5 - 3))/4 = 26/4 =
6.5 milliseconds.
• A nonpreemptive SJF scheduling would result in an average waiting time of 7.75
P milliseconds.
L
Consider the following five processes
each having its own unique burst time
and arrival time. Compare average
time for non preemptive and
waiting
preemptive SJF
E
X
A
P
L
E
• SJF can be proven to be the fastest scheduling algorithm, but it suffers from one
important problem: How do you know how long the next CPU burst is going to be?
• For long-term batch jobs this can be done based upon the limits that users set for
their jobs when they submit them, which encourages them to set low limits, but
risks their having to re-submit the job if they set the limit too low. However that
does not work for short-term CPU scheduling on an interactive system.
• Another option would be to statistically measure the run time characteristics of
jobs, particularly if the same tasks are run repeatedly and predictably. But once
again that really isn't a viable option for short term CPU scheduling in the real
world.
• A more practical approach is to predict the length of the next burst, based on some
historical measurement of recent burst times for this process. One simple, fast, and
relatively accurate method is the exponential average of the measured lengths of
previous CPU bursts.
Priority Scheduling
A priority is associated with each process, and the CPU is allocated to the process
with the highest priority. Equal-priority processes are scheduled in FCFS order.
An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse
of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority,
and vice versa.
In practice, priorities are implemented using integers within a fixed range, but
there is no agreed-upon convention as to whether "high" priorities use large numbers
or small numbers.
consider the following set of processes, assumed to have
arrived at time 0 in the order P1, P2, · · ·, P5, with the length
of the CPU burst given in milliseconds:
X
A
Now
Try this!!!!
M
P
L The average waiting time is 9.6
milliseconds
• Priorities can be assigned either internally or externally.
Internal priorities are assigned by the OS using criteria such as average burst time, ratio of
CPU to I/O activity, system resource use, and other factors available to the kernel.
External priorities are assigned by users, based on the importance of the job,
fees paid, politics, etc.
• Priority scheduling can be either preemptive or non-preemptive.
When a process arrives at the ready queue, its priority is compared with the priority of the
currently running process.
A preemptive priority scheduling algorithm will preempt the CPU if the priority of the newly
arrived process is higher than the priority of the currently running process.
A nonpreemptive priority scheduling algorithm will simply put the new process at the head
of the ready queue.
Priority scheduling can suffer from a major problem known as indefinite
blocking, or starvation, in which a low-priority task can wait forever
because there are always some other jobs around that have higher priority.
If this problem is allowed to occur, then processes will either run eventually
when the system load lightens, or will eventually get lost when the system is shut
down or crashes. (There are rumors of jobs that have been stuck for years.)
Under this scheme a low-priority job will eventually get its priority raised high
enough that it gets run.
Round-Robin Scheduling
The average waiting time is calculated for this schedule. P1 waits for 6 milliseconds
(10 - 4), P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds. Thus, the
average waiting time is 17/3 = 5.66 milliseconds.
• In the RR scheduling algorithm, no process is allocated the CPU for more than 1 time
quantum in a row (unless it is the only runnable process).
• If a process’s CPU burst exceeds 1 time quantum, that process is preempted and is put
back in the ready queue. The RR scheduling algorithm is thus preemptive.
• The performance of RR is sensitive to the time quantum selected. If the quantum is
large enough, then RR reduces to the FCFS algorithm; If it is very small, then each
process gets 1/nth of the processor time and share the CPU equally.
• BUT, a real system invokes overhead for every context switch, and the smaller the
time quantum the more context switches there are.
• Turnaround time also depends on the size of the time quantum. In general, turnaround
time is minimized if most processes finish their next cpu burst within one time
quantum.
• The way in which a smaller time
quantum increases context
switches.
• A rule of thumb is that 80 percent
of the CPU bursts should be
shorter than the time quantum.
P1= 16-4
P2= 4-1
P3= {(8-2)+(20-12)+(25-24)}
P4= {(12-3)+(24-16)}
Average Waiting Time: 12+3+15+17 = 47/4 = 11.75
Average Turn Around Time: 20+7+24+22 = 73/4 = 18.25
Get the folowing things
ready!!!!!!
Four jobs to be executed on a single processor system arrive at time 0 in the order
A, B, C, D . Their burst CPU time requirements are 4, 1, 8, 1 time units respectively.
Calculate the completion time of A under round robin scheduling with time slice of
one time unit is Explanation:
1. All processes are arrived at time 0.
2. Algorithm used for scheduling is round robin with time quantum of one unit time.
3. The order of execution of the processes A B C D A C A C A,C,C,C,C,C
4. After 8 context switches, process A completes it execution So the completion time is
9
Consider three CPU-intensive processes, which require 10, 20 and 30 time units and arrive at
times 0, 2 and 6, respectively. How many context switches are needed if the operating system
implements a shortest remaining time first scheduling algorithm? Do not count the context
switches at time zero and at the end.
Which of the following
statements are true?
T/F
At, 4ms p1 and p2 are finished, and waiting time of p1 and p2 is 1 and 0. In order to have avg wait time as 1 sum of wait time
of p3 and p4 should be 3. If p4 starts at 4 with BT as "2" then its wait time is 0, and at 6 ms it will be over and p3 will start at
6. So wait time of p3 is 6-3=3 and hence it satisfies the condition that sum of wait times of p3 and p4 should be 3. Hence,
Z=2
Consider a uniprocessor system executing three tasks T1, T2 and T3, each of which is composed of an infinite sequence
of jobs (or instances) which arrive periodically at intervals of 3, 7 and 20 milliseconds, respectively. The priority of each
task is the inverse of its period and the available tasks are scheduled in order of priority, with the highest priority task
scheduled first. Each instance of T1, T2 and T3 requires an execution time of 1, 2 and 4 milliseconds, respectively.
Given that all tasks initially arrive at the beginning of the 1st milliseconds and task preemptions are allowed, the first
instance of T3 completes its execution at the end of milliseconds.
Consider three processes, all arriving at time zero, with total execution time of 10,
20 and 30 units, respectively. Each process spends the first 20% of execution time
doing I/O, the next 70% of time doing computation, and the last 10% of time doing
I/O again. The operating system uses a shortest remaining compute time first
scheduling algorithm and schedules a new process either when the running process
gets blocked on I/O or when the running process finishes its compute burst. Assume
that all I/O operations can be overlapped as much as possible. For what percentage
of time does the CPU remain idle?
When processes can be readily categorized, then multiple separate queues can be established, each
implementing whatever scheduling algorithm is most appropriate for that type of job, and/or with
different parametric adjustments.
A common division is made between foreground(or interactive) processes and background (or
batch) processes. These two types of processes have different response-time requirements, and so
might have different scheduling needs. In addition, foreground processes may have priority over
background processes.
In addition, there must be scheduling among the queues, which is commonly implemented as
fixed-priority preemptive scheduling. For example, the foreground queue may have absolute
priority over the background queue.
Under this algorithm jobs cannot switch from queue to queue - Once they are assigned a queue,
that is their queue until they finish.
A multi-level queue scheduling algorithm partitions the ready queue into
several separate queues.
The processes are permanently assigned to one queue, generally based on
some property of the process, such as memory size, process priority, or process
type.
Each queue has its own scheduling algorithm.
Multilevel Feedback-Queue Scheduling
Multilevel feedback queue scheduling is the most flexible, because it can be tuned for any
situation. But it is also the most complex to implement because of all the adjustable parameters.
Some of the parameters which define one of these systems include:
The number of queues.
The scheduling algorithm for each queue.
The methods used to upgrade or demote processes from one queue to another. ( Which may be different. )
Practice Problem
ANS –
4,5
Multiple - Processor
Scheduling
When multiple processors are available, then the scheduling gets more
complicated, because now there is more than one CPU which must be kept busy
and in effective use at all times.
Load sharing revolves around balancing the load between multiple processors.
On the other hand, if P1 is given higher priority, it gets to go first, and P2 starts after P1
completes its burst. At time 50 when the next period for P1 starts, P2 has only completed 30
of its 35 needed time units, but it gets pre-empted by P1. At time 70, P1 completes its task for
its second period, and then P2 is allowed to complete its last 5 time units. Overall both
processes complete at time 75, and the cpu is then idle for 25 time units, before the process
repeats.
Rate-monotonic scheduling is considered optimal among algorithms that use static priorities,
because any set of processes that cannot be scheduled with this algorithm cannot be scheduled
with any other static-priority scheduling algorithm either. There are, however, some sets of
processes that cannot be scheduled with static priorities.
For example, supposing that P1 =50, T1 = 25, P2 = 80, T2 = 35, and the deadlines match the
periods. Overall CPU usage is 25/50 = 0.5 for P1, 35/80 =0.44 for P2, or 0.94 (94%) overall,
indicating it should be possible to schedule the processes. With rate-monotonic scheduling, P1
goes first, and completes its first burst at time 25.
P2 goes next, and completes 25 out of its 35 time units before it gets pre-empted by P1 at time
50. P1 completes its second burst at 75, and then P2 completes its last 10 time units at time 85,
missing its deadline of 80 by 5 time units.
The worst-case CPU utilization for scheduling N processes under
this
algorithm is which is 100% for a single process, but drops
to 75% for two processes and to 69% as N approaches infinity. Note that in our
example above 94% is higher than 75%. For two processes, CPU Utilization is
bounded at about 83%
Cases of fixed-priority scheduling with two tasks, T1=50, C1=25, T2=100, C2=40
Earliest-Deadline-First Scheduling
EDF scheduling dynamically assigns priorities according to deadline. The earlier the
deadline, the higher the priority; the later the deadline, the lower the priority.
Under the EDF policy, when a process becomes runnable, it must announce its
deadline requirements to the system.
EDF has been proven to be an optimal uniprocessor scheduling algorithm. This
means that, if a set of tasks is not schedulable under EDF, then no other scheduling
algorithm can feasibly schedule this task set.
P1 then starts its second burst, which it completes at time 85. P2 started its second period at time 80, but
since P1 had an earlier deadline, P2 did not pre-empt P1.
P2 starts its second burst at time 85, and continues until time 100, at which time P1 starts its third
period. At this point P1 has a deadline of 150 and P2 has a deadline of 160, so P1 preempts P2.
P1 completes its third burst at time 125, at which time P2 starts, completing its third burst at time 145.
The CPU sits idle for 5 time units, until P1 starts its next period at 150 and P2 at 160.
Unlike the rate-monotonic algorithm, EDF scheduling does not require that processes
be periodic, nor must a process require a constant amount of CPU time per burst.
The only requirement is that a process announce its deadline to the scheduler when it
becomes runnable.
The appeal of EDF scheduling is that it is theoretically optimal—theoretically, it can
schedule processes so that each process can meet its deadline requirements and CPU
utilization will be 100 percent.
In practice, however, it is impossible to achieve this level of CPU utilization due to
the cost of context switching between processes and interrupt handling.
Task set: U= 1/4 +2/6 +3/8 = 0.25 + 0.333 +0.375 = 0.95 = 95%
As processor utilization is less than 1 or 100% so task set is surely schedulable by EDF.
In the example below, when time is 0, both A1 and B1 arrive. Since A1 has the earliest
deadline, it is scheduled first. When A1 completes, B1 is given the processor. when time
is 20, A2 arrives. Because A2 has an earlier deadline than B1, B1 is interrupted so that
A2 can execute to completion. Then B1 is resumed when time is 30. when time is 40, A3
arrives. However, B1 has an earlier ending deadline and is allowed to execute to
completion when time is 45. A3 is then given the processor and finishes when time is 55
Practice Question on Rate Monotonic Scheduling
Q. Consider two processes, P1 and P2, where p1 = 40, t1 =
20, p2 = 70, t2 = 30. Deadline for each process is equal to
the period of the process. Illustrate the scheduling of these
two processes using rate monotonic scheduling.