Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPU Scheduling Section 2.5.

Similar presentations


Presentation on theme: "CPU Scheduling Section 2.5."— Presentation transcript:

1 CPU Scheduling Section 2.5

2 CPU scheduling goals

3 Competing performance objectives
Waiting Time. Minimize the total amount of time spent in the ready queue Response Time. (for interactive jobs) Minimize the amount of time from the submission of a job to the first response. CPU utilization. Keep the CPU as busy as possible Throughput. Maximize the number of processes completed in a unit of time Turnaround Time. Minimize the time it takes a process to execute

4 CPU scheduling and process states
new terminated interrupt job sched ready running cpu sched I/O or event wait I/O or event complete waiting

5 How the OS handles CPU allocation
When the CPU becomes idle the short-term scheduler is invoked. It selects a process from the ready queue. Then the dispatcher assigns the CPU to the chosen process. Its functions include: context switching switching from kernel to user mode branching to the proper place in the user process

6 About that ready queue... It contains the PCBs of all processes ready to execute. We refer to it as a queue but it is not necessarily FIFO. It may be a priority queue, a tree, or an unordered linked list The scheduling algorithm determines which of these data structures is used

7 When scheduling decisions are important
When a process is created Should the parent or the child process run? When a process is terminated What if no other process is ready to run? When a process is blocked Why is it blocked? What effect does the reason have on which process is chosen next? When an I/O interrupt occurs Should the process that was waiting on I/O be scheduled immediately?

8 Two kinds of scheduling algorithms
Non-Preemptive Once a process is allocated the CPU a process, it keeps it until it voluntarily relinquishes it (by terminating or switching to another state). Preemptive The OS can ‘bump’ a process from the CPU and allocate it to another process. Which has more overhead?

9 Scheduling algorithm categories
Batch scheduling First-Come, First-Served (FCFS) Shortest Job First (SJF) Shortest Remaining Time (SRT) Three-level scheduling Interactive scheduling Round-Robin Priority Scheduling Multilevel Feedback Queues (MLFQ) Real-Time scheduling Shortest Process Next Guaranteed Scheduling Fair-Share Scheduling

10 Analogy: Waiting to photocopy credit: John Estell, Bluffton College
In an office, we have several people and one photocopier. Each person has a variety of items to photocopy - some have one page, others a few pages out of several books, and there are also those who want to copy an entire book. How should we allocate access to the photocopier?

11 An individual represents a process
A variety of processes: short processes --- copying one page long processes --- copying an entire book CPU-bound processes (performing many computations without interruption) copying a sequence of pages from one book I/O-bound processes (performing only a few computations before an interruption occurs) copying a few pages each from several books, or single pages scattered throughout a single book

12 FCFS (Batch) Whoever arrived first gets to use the machine to make as many copies as desired.

13 FCFS Non-preemptive Processes are assigned CPU in the order in which they request it. Easy to implement. The ready queue is FIFO. What are its weaknesses?

14 FCFS Process CPU burst P1 24 P2 3 P3 3 P1 P2 P3 24 27 30
24 27 30 Average waiting Time = ( )/3 = 17 msec If the processes arrive in order P2, P3, P1 we have: P P P1 3 6 30 Average waiting Time = ( )/3 = 3 msec

15 SJF (Batch) dispatcher “3” goes next! “5” “32” “28” “3”
“5” “32” “28” “3” “31” “26” “11” “28” dispatcher

16 SJF Non-preemptive Processes are assigned the CPU on the basis of the length of their next CPU bursts This algorithm should really be called “shortest next burst” Theoretically optimal when all processes are available at the same time SRT is the preemptive version of SJF

17 SRT (Batch) “2” goes next! “5” “32” “28” “3” “31” “26” “11” “28” “2”

18 SJF Process Burst Time P1 6 P2 8 P3 7 P4 3 P4 P1 P3 P2 24 3 9 16
3 9 16 Average waiting time = ( )/4 = 7 msec With FCFS scheduling the average waiting time would be 10.25 milliseconds - try it out!

19 SJF Process Arrival Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5
Non-preemptive: SJF P1 P2 P4 P3 8 12 17 26 Average waiting time = ( )/4 = 7.75 msec Preemptive: P1 P2 P4 P1 P3 26 1 5 10 17 Average waiting time = ( )/4 = 6.5 msec

20 Three level scheduling (Batch)
Short-term Long-term Intermediate

21 Round Robin (interactive)
N copies at a time! Round Robin (interactive) Dispatcher

