scheduling algorithms. a computer system has limited resources that must be shared among the...
TRANSCRIPT
Scheduling AlgorithmsScheduling Algorithms
A computer system has limited resources that must A computer system has limited resources that must be shared among the processes competing for those be shared among the processes competing for those resources.resources.
The operating system must insure that resources The operating system must insure that resources are assigned to processes in fair and reasonable are assigned to processes in fair and reasonable way.way.
This is the problem of scheduling.This is the problem of scheduling.
Scheduling occurs at many levels, and are handled Scheduling occurs at many levels, and are handled by various OS components called schedulers. by various OS components called schedulers.
Most scheduling is performed on the behalf of Most scheduling is performed on the behalf of processes.processes.
As processes move from state to state they are As processes move from state to state they are under the influence of scheduling.under the influence of scheduling.
Process scheduling involves deciding which process to Process scheduling involves deciding which process to select, when multiple processes are waiting for service.select, when multiple processes are waiting for service.
In general, Scheduling can occur at 3 different levels:In general, Scheduling can occur at 3 different levels:
Long-term Long-term (job scheduling): determining which jobs to admit (job scheduling): determining which jobs to admit into the system as processesinto the system as processes
Short-termShort-term (CPU scheduling): determining which process will (CPU scheduling): determining which process will be given control of the CPU next. Performed by two modules: be given control of the CPU next. Performed by two modules:
the scheduler and the dispatcher.the scheduler and the dispatcher.
Medium termMedium term (storage scheduling): determining which of (storage scheduling): determining which of the processes will be active, occupying memory, and which the processes will be active, occupying memory, and which
processes will become inactive and have their memory processes will become inactive and have their memory swapped to disk.swapped to disk.
Begin execution
termination
CPU bound termination
I/O bound termination
It’s important to maintain a good process mix.
Long Term Scheduler (job scheduler)
Wait in the “job pool” for initial entry into the system
Long Term Scheduler (job scheduler) moves process into the active set
Active(Running)
SHORT
TERM
SCHEDULING
Inactive (suspende
d)
MEDIUM
TERM
SCHEDULING
Process terminates and exits the system
Active
(Ready, Blocked)
INITIAL
READY
RUNNING SUSPENDED READY
SUSPENDED BLOCKED
BLOCKED
TERMINAL
Terminatewhen complete
Time quantum expires
New process is ready to run
CPU assigned
Waiting for event or resource
Waiting for long term event
Event occurs or resource available
Event occurs or resource available
Process swapped back into memory
Abort due to error
Time Limits – that affect schedulingTime Limits – that affect scheduling
Processor time quantum (time slice)Processor time quantum (time slice)
Storage time quantumStorage time quantum
Job time limitJob time limit
BATCH INTERACTIVE REAL-TIME
LONG-TERM Job admission based on characteristics and resource needs.
Sessions and processes normally accepted unless capacity reached.
Processes either permanent or accepted at once
MEDIUM-TERM Usually none. Jobs remain in storage until done.
Processes swapped on rotating basis when necessary, using storage time quantum.
Processes never swapped or suspended.
SHORT-TERM Processes scheduled by priority. They usually continue until they wait voluntarily, request service or are terminated.
Processes scheduled on a rotating basis. They continue until service is requested or processor time quantum expires. Optional preemption
Scheduling based on strict priority with immediate preemption. Optional time sharing among equal priority processes.
Scheduling ObjectivesScheduling Objectives Strategies used for scheduling decisions vary widely at each level.Strategies used for scheduling decisions vary widely at each level.
Throughput:Throughput: If the CPU and resources are busy being used then work is If the CPU and resources are busy being used then work is being accomplished. The goal is to perform as much work as possible being accomplished. The goal is to perform as much work as possible in a given time period. This is a measure of the number of processed in a given time period. This is a measure of the number of processed completed per time unit.completed per time unit.
Turnaround:Turnaround: Goal is to complete jobs as soon as possible. This is the Goal is to complete jobs as soon as possible. This is the interval from the time of submission to the time of completion. It is the interval from the time of submission to the time of completion. It is the sum of all of the time periods spent waiting (in the ready queue, in sum of all of the time periods spent waiting (in the ready queue, in resource queues, waiting to enter memory) and time executing.resource queues, waiting to enter memory) and time executing.
Response:Response: Goal (particularly for interactive processes) is to process Goal (particularly for interactive processes) is to process individual requests as quickly as possible.individual requests as quickly as possible.
FairnessFairness:: Goal is to treat each process (or job) in a way that Goal is to treat each process (or job) in a way that corresponds “fairly” with its characteristics. Essentially process should corresponds “fairly” with its characteristics. Essentially process should be given an opportunity to execute.be given an opportunity to execute.
ConsistencyConsistency:: Goal is to treat processes with the same characteristics Goal is to treat processes with the same characteristics predictably.predictably.
Resource UseResource Use:: Goal is to keep each category of resource used as fully Goal is to keep each category of resource used as fully as possible.as possible.
Measuring scheduling Measuring scheduling objectivesobjectives
We can evaluate how successfully a scheduling algorithm meets We can evaluate how successfully a scheduling algorithm meets its goals by observing the queues waiting for service or resources. its goals by observing the queues waiting for service or resources. Some of the specific properties we consider are:Some of the specific properties we consider are:
Waiting time: Waiting time: A CPU scheduling algorithm only affects how long a A CPU scheduling algorithm only affects how long a process spends waiting in the ready queue. (not how long it will process spends waiting in the ready queue. (not how long it will execute, or how long it must wait for resources). execute, or how long it must wait for resources).
Queue Length:Queue Length: this measures the size of the queues of waiting this measures the size of the queues of waiting processes. We want to minimize queue length to conserve storage.processes. We want to minimize queue length to conserve storage.
Response Ration: Response Ration: This is a measure of waiting time balanced against This is a measure of waiting time balanced against the service time required for each request: R=W/(W+S) Where “W” is the service time required for each request: R=W/(W+S) Where “W” is the waiting time, and “S” is the service time. A consistent response the waiting time, and “S” is the service time. A consistent response ration means that better service is being given to shorter processes, ration means that better service is being given to shorter processes, which is usually considered fairwhich is usually considered fair..
PrioritiesPriorities Priority is used to represent the relative Priority is used to represent the relative
importance of a process in the system.importance of a process in the system. Priorities aid the schedulers in making quick Priorities aid the schedulers in making quick
decisionsdecisions Dynamic vs. Static prioritiesDynamic vs. Static priorities
PreemptionPreemption In a non-preemptive scheduling strategy, a In a non-preemptive scheduling strategy, a
process is assigned the processor until it process is assigned the processor until it voluntarily gives it up.voluntarily gives it up.
In a preemptive strategy, a high-priority may be In a preemptive strategy, a high-priority may be given the processor immediately even though a given the processor immediately even though a lower or equal priority process is currently lower or equal priority process is currently running.running.
OverheadOverhead This is a performance penalty associated with This is a performance penalty associated with
context switching.context switching.
CPU Scheduling of Batch CPU Scheduling of Batch ProcessesProcesses
Submitted with a good estimate of their Submitted with a good estimate of their resource needs, via job control statementsresource needs, via job control statements
Upper limits the OS is expected to enforceUpper limits the OS is expected to enforce Generally expected that the OS will give Generally expected that the OS will give
preferential treatment to processes with preferential treatment to processes with lower resource needs.lower resource needs.
Resource estimates may also include the Resource estimates may also include the specification of an initial priority for a jobspecification of an initial priority for a job
For batch processesFor batch processes
scheduling occurs at 2 scheduling occurs at 2 levels:levels:
Long-term and short termLong-term and short term
Job Scheduling (long term)Job Scheduling (long term)
Job scheduling involves deciding which job Job scheduling involves deciding which job (job step) to admit into the system as a (job step) to admit into the system as a process. A job is a program which has process. A job is a program which has been submitted to the system, but which been submitted to the system, but which has not been loaded into memory.has not been loaded into memory.
Initial resource estimates play an Initial resource estimates play an important role of scheduling at the job important role of scheduling at the job level. A job can not be loaded until level. A job can not be loaded until sufficient resources are availablesufficient resources are available
A “Job”A “Job”
1) typically consists of multiple steps1) typically consists of multiple steps
2) Job control statements provide information about the 2) Job control statements provide information about the resource needs of each step. Including program to execute, resource needs of each step. Including program to execute, maximum memory requirements, and cpu time limits, and maximum memory requirements, and cpu time limits, and
user informationuser information
3) Waiting jobs are stored in one or more job queues, ordered 3) Waiting jobs are stored in one or more job queues, ordered by priority or class, which is determined by their announced by priority or class, which is determined by their announced
resource needs.resource needs.
4) System processes called initiators, examine the job queues 4) System processes called initiators, examine the job queues and selects jobs when the system is ready for more work. and selects jobs when the system is ready for more work.
5) Jobs are admitted only when sufficient resources are 5) Jobs are admitted only when sufficient resources are
available to begin the first “step”. Also each additional step is available to begin the first “step”. Also each additional step is admitted only when resources are available.admitted only when resources are available.
6) Once a step has been admitted, it holds all of its resources 6) Once a step has been admitted, it holds all of its resources until it is complete. A timer is set to ensure that it does not until it is complete. A timer is set to ensure that it does not
exceed its planned processor time.exceed its planned processor time.
Short term schedulingShort term scheduling When the processor (CPU) is When the processor (CPU) is
available, a process from the ready available, a process from the ready queue(s) is selected according to queue(s) is selected according to some type of algorithm.some type of algorithm.
The process is then given control of The process is then given control of the CPU and continues running until:the CPU and continues running until:
The process terminatesThe process terminates The process voluntarily suspends The process voluntarily suspends
itself (sleep)itself (sleep) The process requests an I/O transfer The process requests an I/O transfer
or other service for which it must waitor other service for which it must wait The process is stopped because it has The process is stopped because it has
exceeded its processor time quantum exceeded its processor time quantum (maximum CPU time for the entire (maximum CPU time for the entire job, or step!)job, or step!)
IF preemption is allowed: a higher IF preemption is allowed: a higher priority process becomes ready. priority process becomes ready. (preemption is usually not used in a (preemption is usually not used in a batch environment.)batch environment.)
Batch CPU Scheduling Batch CPU Scheduling AlgorithmsAlgorithms
examples of scheduling algorithms used later in this presentation were taken from examples of scheduling algorithms used later in this presentation were taken from
“Operating System Concepts”, by Silberschatz, Galvin, Gagne“Operating System Concepts”, by Silberschatz, Galvin, Gagne
Please note: On the following slides, Please note: On the following slides, all estimates of average waiting time all estimates of average waiting time of a process is based on the following of a process is based on the following assumptions:assumptions:
Processes complete their computations Processes complete their computations in a single burst of CPU activity with no in a single burst of CPU activity with no I/OI/O
No time is added to perform context No time is added to perform context switchesswitches
F
I
F
O
P2 P3 P1
headtail
Next to dispatch
Jobs would be executed in the order received. P1 would be executed followed by P3, followed by P2. The wait time of any particular process is entirely dependent upon the processes in front of it, and is often quite long. Lets assume the length of the CPU-burst time (in milliseconds) for each of process above is:
Process
P1P3P2
Burst time
2433
Then the wait time for P1 is 0 milliseconds, 24 milliseconds for P3 and 27 milliseconds for P2. The average wait time is (0+24+27)/3 or 17 milliseconds. However if they arrived in the order P2, P3, then P1, the average wait time would be (0 + 3 + 6)/3 or 3 milliseconds
There is an underlying assumption that all processes complete their activity in a single CPU burst.
Static priority scheduling: Multiple Ready queues
Priority 3
Priority 1
P2
P1P3
Processes are executed in a FIFO order based on priority. In this example the order P2, P1, P3. If another priority 3 process would enter the system it would be executed before any remaining jobs in the priority 1 queue.. In static priority methods the priority of a process does NOT change.
P1P2P3
=====wait===------execute------ done==---execute----- done=============wait======---execute----- done
Shortest Job NextShortest Job Next
Priority is based on the total expected Priority is based on the total expected running time of the processrunning time of the process
Expected running time may be Expected running time may be adjusted if the process waits or is adjusted if the process waits or is suspended after a CPU burstsuspended after a CPU burst
Gives favorable treatment to SHORT Gives favorable treatment to SHORT jobsjobs
Shortest average waiting time: waiting Shortest average waiting time: waiting is concentrated in a few long processesis concentrated in a few long processes
Can lead to starvation!Can lead to starvation!
If we have the following set of processes, with arrive in the following order, and with the length of expected CPU usage in milliseconds:
Process
P1P2P3P4
Expected Cpu Timeneeded
100241035
Wait time
69100
34
The average waiting time would be:
(0+10+34+69)/4= 28 milliseconds
If these jobs had been executed in a FIFO order their wait time would be: p2 -100, p3 – 124, p4 – 134 with an average of (0+100+124+134)/4 or 89 milliseconds!!!!!
Dynamic Priority SchedulingDynamic Priority Scheduling Priority of a process can be increased Priority of a process can be increased
while it waitswhile it waits Aging is the technique used to adjust Aging is the technique used to adjust
process priorityprocess priority A fixed increment is added to the A fixed increment is added to the
priority of each waiting processpriority of each waiting process Processes which have waited a long Processes which have waited a long
time will develop higher and higher time will develop higher and higher prioritiespriorities
Ensures that all waiting processes will Ensures that all waiting processes will be served in a reasonable timebe served in a reasonable time
Preemptive schedulingPreemptive scheduling Higher priority processes get Higher priority processes get
immediate service. immediate service. A process that is preempted is A process that is preempted is
returned to the ready queue, and returned to the ready queue, and possibly it’s priority is adjusted.possibly it’s priority is adjusted.
Well known example is: Shortest Well known example is: Shortest Remaining Time Next!Remaining Time Next!
Assume that the following 4 processes arrive at the listed arrival times, and with the CPU burst time of:
Process Arrival time CPU Burst timeP1 0 8P2 1 4P3 2 9P4 3 5
All times are in milliseconds
P1 arrives at time 0, and begins processing since it is the only process in the ready queue. At time 1, P2 arrives its CPU burst time is 4 which is less that the remaining time for P1 which is 7. P1 is preempted, and P2 begins execution. At time 2, P3 arrives, it’s CPU burst time is 9 which is greater than the remaining time for P2, which is 3, so P2 continues. At time 3, P4 arrives with a CPU burst time of 5. This is larger than the remaining time for P2 (which is 2) so P2 is allowed to complete.
Next, we compare the remaining CPU burst time for P1, P3, and P4. P1 has a remaining time of 7, P3 has a time of 9, and P4 has a time of 5. If no other processes arrive in the system, P4 is selected next for execution, followed by P1, and then finally P3. The average wait time for these 4 processes is:P1 P2 P4 P1 P3
0 1 5 10 17
((10-1) + (1-1)+(17-2)+(5-3))/4 = 26/4 = 6.5
The number subtracted represents the arrival time of the process
Scheduling Interactive Scheduling Interactive ProcessesProcesses
Expected to perform computations in Expected to perform computations in response to requests made by the user.response to requests made by the user.
Requests require short bursts of serviceRequests require short bursts of service Interactive session may be one process Interactive session may be one process
which executes a succession of programswhich executes a succession of programs Or Each new request can create a new Or Each new request can create a new
processprocess Typically nothing is known about their Typically nothing is known about their
resource needs.resource needs.
Levels of schedulingLevels of scheduling Few long-term scheduling decisions, as Few long-term scheduling decisions, as
long as resources permit, processes are long as resources permit, processes are immediately admitted into the systemimmediately admitted into the system
Medium-term scheduling:Medium-term scheduling: Maintains a set of “active” processes.Maintains a set of “active” processes. Makes use of a storage time quantum (PCB)Makes use of a storage time quantum (PCB) Counter is reduced by the running and Counter is reduced by the running and
waiting time the process consumeswaiting time the process consumes When expired the process is swapped to diskWhen expired the process is swapped to disk Ranges from a few seconds to a few minutesRanges from a few seconds to a few minutes
Medium cont.Medium cont. Inactive processes are selected for Inactive processes are selected for
readmission based on:readmission based on: Its assigned priorityIts assigned priority Time in the inactive groupTime in the inactive group Storage requirements.Storage requirements.
Short-Term scheduling: Short-Term scheduling: May select jobs based on priority or their May select jobs based on priority or their
order of entry in the “ready” queue.order of entry in the “ready” queue.
Short term cont….Short term cont…. Preemption may be allowedPreemption may be allowed Makes use of a Processor Time quantumMakes use of a Processor Time quantum
Maximum amount of time a process can Maximum amount of time a process can control the CPU without being interrupted.control the CPU without being interrupted.
If the time quantum expires before the If the time quantum expires before the process voluntarily gives up the CPU it is process voluntarily gives up the CPU it is interrupted, and another process given interrupted, and another process given control of the CPUcontrol of the CPU
Typical values range from 100 milliseconds Typical values range from 100 milliseconds to several seconds.to several seconds.
Using a time quantum to share the Using a time quantum to share the processor is called processor is called time slicingtime slicing
Round RobinRound Robin Scheduling method designed especially Scheduling method designed especially
for interactive processes.for interactive processes. Ready processes are maintained in a Ready processes are maintained in a
simple FIFO queue, all processes have simple FIFO queue, all processes have the same priority.the same priority.
Each process is assigned a time Each process is assigned a time quantumquantum
When the processor becomes available When the processor becomes available it is assigned to the process at the front it is assigned to the process at the front of the queue.of the queue.
P2 P3 P1
headtailRun
Place on end of queue
P1 --------===========--------=============
P2 =====--------===========--------======
P3 ==========--------===========---------
------ represents run and ====== represents wait
Ready queue
Average wait time can become quite long!Average wait time can become quite long!
If the 3 processes above all arrived at time 0, and with expected CPU If the 3 processes above all arrived at time 0, and with expected CPU burst times of:burst times of:
ProcessProcess CPU burst timeCPU burst time P1P1 24 24 P2P2 3 3 P3P3 3 3
If we use a time quantum of 4 milliseconds, the P1 gets the first 4 If we use a time quantum of 4 milliseconds, the P1 gets the first 4 milliseconds, and is preempted. P2 does not need the full time milliseconds, and is preempted. P2 does not need the full time and terminates. P3 also does not need the full time. Finally, P1 and terminates. P3 also does not need the full time. Finally, P1 gets another turn for an additional time quantum. gets another turn for an additional time quantum.
No process receives more than one time quantum without No process receives more than one time quantum without being interruptedbeing interrupted. Even if P1 is the only process in the queue, it . Even if P1 is the only process in the queue, it will be interrupted and placed back on the queue, when its time will be interrupted and placed back on the queue, when its time quantum is exhausted. If there are n processes then each quantum is exhausted. If there are n processes then each process must wait a maximum of process must wait a maximum of (n-1)*q(n-1)*q
Priority methodsPriority methods
We can also apply the “round robin” We can also apply the “round robin” method to a set of processes with method to a set of processes with assigned priorities!assigned priorities!
Ready queue is maintained in priority Ready queue is maintained in priority order (or separate queues for each order (or separate queues for each priority)priority)
Sometimes a process which exceeds its Sometimes a process which exceeds its time quantum is assigned a lower prioritytime quantum is assigned a lower priority
Different priority processes have different Different priority processes have different time quantatime quanta
Ready queue
P9
Priority 5Time 12
P4
Priority 10Time 8
P1
Priority 10Time 8
P6
Priority 15Time 4
P2
Priority 15Time 4
HeadTail
RUN
Feedback queuesFeedback queues This algorithm gives more favorable This algorithm gives more favorable
service to I/O bound algorithmsservice to I/O bound algorithms Multi-level queue scheduling algorithm, Multi-level queue scheduling algorithm,
based on prioritybased on priority Priority of processes that use up Priority of processes that use up
successive time quanta is gradually successive time quanta is gradually reducedreduced
Each time a process is stopped for Each time a process is stopped for exceeding it’s time quanta, it is moved to exceeding it’s time quanta, it is moved to a lower priority!a lower priority!
Queue 0, quantum=8
Queue 1, quantum = 10
Queue 2, quantum = 12
P5
P4P12
P7P3
P6 P1P2
Feedback Queues
Selfish Scheduling Algorithm
4
6
12
3
7
1
12
3
12
3
12
1
Active Queue
Waiting Queue
Move process to active queue
Scheduling Priority
Entrance Priority
Dispatch processes using some standard algorithm
Scheduling Real-time ProcessesScheduling Real-time Processes There is no long-term scheduling. Most There is no long-term scheduling. Most
processes are permanent. Dynamically processes are permanent. Dynamically created processes are admitted immediately. created processes are admitted immediately.
There is no storage scheduling. All processes There is no storage scheduling. All processes remain active and resident in main memory at remain active and resident in main memory at all timesall times
Strict priority based scheduling is used. Strict priority based scheduling is used. Usually high –priority processes can preempt Usually high –priority processes can preempt those of lower priorities. However, processes those of lower priorities. However, processes can lock themselves to block preemption. can lock themselves to block preemption. (typically preemption occurs only when a (typically preemption occurs only when a system call is complete or an I/O block occurs)system call is complete or an I/O block occurs)
Real-time processes are trusted. They Real-time processes are trusted. They may control each other, and they may may control each other, and they may influence the overall scheduling algorithm. influence the overall scheduling algorithm. They may be allowed to change the They may be allowed to change the priority of other processes, if that process priority of other processes, if that process must complete before the real-time must complete before the real-time process can run. (perhaps the lower process can run. (perhaps the lower priority process is modifying data needed priority process is modifying data needed by the real-time process) This ability is by the real-time process) This ability is known as known as priority inversionpriority inversion. .
The maximum resource needs of real-time The maximum resource needs of real-time processes are usually known. processes are usually known.
Optional timesharing is supported within Optional timesharing is supported within priority groups. (time slicing) This may be priority groups. (time slicing) This may be enabled or disabled by any processenabled or disabled by any process
Processes may be scheduled to run Processes may be scheduled to run periodically at specific intervals, or to periodically at specific intervals, or to be started in response to certain be started in response to certain events.events.
Processes may be required to Processes may be required to complete an activity by a specific complete an activity by a specific deadline.deadline.
Processes may create micro processes Processes may create micro processes or threads and schedule these or threads and schedule these privately within their allotted time.privately within their allotted time.