Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPU-Scheduling Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The short term scheduler.

Similar presentations


Presentation on theme: "CPU-Scheduling Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The short term scheduler."— Presentation transcript:

1 CPU-Scheduling Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The short term scheduler (or CPU scheduler) selects a process to get the processor (CPU) among the processes which are already in memory. (i.e. from the ready queue). Processor scheduling algorithms try to answer the following crucial question.  Which process in the ready queue will get the processor? In order to answer this, one should consider the relative importance of several performance criteria.

2 Performance Criteria CPU utilization - It is defined as: (processor busy time) / (processor busy time + processor idle time) Throughput – # of processes that complete their execution per time unit. Turnaround time – The interval from the time of submission to the time of completion of a process. Waiting time – amount of time a process has been waiting in the ready queue (waiting for I/O device is not counted) Response time – amount of time it takes from when a request was submitted until the first response is produced, not output.

3 Performance Criteria It is desirable that we,  Maximize CPU utilization  Maximize throughput  Minimize turnaround time  Minimize waiting time  Minimize response time

4 Preemptive – Nonpreemptive Scheduling CPU scheduler selects from 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.  2.Switches from running to ready state.  3.Switches from waiting to ready.  4.Terminates.  5.A new process joins the ready queue 1 2 3 4 5

5 Preemptive – Nonpreemptive Scheduling In (1) and (4), a new process must be selected from the ready queue. In (2), (3) and (5), previously running process or a new process may be selected. Scheduling algorithms that act only in circumstances (1) and (4) are called nonpreemptive. Once CPU has been allocated to a process, that process keeps the CPU until it releases the CPU (either by termination or by requesting I/O) 1 2 3 4 5

6 Preemptive – Nonpreemptive Scheduling A scheduling algorithm which acts on all circumstances is called preemptive. (i.e. such an algorithm can select a new process in circumstances 2, 3 and 5). The CPU is allocated to the highest-priority process among all ready processes. The scheduler is called each time a process enters the ready state. Advantages of non-preemptive scheduling algorithms:  They cannot lead the system to a race condition.  They are simple. Disadvantage of non-preemptive scheduling algorithms:  They do not allow real multiprogramming. Advantage of preemptive scheduling algorithms:  They allow real multiprogramming. Disadvantages of preemptive scheduling algorithms:  They can lead the system to a race condition.

7 First-Come, First-Served (FCFS) Scheduling This is the simplest one. In this algorithm the set of ready processes is managed as FIFO (first-in-first-out) Queue. The processes are serviced by the CPU until completion in the order of their entering in the FIFO queue. Once allocated the CPU, each process keeps it until releasing either due to termination or requesting I/O.

8 Example: FCFS Scheduling Process Arrival Time CPU Burst Time (ms) P 1 0 24 P 2 2 3 P 3 10 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: W(P 1 ) = 0; W(P 2 ) = 22ms; W(P 3 )= 17ms Average waiting time: (0 + 22 + 17)/3 = 13ms P1P1 P2P2 P3P3 2427300

9 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P 2 (AT=0), P 3 (AT=2), P 1 (AT=5). The Gantt chart for the schedule is: W(P 1 )= 1ms; W(P 2 ) = 0 ; W(P 3 ) = 1ms Average waiting time: (1 + 0 + 1)/3 = 0.66ms. tat(P 1 )= 25ms; tat(P 2 ) = 3ms; tat(P 3 ) = 4ms Much smaller wait time than previous case. P1P1 P3P3 P2P2 63300

10 Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. Two versions:  nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst.  preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, then preempt thr current one. This scheme is known as Shortest-Remaining-Time-First (SRTF). SJF is optimal – gives minimum average waiting time for a given set of processes. The main problem with the SJF algorithm is to know for each process the next CPU burst time!!! However some techniques exist to predict this next CPU burst time. Another problem is that starvation of long processes may occur.

11 ProcessArrival TimeCPU Burst Time(ms) P 1 07 P 2 24 P 3 41 P 4 54 SJF (non-preemptive) Average waiting time = (0 + 6 + 3 + 7)/4 = 4ms Example of Non-Preemptive SJF P1P1 P3P3 P2P2 73160 P4P4 812

12 Example of Preemptive SJF (SRTF) ProcessArrival TimeCPU Burst Time(ms) P 1 07 P 2 24 P 3 41 P 4 54 SJF (preemptive) Average waiting time = (9 + 1 + 0 +2)/4 = 3ms P1P1 P3P3 P2P2 42 11 0 P4P4 57 P2P2 P1P1 16

13 Round Robin (RR) (preemptive) This scheduling algorithm is designed specially for time sharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes. Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. 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)q time units. Performance  q very large  FIFO  q small  q must be large with respect to context switch, otherwise overhead is too high.