22 Round-Robin Preemptive FCFS
A time slice or quantum q, 20  q  50 msec The next process in the ready queue gets up to q msec of CPU time. If the CPU burst of the process < q, it voluntarily relinquishes the CPU If it is > q, a timer goes off, the CPU is interrupted and the process is preempted and put at the end of the ready queue. The next process at the head of the queue is gets the CPU.

23 Process Burst Time Round Robin P1 24 P2 3 P3 3 Time quantum is 4 P1 P2
4 7 10 14 18 22 26 30 Average waiting time = ( )/3 = 5.66 msec Recall that for these same processes with no preemption the average waiting time was 17 msec

24 RR performance depends on the size of the time quantum
If it is very large, it is the same as having no preemption (i.e. FCFS) If it is very small, there are many context switches and valuable CPU time is spent swapping processes in and out A rule of thumb is that 80% of the CPU bursts should be shorter than the quantum

25 Priority (interactive)
The boss goes next! “2” goes next! “5” “32” “28” “3” “31” “26” “11” “28” “200” Boss

26 Priority Scheduling Some processes are deemed more important than others So, processes are assigned numbers indicating their relative priority Preemptive or non-preemptive Static or dynamic SJF is a priority scheduling algorithm the length of the job determines the priority level. How can starvation be avoided?

27 MLFQ (interactive) “3” goes next! max = 10 max = 20 max = 30

28 MLFQ Implemented as multiple level Round-Robin queues:
the highest level has smallest quantum size a process enters the ready queue at the highest level; if it does not complete the first time it gets the CPU, it returns to the ready queue one level down I/O bound and interactive processes usually complete execution after one time in the CPU. CPU-bound processes that wait too long in lower priority queues may be promoted to prevent starvation

29 MLFQ with four classes q = 1 q = 2 q = 4 q = 8

30 Multilevel Queue (hybrid)
Processes are placed in different queues depending on their processing requirements E.g., interactive jobs have different response time requirements from batch jobs Each queue has its own scheduling algorithm. E.g., the foreground (interactive) queue might be RR, the background (batch) queue FCFS There is a scheduling algorithm among queues. The foreground queue has priority over the background queue a background job is run only when the foreground queue is empty

31 A multilevel system of queues
System processes Interactive processes Interactive editing processes Batch processes Each queue has absolute priority over lower queues no batch process can run unless upper queues are empty If a higher priority process enters a queue while a batch process is running, the batch process may be preempted.

32 Parameters of a multilevel scheduler
the number of queues the scheduling algorithm for each queue the method used to determine when to upgrade a process to a higher priority queue the method used to determine when to downgrade a process to a lower priority queue the method used to determine which queue a process will enter initially

33 The lowest levels may starve
Instead of absolute priority, we can time slice between the queues. Each queue gets a certain amount of CPU time, which can then be scheduled among the processes E.g., with two queues we can give 80% of CPU time to the high priority queue, 20% to the secondary one This ensures that background jobs run

34 Shortest Process Next The interactive version of SJF.
Each command is considered as a process The necessary processing time for each command is estimated.

35 Miscellaneous algorithms
Guaranteed scheduling With n users, each gets about 1/n of CPU power Lottery scheduling Randomly distributed “lottery tickets” Lottery may be held 50 times/second Many variations Fair-share scheduling

36 Real-Time Scheduling Time is an important factor in real-time systems
Data must be processed within a given time frame or the system is worthless Program is divided into a number of processes whose behaviors are known in advance They run to completion once they have the CPU

37 Separating scheduling mechanism from the scheduling policy
A process knows which of its children are important and need priority So, provide the mechanism in the kernel but allow user processes to set policy among their child processes & threads Scheduling algorithm is parameterized mechanism is in the kernel Parameters are filled in by user processes policy is set by user process

38 User-level thread scheduling

39 Kernel-level thread scheduling

40 Scheduling algorithm performance
The only accurate way to evaluate a scheduling algorithm is to code it and see how it works This subjects the algorithm to the system’s actual conditions However, it is costly to rewrite the code & modify the operating system, and users must deal with a changing environment

41 Windows NT CPU scheduling
The scheduler runs in the kernel. It is time-sliced (ie, round robin) 20 msec <= quantum <= 200 msec Servers have 6 * quantum of workstations It is priority based real time, high, normal, idle priority classes Threads inherit their process priority & also have relative priorities within their process. It is preemptive MLFQ 32 queues, with absolute priority from top to bottom

42 Linux CPU scheduling Threads are implemented at the kernel level
Scheduling is based on threads, not processes Three classes of threads Real time FIFO Real time round robin Timesharing See pp Priority decreases downward


Download ppt "CPU Scheduling Section 2.5."

Similar presentations


Ads by Google