1.06k likes | 1.62k Views
Chapter 5: Process Scheduling. Basic Concepts. CPU scheduling is the basic of multiprogramming OS. Scheduling is a fundamental OS function. Almost all computer resources are scheduled before use. The CPU is one of the primary computer resources.
E N D
Basic Concepts • CPU scheduling is the basic of multiprogramming OS. • Scheduling is a fundamental OS function. • Almost all computer resources are scheduled before use. • The CPU is one of the primary computer resources. • By switching the CPU among processes, the OS can make the computer more productive. • Thus its scheduling is central to OS design
CPU-I/O Burst Cycle • CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait • Processes alternate between these two states. • Process execution begins with a CPU burst. • That is followed by an I/O burst, then another CPU burst, then another I/O burst and so on.
CPU-I/O Burst Cycle • Finally, the last CPU burst will end with a system request to terminate execution , rather than with another I/O burst. • This distribution can help us select an appropriate CPU-scheduling algorithm.
CPU-I/O Burst Cycle • The duration of these CPU burst have been extensively measured. • In measured in histogram the curve is generally characterized as exponential or hyper exponential with many short CPU bursts, and a few long CPU bursts.
CPU Scheduler • Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them • CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state • Switches from running to terminates state • Switches from running to ready state 3. Switches from waiting to ready state
Preemptive versus Non-preemptive Scheduling • Non-Preemptive scheduling : • Scheduling takes place only under first two of the above cases. • Here, one process is selected to execute. Then, it is allowed to run until it voluntarily enter into the wait state (case 1) or gets terminate (case 2). • Simple, but its not efficient. • doesn’t require timer or clock interrupt.
Preemptive versus Non-preemptive Scheduling • Preemptive scheduling : • Scheduling takes place only under any of the above cases. • Here, one process is selected to execute. Then, it is allowed to run only for some maximum time duration. After the time duration, another process is selected to execute. • Very efficient, but complex. • require timer or clock interrupt.
Dispatcher • Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this function involves: • switching context • switching to user mode • jumping to the proper location in the user program to restart that program • Dispatch latency – time it takes for the dispatcher to stop one process and start another running
Scheduling Criteria • Various scheduling algorithms are available. • They work based on different criteria to select a process. • To compare the performance of different scheduling algorithms, various performance criteria are decided.
Scheduling Criteria • They are as given below : • CPU utilization : • It is an average fraction of time during which CPU is busy. • It ranges form 0 to 100 percent. In a real system, it should be between 40 to 90 percent. • CPU should remain as busy as possible, or CPU utilization should remain as high as possible.
Scheduling Criteria • Throughput : • Number of processes completed per time unit is called throughput. • For long process, this rate may be 1 hour; for short transactions, throughput might be 10 processes per second. • Turnaround time : • Time required to complete execution of a process is called turn-around time. • It specifies that how long it takes to complete a process execution. • Turnaround time=process finish time-process arrival time.
Scheduling Criteria • Waiting time : • It is total time duration spent by a process waiting in ‘Ready’ queue. • Waiting time=turnaround time-actual execution time. • Scheduling algorithm affects the time that a process spends waiting in the ‘Ready’ queue.
Scheduling Criteria • Response time : • It is time between issuing a command/request and getting output/result. • It is more important in interactive system. • User requests should be served as quickly as possible. • Here, user requests are given more priority compared to backgroundsystem processes.
Optimization Criteria • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time
Scheduling Algorithms • There are four different algorithms are described bellow : • First-Come, First-Served (FCFS) Scheduling • Shortest-Job-First (SJF) Scheduling • Priority Scheduling • Round-Robin Scheduling
First-Come, First-Served (FCFS) Scheduling • Selection criteria : • The process that requests first is served first. It means that processes are served in the exact order as they come. • Decision Mode: • Non-Preemptive: once a process is selected, it runs until it blocks for an I/O or some event, or it terminates.
First-Come, First-Served (FCFS) Scheduling • Implementation: • This strategy can be easily implemented by using FIFO queue. • FIFO means first in first out. When first process enters the system, it starts its execution. • All other processes are appended in a queue. When CPU becomes free, a process from the first position in a queue is selected to run.
First-Come, First-Served (FCFS) Scheduling • Example: • Consider the following set of four processes. Their arrival time and time required to complete the execution are given in following table. • Consider all time values in milliseconds.
First-Come, First-Served (FCFS) Scheduling • Gantt Chart: • Average Turnaround Time: (10+15+15+17)/4=57/4=14.25 ms • Average Waiting Time: (0+9+13+13)/4=35/4=8.75 ms P1 P0 P2 P3 22 22 22 22 22 18 18 18 18 18 18 16 16 16 16 10 10 10 0 0
First-Come, First-Served (FCFS) Scheduling • Initially only process P0 is present and it is allowed to run. • But, when P0 completes, all other processes are present. • So, next process P1 from ready queue is selected and allowed to run till it completes. • This procedure is repeated till all processes complete their execution.
First-Come, First-Served (FCFS) Scheduling • Advantages: • Simple, Fair • Easy to understand, easy to implement • Disadvantages: • Not efficient. Average waiting time is too high. • CPU utilization may be too low. Consider a CPU-bound process running with many I/O bound process.
Shortest-Job-First (SJR) Scheduling • Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time • Two schemes: • Non-Preemptive – once CPU given to the process it cannot be preempted until completes its CPU burst
Shortest-Job-First (SJF) Scheduling • Preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF) • SJF is optimal – gives minimum average waiting time for a given set of processes
Shortest-Job-First (SJF) SchedulingNon-preemptive • Selection Criteria: • The process that requires shortest time to complete execution, is served first. • Decision Mode: • Non-preemptive: Once a process is selected, it runs until it blocks for an I/O or some event, or it terminates.
Shortest-Job-First (SJF) SchedulingNon-preemptive • Implementation: • This strategy can be implemented by using sorted FIFO queue. • All processes in a queue are sorted in ascending order based on their required CPU burst. • When CPU becomes free, a process from the first position in queue is selected to run.
Shortest-Job-First (SJF) SchedulingNon-preemptive • Example: • Consider the following set of four processes. Their arrival time and time required to complete the execution are given in following table. • Consider all time values in milliseconds.
Shortest-Job-First (SJF) SchedulingNon-preemptive • Gantt Chart: • Average Turnaround Time: (10+21+9+11)/4=51/4=12.75 ms • Average Waiting Time: (0+15+7+7)/4=29/4=7.25 ms P0 P3 P1 P2 22 16 12 10 0
Shortest-Job-First (SJF) SchedulingNon-preemptive • Initially only process P0 is present and it is allowed to run. • But, when P0 completes, all other processes are present. • So, process with shortest CPU burst P2 is selected and allowed to run till it completes. • Whenever more than one process is available, such type of decision is taken. • This procedure is repeated till all process complete their execution.
Shortest-Job-First (SJF) SchedulingNon-preemptive • Advantages: • Less waiting time. • Good response for short processes • Disadvantages: • It is difficult to estimate time required to complete execution. • Starvation is possible for long process. Long process may wait for ever.
Shortest-Job-First (SJF) SchedulingPreemptive • Selection Criteria: • The process, whose remaining run time is shortest, is served first. • This is a preemptive version of SJF scheduling • Decision Mode: • Preemptive: when a new process arrives, it total time is compared to the current process remaining time.
Shortest-Job-First (SJF) Scheduling Preemptive • If the new job needs less time to finish than the current process, the current process is suspended and new job started. • Implementation: • This strategy can be implemented by using sorted FIFO queue. • All processes in a queue are sorted in ascending order based on their remaining run time. • When CPU becomes free, a process from the first position in queue is selected to run.
Shortest-Job-First (SJF) SchedulingPreemptive • Example: • Consider the following set of four processes. Their arrival time and time required to complete the execution are given in following table. • Consider all time values in milliseconds.
Shortest-Job-First (SJF) SchedulingPreemptive • Gantt Chart: • Average Turnaround Time: (22+8+2+8)/4=40/4=10.00 ms • Average Waiting Time: (12+2+0+4)/4=18/4=4.5 ms P1 P3 P1 P1 P1 P1 P1 P1 P0 P0 P0 P0 P0 P2 P2 P2 P2 P2 P2 P2 5 9 3 22 13 0 1
Shortest-Job-First (SJF) SchedulingPreemptive • Initially only process P0 is present and it is allowed to run. • But, when P1 comes, it has shortest remaining run time. • So, P0 is preempted and P1 is allowed to run. • Whenever new process comes or current process blocks, such type of decision is taken. • This procedure is repeated till all process complete their execution.
Shortest-Job-First (SJF) SchedulingPreemptive • Advantages: • Less waiting time. • Good response for short processes • Disadvantages: • Starvation is possible for long process. Long process may wait for ever. • Context switch overhead is there.
Priority Scheduling • A priority number (integer) is associated with each process • The CPU is allocated to the process with the highest priority (smallest integer highest priority) • Preemptive • Non-Preemptive • SJF is a priority scheduling where priority is the predicted next CPU burst time • Problem Starvation – low priority processes may never execute • Solution Aging – as time progresses increase the priority of the process
Priority Based SchedulingNon-Preemptive • Selection Criteria: • The process, the has highest priority is served first. • Decision Mode: • Non-preemptive: Once a process is selected, it runs until it blocks for an I/O or some event, or it terminates.
Priority Based SchedulingNon-Preemptive • Implementation: • This strategy can be implemented by using sorted FIFO queue. • All processes in a queue are sorted based on their priority with highest priority process at front end. • When CPU becomes free, a process from the first position in queue is selected to run.
Priority Based Scheduling Non-Preemptive • Example: • Consider the following set of four processes. Their arrival time and time required to complete the execution and priorities are given in following table. • Consider all time values in milliseconds and small value for priority means higher priority of a process.
Priority Based Scheduling Non-Preemptive • Gantt Chart: • Average Turnaround Time: (10+21+13+9)/4=53/4=13.25 ms • Average Waiting Time: (0+15+11+5)/4=31/4=7.75 ms P2 P0 P1 P3 14 22 16 0 10
Priority Based Scheduling Non-Preemptive • Initially only process P0 is present and it is allowed to run. • But, when P0 completes, all other processes are present. • So, process with highest priority P3 selected and allowed to run till it completes. • Whenever more than one process is available, such type of decision is taken. • This procedure is repeated till all processes complete their execution.
Priority Based Scheduling Non-Preemptive • Advantages: • Priority is considered. Critical processes can get better response time. • Disadvantages: • Starvation is possible for low priority processes. It can be overcome by using technique called ‘Aging’. • Aging : gradually increases the priority of processes that wait in the system for a long time.
Priority Based SchedulingPreemptive • Selection Criteria: • The process, the has highest priority is served first. • Decision Mode: • Preemptive: When a new process arrives, its priority is compared to the current process priority. • If the new job has higher priority than the current process, the current process is suspended and the new job started.