14 Round Robin (continue) The scheduler allocates the CPU to each process in the ready queue for a time interval of up to 1 time quantum in FIFO (circular) fashion. If the process still running at the end of the quantum; it will be preempted from the CPU A context switch will be executed, and the process will be put at the tail of the ready queue. Then the scheduler will select the next process in the ready queue. However, if the process has blocked or finished before the quantum has elapsed, the scheduler will then proceed with the next process in the ready queue.

15 Example: RR with Time Quantum = 20 ProcessBurst Time(ms) P 1 53 P 2 17 P 3 68 P 4 24 The Gantt chart is: P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P3 02037577797117121134154162

16 Example: RR with Quantum=10ms Consider the following set of process that arrive at time 0 in the order A,B,C,D with the following given CPU burst time. Find the average waiting time with RR of quantum : 10 ms Process CPU burst time (ms) A 20 B 40 C 14 D 6 ABCDABCBB Gantt Chart: 0 10 20 30 36 46 56 60 70 80 W (A) = 36-10 =26 ms W (B) = (10-0) + (46-20) + (60-56)= 40 ms W (C) = (20-0) + (56-30) = 46 ms W (D) = 30-0 = 30 ms Average waiting time = (26+40+46+30) /4 = 142/4 = 35.5 ms

17 Example: RR with Quantum = 10ms Consider the following set of process that arrive at time 0 in the order A,B,C,D with the following given CPU burst time. Find the average waiting time with RR of quantum = 10 ms and context switch time = 2ms Process CPU burst timeI/OCPU burst time A 10 40 10 B 40 - - C 14 - - D 6 - - ABCDBCABB 0 10 12 22 24 34 36 42 44 54 56 60 62 72 74 84 86 96 0 10 50 Wait (A) = 62-50 = 12 ms Wait (B) = 12+22+20+2 = 56 ms Wait (C) = 24 + 22 = 46 ms Wait (D) = 36 ms Average waiting time = 150/4 = 37.5 ms A idle

18 Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest or largest integer value may be defined as the highest priority).  Preemptive  nonpreemptive SJF is a case of the priority scheduling where priority = 1/ (next CPU burst time). Problem  Starvation – low priority processes may never execute. Solution  Aging – as time progresses increase the priority of the process.

19 Example - Priority Scheduling ProcessArrival timeCPU burst timePriority A01003 B0101 C03003 D0605 E801504 Non-preemptive priority Algorithm: DAECB 0 60 160 310 610 620 W (A) = 60ms, W (B) = 610ms, W (C) = 310ms, W (D) = 0 ms, W (E) = 80ms Preemptive priority Algorithm: DAEACB 0 60 80 230 310 610 620 W(A) = 210 ms, W(B) = 610 ms, W(C) = 310 ms, W(D)=0ms, Wait (E) = 0 ms

20 Priority+RR Scheduling Algorithm The priority scheduling algorithm may be combined with the RR algorithm. In this case, processes are grouped into priority classes: Among classes a priority scheduling algorithm is used. Inside a class, RR algorithm is used => 1 ready queue for each class. The following shows a system with four priority classes. As long as there are runnable processes in priority class 4, just run each one for one quantum (i.e. round robin fashion), and never bother with lower priority classes. If priority class 4 in empty, then run the class 3 processes in round robin fashion, and so on. If priorities are not adjusted from time to time, lower priority classes may all starve.

21 Priority+RR Scheduling Algorithm nonpreemptive priority ProcessArrival timeCPU burst time (ms)Priority A0221 B0122 C0232 D0113 E0212 F0153 G30223 Assume quantum =10 ms and 0 switching time, draw the Gantt chart and compute the average waiting time. Queue_1 : A Queue_2 : B, C, E Queue_3 : D, F, G (join the queue at time 30) DFDFBCEBCECEGGGAAA 0 10 20 21 26 36 46 56 58 68 78 81 82 92 102 104 114 124 126 W (A) = 104msW (B) = 46msW (C) =58msW (D) = 10ms W (E) =61ms W (F) = 11ms W (G) =52ms=> Average W: 48.8ms

22 Priority+RR Scheduling Algorithm preemptive priority ProcessArrival timeCPU burst timePriority A0221 B0122 C0232 D0113 E0212 F0153 G30223 Assume quantum =10 ms and 0 switching time, draw the Gantt chart and compute the average waiting time. Queue_1 : A Queue_2 : B, C, E Queue_3 : D, F, G (join the queue at time 30) DFDFBGGGCEBCECEAAA 0 10 20 21 26 36 46 56 58 68 78 80 90 100 103 104 114 124 126 W (A) = 104ms W (B) = 68msW (C) =80msW (D) = 10ms W (E) =83msW (F) =11msW (G) =6ms=> Average W = 51.7ms


Download ppt "CPU-Scheduling Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The short term scheduler."

Similar presentations


Ads by Google