Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: Process Scheduling.

Similar presentations


Presentation on theme: "Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: Process Scheduling."— Presentation transcript:

1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: Process Scheduling

2 5.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 5: Process Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation

3 5.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Objectives To introduce process scheduling, which is the basis for multiprogrammed operating systems To describe various process-scheduling algorithms To discuss evaluation criteria for selecting a process-scheduling algorithm for a particular system

4 5.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Basic Concepts Maximum CPU utilization obtained with multiprogramming Process execution consists of: A cycle of CPU execution: CPU burst I/O wait: I/O Burst Cycle

5 5.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Alternating Sequence of CPU And I/O Bursts

6 5.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition CPU Scheduler CPU Scheduler or short-term 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 (ex. I/O request or invocation of wait for termination of one of he child process) 2.Switches from running to ready state (ex. an interrupt occur) 3.Switches from waiting to ready (ex. a completion of I/O) 4.When a process terminate Scheduling under 1 and 4 is non-preemptive Scheduling under 2 and 3 is preemptive

7 5.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition CPU Scheduler Microsoft windows 3.X Windows 95 introduced preemptive and all subsequent versions of windows. Mac OS X operating system uses preemptive scheduling. Other previous versions used non-preemptive scheduling.

8 5.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this 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

9 5.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Scheduling Criteria In order to use certain CPU-scheduling algorithm, we have to look at many criteria that used for comparing different scheduling algorithms: CPU utilization – keep the CPU as busy as possible Throughput – number of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process form submission until completion including all waiting times. Waiting time – sum of periods spent waiting in the ready queue Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time- sharing environment)

10 5.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Scheduling Algorithm Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

11 5.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition First-Come, First-Served (FCFS) Scheduling It is simple, first process requests the CPU first, is allocated the CPU first. ProcessBurst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 One negative side: average waiting time is often quit long. P1P1 P2P2 P3P3 2427300

12 5.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition FCFS Scheduling (Cont) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case P1P1 P3P3 P2P2 63300

13 5.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition FCFS Scheduling (Cont) Another important negative is a Convoy effect : When short processes behind long process. Ex. Suppose P1 is CPU-bound and many I/O-bound processes. CPU-bound process gets the CPU, other I/O-bound processes will move into ready queue when completing I/O operations. I/O devices will remain idle and all other I/O-bound processes will wait until CPU-bound process is done. Convoy effect is when all other processes are waiting for one big process to get off the CPU. FCFS Scheduling algorithm is non-preemptive. Once process keeps the CPU until it releases the CPU, either by termination or request I/O.

14 5.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst not the total length of execution. Thus, some books use shortest-next-CPU-burst algorithm. Use these lengths to schedule the process with the shortest time. When the CPU is free, it is assigned the next process with the smallest CPU burst. SJF is optimal – gives minimum average waiting time for a given set of processes The difficulty is knowing the length of the next CPU request

15 5.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of SJF Process Burst Time P 1 6 P 2 8 P 3 7 P 4 3 SJF scheduling chart Waiting time for P 1 = 3; P 2 =16 ; P 3 = 9; P4=0 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 P4P4 P3P3 P1P1 3 16 0 9 P2P2 24

16 5.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First (SJF) Scheduling The problem is the difficulty to know length of the next CPU request in short-term CPU scheduling. There is no way to know! But how we can predict the length? One approach is to approximate SJF scheduling by prediction. We expect the length of the next CPU burst will be similar in length of the previous ones using exponential averaging. We use this formula:

17 5.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Determining Length of Next CPU Burst Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging.

18 5.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Prediction of the Length of the Next CPU Burst Here we have =1/2 and =10

19 5.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First (SJF) Scheduling The SJF algorithm can be either preemptive or non-preemptive. Ex: suppose a new process arrives at the ready queue while a previous process is still executing. The new arrived process is shorter than what is left of the currently executing process. We have two options: The preemptive SJF algorithm will preempt the current executed process. So it is called shortest-remaining-time-first-scheduling The non-preemptive SJF algorithm will allow the current executed process to complete its CPU burst.

20 5.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of SJF ProcessArrival TimeBurst Time P 1 08 P 2 14 P 3 29 P 4 35 Then preemptive SJF scheduling chart P1=0 time, P2 arrives at time 1, (remaining time for P1=7) so P1 is preempted and P2 is scheduled. Average waiting time = [(10-1)+(1-1)+(17-2)+(5-3)] / 4 = 6.5 In non-preemptive SJF scheduling average time is 7.75. P1P1 5 17 0 10 P3P3 26 P1P1 P4P4 P2P2 1

21 5.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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 nonpreemptive 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

