Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Operating System Created by : Zahid Javed CPU Scheduling Fifth Lecture.

Similar presentations


Presentation on theme: "Introduction to Operating System Created by : Zahid Javed CPU Scheduling Fifth Lecture."— Presentation transcript:

1 Introduction to Operating System Created by : Zahid Javed CPU Scheduling Fifth Lecture

2 CPU Scheduling (Core Definitions) Scheduling Objectives CPU I/O Burst Cycle Working of CPU Scheduler Preemptive Scheduling Non-Preemptive Scheduling Dispatcher Scheduling Criteria Scheduling Algorithm Optimization Criteria First-Come, First-Served (FCFS) Scheduling FCFS Scheduling (non-preemptive) Shortest-Job-First (SJF) Scheduling Determining Length of Next CPU Burst Example of SJF Non-preemptive SJF Example Priority Scheduling Non-Preemptive Priority Scheduling Prove It (SJF is Optimal in Non- Preemptive) Round Robin (RR) Multilevel Queue Scheduling Multilevel Feedback Queue Today Agenda

3 CPU Scheduling (Core Definitions)  General rule — keep the CPU busy; an idle CPU is a wasted CPU  Major source of CPU idleness: I/O (or waiting for it)  CPU scheduling (short-term scheduling) is the act of selecting the next process for the CPU to “service” once the current process leaves the CPU idle  Many algorithms for making this selection  Implementation-wise, CPU scheduling manipulates the operating system’s various PCB queues  The dispatcher is the software that performs the dirty work of passing the CPU to the next selected process

4 Scheduling Objectives  Maximize CPU utilization  Maximize utilization of other resource(Disk, printer)  Maximize throughout = number of jobs completed per unit time.  Minimize waiting time = total time a job spends waiting in the various queues for resource to become available.  Minimize turnaround time = Waiting + Computation + I /O time  Minimize response time (timesharing) = time from entry of a command until first output starts to appear.  Fairness: All comparable jobs should be treated equally

5 CPU I/O Burst Cycle Almost all processes alternate between two states in a continuing cycle, as shown in Figure below :  A CPU burst of performing calculations, and  An I/O burst, waiting for data transfer in or out of the system.

6 Working of 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 (Ex. I/O Request) 2.Switches from running to ready state (Ex. Interrupts Occur) 3.Switches from waiting to ready (Ex. Completion of I/O ) 4.Terminates Scheduling under 1 and 4 is non-preemptive All other scheduling is preemptive

7 Preemptive Scheduling Preemptive: Preemptive algorithms are driven by the notion of prioritized computation. The process with the highest priority should always be the one currently using the processor. If a process is currently using the processor and a new process with a higher priority enters, the ready list, the process on the processor should be removed and returned to the ready list until it is once again the highest- priority process in the system

8 Non-Preemptive Scheduling Non-Preemptive: Non-preemptive algorithms are designed so that once a process enters the running state(is allowed a process), it is not removed from the processor until it has completed its service time. context_switch() is called only when the process terminates or blocks.

9 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

10 Scheduling Criteria

11

12 Scheduling Algorithm Optimization Criteria  Max CPU utilization  Max throughput  Min turnaround time  Min waiting time  Min response time

13 Scheduling Algorithms 1.First-Come, First-Served (FCFS) Scheduling 2.Shortest-Job-First (SJF) Scheduling 3.Priority Scheduling 4.Round robin Scheduling 5.Multilevel queue Scheduling 6.Multilevel feedback-queue Scheduling

14 First-Come, First-Served (FCFS) Scheduling 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 P1P1 P2P2 P3P3 2427300

15 FCFS Scheduling (non-preemptive) 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 Convoy effect  short process behind long process P1P1 P3P3 P2P2 63300

16 Convoy Effect

17

18 FCFS Scheduling (non-preemptive) Process Name Arrival timeBurst timeFinish timeWaiting time Process 1 024306 Process 2 0330 Process 3 0363 Average waiting time: (6 + 0 + 3)/3 = 3 It is not good scheduling algorithm for system where response time is important (i.e. real time or time sharing system)

19 Shortest-Job-First (SJF) Scheduling Pick the job that will take the least amount of time to complete Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time 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 Two schemes of this algorithm Non-preemptive preemptive

20 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

