Download presentation
Presentation is loading. Please wait.
Published byLogan Wright Modified over 9 years ago
1
Scheduling Algorithms
2
A computer system has limited resources that must be shared among the processes competing for those resources. A computer system has limited resources that must be shared among the processes competing for those resources. The operating system must insure that resources are assigned to processes in fair and reasonable way. The operating system must insure that resources are assigned to processes in fair and reasonable way. This is the problem of scheduling. This is the problem of scheduling. Scheduling occurs at many levels, and are handled by various OS components called schedulers. Scheduling occurs at many levels, and are handled by various OS components called schedulers. Most scheduling is performed on the behalf of processes. Most scheduling is performed on the behalf of processes. As processes move from state to state they are under the influence of scheduling. As processes move from state to state they are under the influence of scheduling.
3
Process scheduling involves deciding which process to select, when multiple processes are waiting for service. In general, Scheduling can occur at 3 different levels: Long-term (job scheduling): determining which jobs to admit into the system as processes Short-term (CPU scheduling): determining which process will be given control of the CPU next. Performed by two modules: the scheduler and the dispatcher. Medium term (storage scheduling): determining which of the processes will be active, occupying memory, and which processes will become inactive and have their memory swapped to disk.
4
Begin execution termination CPU bound termination I/O bound termination It’s important to maintain a good process mix.
5
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) SHORTTERMSCHEDULINGSHORTTERMSCHEDULING Inactive (suspended ) MEDIUMTERMSCHEDULINGMEDIUMTERMSCHEDULING Process terminates and exits the system Active (Ready, Blocked)
6
INITIALREADY RUNNING SUSPENDED READY SUSPENDED BLOCKED BLOCKED TERMINAL Terminate when 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 Process swapped back into memory Abort due to error
7
Time Limits – that affect scheduling Time Limits – that affect scheduling Processor time quantum (time slice) Processor time quantum (time slice) Storage time quantum Storage time quantum Job time limit Job time limit
8
BATCHINTERACTIVEREAL-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.
9
Scheduling Objectives Strategies used for scheduling decisions vary widely at each level. Strategies used for scheduling decisions vary widely at each level. Throughput: If the CPU and resources are busy being used then work is 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 completed per time unit. Throughput: If the CPU and resources are busy being used then work is 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 completed per time unit. Turnaround: 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 sum of all of the time periods spent waiting (in the ready queue, in resource queues, waiting to enter memory) and time executing. Turnaround: 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 sum of all of the time periods spent waiting (in the ready queue, in resource queues, waiting to enter memory) and time executing. Response: Goal (particularly for interactive processes) is to process individual requests as quickly as possible. Response: Goal (particularly for interactive processes) is to process individual requests as quickly as possible. Fairness: Goal is to treat each process (or job) in a way that corresponds “fairly” with its characteristics. Essentially process should be given an opportunity to execute. Fairness: Goal is to treat each process (or job) in a way that corresponds “fairly” with its characteristics. Essentially process should be given an opportunity to execute. Consistency: Goal is to treat processes with the same characteristics predictably. Consistency: Goal is to treat processes with the same characteristics predictably. Resource Use: Goal is to keep each category of resource used as fully as possible. Resource Use: Goal is to keep each category of resource used as fully as possible.
10
Measuring scheduling objectives We can evaluate how successfully a scheduling algorithm meets its goals by observing the queues waiting for service or resources. Some of the specific properties we consider are: We can evaluate how successfully a scheduling algorithm meets its goals by observing the queues waiting for service or resources. Some of the specific properties we consider are: Waiting time: A CPU scheduling algorithm only affects how long a process spends waiting in the ready queue. (not how long it will execute, or how long it must wait for resources). Waiting time: A CPU scheduling algorithm only affects how long a process spends waiting in the ready queue. (not how long it will execute, or how long it must wait for resources). Queue Length: this measures the size of the queues of waiting processes. We want to minimize queue length to conserve storage. Queue Length: this measures the size of the queues of waiting processes. We want to minimize queue length to conserve storage. Response Ration: This is a measure of waiting time balanced against 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 ration means that better service is being given to shorter processes, which is usually considered fair. Response Ration: This is a measure of waiting time balanced against 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 ration means that better service is being given to shorter processes, which is usually considered fair.
11
Priorities Priorities Priority is used to represent the relative importance of a process in the system. Priority is used to represent the relative importance of a process in the system. Priorities aid the schedulers in making quick decisions Priorities aid the schedulers in making quick decisions Dynamic vs. Static priorities Dynamic vs. Static priorities Preemption Preemption In a non-preemptive scheduling strategy, a process is assigned the processor until it voluntarily gives it up. In a non-preemptive scheduling strategy, a process is assigned the processor until it voluntarily gives it up. In a preemptive strategy, a high-priority may be given the processor immediately even though a lower or equal priority process is currently running. In a preemptive strategy, a high-priority may be given the processor immediately even though a lower or equal priority process is currently running. Overhead Overhead This is a performance penalty associated with context switching. This is a performance penalty associated with context switching.
12
CPU Scheduling of Batch Processes Submitted with a good estimate of their resource needs, via job control statements Submitted with a good estimate of their resource needs, via job control statements Upper limits the OS is expected to enforce Upper limits the OS is expected to enforce Generally expected that the OS will give preferential treatment to processes with lower resource needs. Generally expected that the OS will give preferential treatment to processes with lower resource needs. Resource estimates may also include the specification of an initial priority for a job Resource estimates may also include the specification of an initial priority for a job
13
For batch processes scheduling occurs at 2 levels: Long-term and short term
14
Job Scheduling (long term) Job scheduling involves deciding which job (job step) to admit into the system as a process. A job is a program which has been submitted to the system, but which has not been loaded into memory. Job scheduling involves deciding which job (job step) to admit into the system as a process. A job is a program which has been submitted to the system, but which has not been loaded into memory. Initial resource estimates play an important role of scheduling at the job level. A job can not be loaded until sufficient resources are available Initial resource estimates play an important role of scheduling at the job level. A job can not be loaded until sufficient resources are available
15
A “Job” 1) typically consists of multiple steps 2) Job control statements provide information about the resource needs of each step. Including program to execute, maximum memory requirements, and cpu time limits, and user information 3) Waiting jobs are stored in one or more job queues, ordered by priority or class, which is determined by their announced resource needs. 4) System processes called initiators, examine the job queues and selects jobs when the system is ready for more work. 5) Jobs are admitted only when sufficient resources are available to begin the first “step”. Also each additional step is admitted only when resources are available. 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 exceed its planned processor time.
16
Short term scheduling When the processor (CPU) is available, a process from the ready queue(s) is selected according to some type of algorithm. When the processor (CPU) is available, a process from the ready queue(s) is selected according to some type of algorithm. The process is then given control of the CPU and continues running until: The process is then given control of the CPU and continues running until:
17
The process terminates The process terminates The process voluntarily suspends itself (sleep) The process voluntarily suspends itself (sleep) The process requests an I/O transfer or other service for which it must wait The process requests an I/O transfer or other service for which it must wait The process is stopped because it has exceeded its processor time quantum (maximum CPU time for the entire job, or step!) The process is stopped because it has exceeded its processor time quantum (maximum CPU time for the entire job, or step!) IF preemption is allowed: a higher priority process becomes ready. (preemption is usually not used in a batch environment.) IF preemption is allowed: a higher priority process becomes ready. (preemption is usually not used in a batch environment.)
18
Batch CPU Scheduling Algorithms examples of scheduling algorithms used later in this presentation were taken from “Operating System Concepts”, by Silberschatz, Galvin, Gagne
19
Please note: On the following slides, all estimates of average waiting time of a process is based on the following assumptions: Processes complete their computations in a single burst of CPU activity with no I/O Processes complete their computations in a single burst of CPU activity with no I/O No time is added to perform context switches No time is added to perform context switches
20
FIFOFIFO P2P3P1 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 P1 P3 P2 Burst time 24 3 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.
21
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. P1 P2 P3 =====wait===------execute------ done ==---execute----- done =============wait======---execute----- done
22
Shortest Job Next Priority is based on the total expected running time of the process Priority is based on the total expected running time of the process Expected running time may be adjusted if the process waits or is suspended after a CPU burst Expected running time may be adjusted if the process waits or is suspended after a CPU burst Gives favorable treatment to SHORT jobs Gives favorable treatment to SHORT jobs Shortest average waiting time: waiting is concentrated in a few long processes Shortest average waiting time: waiting is concentrated in a few long processes Can lead to starvation! Can lead to starvation!
23
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 P1 P2 P3 P4 Expected Cpu Time needed 100 24 10 35 Wait time 69 10 0 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!!!!!
24
Dynamic Priority Scheduling Priority of a process can be increased while it waits Priority of a process can be increased while it waits Aging is the technique used to adjust process priority Aging is the technique used to adjust process priority A fixed increment is added to the priority of each waiting process A fixed increment is added to the priority of each waiting process Processes which have waited a long time will develop higher and higher priorities Processes which have waited a long time will develop higher and higher priorities Ensures that all waiting processes will be served in a reasonable time Ensures that all waiting processes will be served in a reasonable time
25
Preemptive scheduling Higher priority processes get immediate service. Higher priority processes get immediate service. A process that is preempted is returned to the ready queue, and possibly it’s priority is adjusted. A process that is preempted is returned to the ready queue, and possibly it’s priority is adjusted. Well known example is: Shortest Remaining Time Next! Well known example is: Shortest Remaining Time Next!
26
Assume that the following 4 processes arrive at the listed arrival times, and with the CPU burst time of: ProcessArrival timeCPU Burst time P108 P214 P329 P435 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: P1P2P4P1P3 0151017 ((10-1) + (1-1)+(17-2)+(5-3))/4 = 26/4 = 6.5 The number subtracted represents the arrival time of the process
27
Scheduling Interactive Processes Expected to perform computations in response to requests made by the user. Expected to perform computations in response to requests made by the user. Requests require short bursts of service Requests require short bursts of service Interactive session may be one process which executes a succession of programs Interactive session may be one process which executes a succession of programs Or Each new request can create a new process Or Each new request can create a new process Typically nothing is known about their resource needs. Typically nothing is known about their resource needs.
28
Levels of scheduling Few long-term scheduling decisions, as long as resources permit, processes are immediately admitted into the system Few long-term scheduling decisions, as long as resources permit, processes are immediately 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 waiting time the process consumes Counter is reduced by the running and waiting time the process consumes When expired the process is swapped to disk When expired the process is swapped to disk Ranges from a few seconds to a few minutes Ranges from a few seconds to a few minutes
29
Medium cont. Medium cont. Inactive processes are selected for readmission based on: Inactive processes are selected for readmission based on: Its assigned priority Its assigned priority Time in the inactive group Time in the inactive group Storage requirements. Storage requirements. Short-Term scheduling: Short-Term scheduling: May select jobs based on priority or their order of entry in the “ready” queue. May select jobs based on priority or their order of entry in the “ready” queue.
30
Short term cont…. Short term cont…. Preemption may be allowed Preemption may be allowed Makes use of a Processor Time quantum Makes use of a Processor Time quantum Maximum amount of time a process can control the CPU without being interrupted. Maximum amount of time a process can control the CPU without being interrupted. If the time quantum expires before the process voluntarily gives up the CPU it is interrupted, and another process given control of the CPU If the time quantum expires before the process voluntarily gives up the CPU it is interrupted, and another process given control of the CPU Typical values range from 100 milliseconds to several seconds. Typical values range from 100 milliseconds to several seconds. Using a time quantum to share the processor is called time slicing Using a time quantum to share the processor is called time slicing
31
Round Robin Scheduling method designed especially for interactive processes. Scheduling method designed especially for interactive processes. Ready processes are maintained in a simple FIFO queue, all processes have the same priority. Ready processes are maintained in a simple FIFO queue, all processes have the same priority. Each process is assigned a time quantum Each process is assigned a time quantum When the processor becomes available it is assigned to the process at the front of the queue. When the processor becomes available it is assigned to the process at the front of the queue.
32
P2P3P1 headtail Run Place on end of queue P1--------===========--------============= P2=====--------===========--------====== P3==========--------===========--------- ------ represents run and ====== represents wait Ready queue
33
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 burst times of: ProcessCPU burst time P1 24 P1 24 P2 3 P2 3 P3 3 P3 3 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 and terminates. P3 also does not need the full time. Finally, P1 gets another turn for an additional time quantum. No process receives more than one time quantum without being interrupted. Even if P1 is the only process in the queue, it will be interrupted and placed back on the queue, when its time quantum is exhausted. If there are n processes then each process must wait a maximum of (n-1)*q
34
Priority methods We can also apply the “round robin” method to a set of processes with assigned priorities! We can also apply the “round robin” method to a set of processes with assigned priorities! Ready queue is maintained in priority order (or separate queues for each priority) Ready queue is maintained in priority order (or separate queues for each priority) Sometimes a process which exceeds its time quantum is assigned a lower priority Sometimes a process which exceeds its time quantum is assigned a lower priority Different priority processes have different time quanta Different priority processes have different time quanta
35
Ready queue P9 Priority 5 Time 12 P4 Priority 10 Time 8 P1 Priority 10 Time 8 P6 Priority 15 Time 4 P2 Priority 15 Time 4 HeadTail RUN
36
Feedback queues This algorithm gives more favorable service to I/O bound algorithms This algorithm gives more favorable service to I/O bound algorithms Multi-level queue scheduling algorithm, based on priority Multi-level queue scheduling algorithm, based on priority Priority of processes that use up successive time quanta is gradually reduced Priority of processes that use up successive time quanta is gradually reduced Each time a process is stopped for exceeding it’s time quanta, it is moved to a lower priority! Each time a process is stopped for exceeding it’s time quanta, it is moved to a lower priority!
37
Queue 0, quantum=8 Queue 1, quantum = 10 Queue 2, quantum = 12 P5 P4P12 P7P3 P6P1P2 Feedback Queues
38
Selfish Scheduling Algorithm 4646 12 3 7171 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
39
Scheduling Real-time Processes There is no long-term scheduling. Most processes are permanent. Dynamically created processes are admitted immediately. There is no long-term scheduling. Most processes are permanent. Dynamically created processes are admitted immediately. There is no storage scheduling. All processes remain active and resident in main memory at all times There is no storage scheduling. All processes remain active and resident in main memory at all times Strict priority based scheduling is used. Usually high –priority processes can preempt those of lower priorities. However, processes can lock themselves to block preemption. (typically preemption occurs only when a system call is complete or an I/O block occurs) Strict priority based scheduling is used. Usually high –priority processes can preempt those of lower priorities. However, processes can lock themselves to block preemption. (typically preemption occurs only when a system call is complete or an I/O block occurs)
40
Real-time processes are trusted. They may control each other, and they may influence the overall scheduling algorithm. They may be allowed to change the priority of other processes, if that process must complete before the real-time process can run. (perhaps the lower priority process is modifying data needed by the real-time process) This ability is known as priority inversion. Real-time processes are trusted. They may control each other, and they may influence the overall scheduling algorithm. They may be allowed to change the priority of other processes, if that process must complete before the real-time process can run. (perhaps the lower priority process is modifying data needed by the real-time process) This ability is known as priority inversion. The maximum resource needs of real-time processes are usually known. The maximum resource needs of real-time processes are usually known. Optional timesharing is supported within priority groups. (time slicing) This may be enabled or disabled by any process Optional timesharing is supported within priority groups. (time slicing) This may be enabled or disabled by any process
41
Processes may be scheduled to run periodically at specific intervals, or to be started in response to certain events. Processes may be scheduled to run periodically at specific intervals, or to be started in response to certain events. Processes may be required to complete an activity by a specific deadline. Processes may be required to complete an activity by a specific deadline. Processes may create micro processes or threads and schedule these privately within their allotted time. Processes may create micro processes or threads and schedule these privately within their allotted time.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.