22 5.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Priority Scheduling The SJF algorithm is a special case of the priority scheduling algorithm. A priority number (integer) is associated with each process Equal priority processes are scheduled in FCFS order. The larger the CPU burst, the lower the priority. Priorities are generally indicated by some fixed range of numbers such as 0- 7 or 0-4095. 0 can be highest or lowest priority since there is no agreement on whether 0 is the highest or lowest priority. In our book, we assume that low numbers represent high priority. 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:

23 5.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of Priority Scheduling ProcessBurstTimePriority P 1 103 P 2 11 P 3 24 P 4 15 P 5 52 Then the priority scheduling chart: Waiting time for P 1 = 6; P 2 =0 ; P 3 = 16; P4=18; P5=1 Average waiting time = (6+ 0 + 16 + 18 + 1) / 5 = 8.2 Scheduling average time is 8.2 milliseconds P3P3 6 18 0 16 P4P4 19 P2P2 P1P1 P5P5 1

24 5.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Priority Scheduling 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 current running in the CPU, what happen? The preemptive algorithm will preempt the CPU if the priority of the newly process is higher than the priority of the currently running process. The non-preemptive algorithm will put the new process at the head of the ready queue.( even if there are processes already waiting before)

25 5.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Priority Scheduling Problem: A major problem is indefinite blocking or starvation. Because the algorithm can leave some low priority processes waiting indefinitely. In a heavily loaded system: a steady stream of higher-priority processes can prevent a low priority process from ever getting the CPU. Generally, one of two things will happen: The process will be run at 2 AM Sunday when the system is finally lightly loaded. OR The computer system will crash and lose all uncompleted low priority processes. Rumor !: when they shut-down the IBM7094 at MIT in 1973, they found a low priority process that had been submitted in 1967 and had not yet been run!.

26 5.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Priority Scheduling What is the solution of indefinite blocking or starvation of low priority processes? Aging: Is a technique of gradually increasing the priority of processes that wait in the system for a long time. For example: if priorities range from 127 low to 0 high, we can increase the priority of a waiting process by 1 every 15 minutes. Eventually, even a process with an initial priority of 127 would have the highest priority in the system with time. In fact, a process with a priority 127 (low) will take no more than 32 hours to age to a priority with 0.

27 5.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin (RR) Each process gets a small unit of CPU time (time quantum). time quantum or time slice is a small unit of time Usually time quantum is about 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. So RR scheduling algorithm is similar with FCFS scheduling, but preemption is added here.

28 5.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin (RR) To implement RR scheduling: keep the ready queue as a FIFO (first in first out) queue of processes. New processes are added to the tail of the ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the process. We have two cases: 1. Process may have a CPU burst of less than 1 time quantum. The Process itself will release the CPU voluntary. 2. The CPU burst of the current process is longer than the 1 time quantum, the timer will go off and will cause as interrupt to the OS. Context switch will be executed and process will be put at the tail of the ready queue. The average waiting time is often long.

29 5.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of RR with Time Quantum = 4 Consider the following processes arrived at time 0, using a time quantum of 4 milliseconds: ProcessBurst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: waiting time: P1=(10-4); P2=4; P3=7; average waiting time: [(4+4+7)/3]=5.66 milliseconds Typically, higher average turnaround than SJF, but better response P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P1 0 47 101418222630

30 5.30 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin (RR) In RR scheduling algorithm, no process is allocated the CPU for more than 1 time quantum unless it’s the only process in the CPU. If there are n processes in the ready queue and the time quantum is q, then: each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1) X q time units. (1) Performance depends heavily on the size of time quantum If the time quantum q is large  RR is similar to FCFS policy. If the time quantum q is small  the RR approach is called processor sharing since each process has its own processor running at 1/n speed of the real processor; q must be large with respect to context switch, otherwise overhead is too high

31 5.31 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Time Quantum and Context Switch Time (2) we need to consider the effect of context switch on the performance of RR scheduling: Assume we have a process of 10 time units: with quantum of 12 time units  P needs less than one time quantum with quantum of 6 time units  P needs 2 time quantum with 1 context switch. with quantum of 1 time units  P needs 10 time quantum with 9 context switches.

32 5.32 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin (RR) So, we need the time quantum to be large with respect to the context switch time. If the context switch is approximately 10% of the time quantum  10% of the CPU time will go in context switch. Turnaround time also depends on size of time quantum see next figure. From the figure, the average turnaround time does not necessarily improve as the time –quantum size increase. In general: the average turnaround time can be improved if most processes finish their next CPU burst in a single time quantum.

33 5.33 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Turnaround Time Varies With The Time Quantum

34 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, End of Chapter 5


Download ppt "Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: Process Scheduling."

Similar presentations


Ads by Google