21 Example of SJF ProcessBurst time P16 P28 P37 P43 P1P3P2 03916 24

22 Non-preemptive SJF Example ProcessArrival timeBurst time P107 P224 P342 P454 P1P3P2P4 07913 17 Process Name Arrival timeBurst timeFinish timeWaiting time Process 1 0770 Process 2 24137 Process 3 4293 Process 4 54178 Gantt Chart Average waiting time: (0 + 7 + 3 + 8) / 4 = 4.5 Waiting time for the processes in this example Note:- Waiting time= Finish Time-(Arrival time + Burst time)

23 Preemptive or non-preemptive Set Scheduling algorithm –Every 1 arrival time –100 Microsecond –New job come Shortest-remaining- time-first scheduling (SRTF)

24 Preemptive SJF Example ProcessArrival timeBurst time P107 P224 P341 P454 P1P2P3P2P4P1 0245711 16 Process Name Arrival timeBurst timeFinish timeWaiting time Process 1 07169 Process 2 2471 Process 3 4250 Process 4 54112 Gantt Chart Average waiting time: (9 + 1 + 0 + 2) / 4 = 3 Waiting time for the processes in this example

25 Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer mean highest priority) Preemptive Non-preemptive SJF is a priority scheduling where priority is the predicted next CPU burst time

26 Problem :- Starvation – low priority processes may never execute Solution :- Aging – as time progresses increase the priority of the process Priority Scheduling

27 Non-Preemptive Priority Scheduling ProcessBurst timePriority P1103 P211 P324 P415 P552 P2P5P1P3P4 01 6 18 19 Gantt Chart Average waiting time: (1 + 6 + 16 + 18 + 19) / 5 = 8.2

28 Preemptive Priority Scheduling ProcessArrival timeBurst timePriority P10103 P2211 P3422 P4614 P5855 P1P2P1P3P1P4P5 02 346 13 14 19 Gantt Chart

29 Prove It (SJF is Optimal in Non- Preemptive) Logical Argument: Decrease in the wait times for short processes is much more than increase in the wait times for long processes P1P2P3 5 32 Gantt Chart P3P2P1 2 35 Change Order F=(0+5+8) / 3= 4.33 S=(0+2+5) / 3=2.33

30 Round Robin (RR) 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+t cs ) time units. If n=6 and q=5 then (6-1)*5= 25 Performance –q large  FIFO or FCFS –q small  q must be large with respect to context switch, otherwise overhead is too high

31 Example of RR with Time Quantum = 4 The Gantt chart is: Typically, higher average turnaround than SJF, but better response and it is preemptive scheduling P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P1 0 47 101418222630 ProcessBurst time P124 P23 P33

32 Example of RR with Time Quantum = 20 The Gantt chart is: ProcessBurst time FSTF P1533313 P217 P36848288 P4244 P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 0 2037 577797117121134 P3P3 P3P3 154 162

33 Example of RR with Time Quantum = 20  Average waiting time: - 73  Average waiting time for SJF: - 38  Typically, higher average turnaround than SJF, but better response ProcessTurnaround time Waiting time P1134134 – 53 = 81 P23737 – 17 = 20 P3162162 – 68 = 94 P4121121 – 24 = 97

34 Time Quantum and Context Switch Time

35 Multilevel Queue Scheduling Ready queue is partitioned into separate queues: foreground (interactive) background (batch) Each queue has its own scheduling algorithm –foreground – RR –background – FCFS

36 Multilevel Queue Scheduling

37 Scheduling must be done between the queues: –Fixed (Absolute) priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. –Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR –20% to background in FCFS

38 Multilevel Feedback Queue A process can move between the various queues; aging can be implemented this way Multilevel-feedback-queue scheduler defined by the following parameters: –number of queues –scheduling algorithms for each queue –method used to determine when to upgrade a process –method used to determine when to demote a process –method used to determine which queue a process will enter when that process needs service

39 Example of Multilevel Feedback Queue Three queues: –Q 0 – RR with time quantum 8 milliseconds –Q 1 – RR time quantum 16 milliseconds –Q 2 – FCFS Scheduling –A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. –At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.

40 Multilevel Feedback Queue


Download ppt "Introduction to Operating System Created by : Zahid Javed CPU Scheduling Fifth Lecture."

Similar presentations


Ads by Google