1 / 39

Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling. Outline. Why scheduling? Scheduling Criteria Scheduling Algorithms Real-Time Scheduling Thread Scheduling Example: Solaris Algorithm Evaluation. Process Execution. Process execution consists of alternating cycles of CPU execution and I/O wait

Download Presentation

Chapter 5: CPU Scheduling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5: CPU Scheduling

  2. Outline • Why scheduling? • Scheduling Criteria • Scheduling Algorithms • Real-Time Scheduling • Thread Scheduling • Example: Solaris • Algorithm Evaluation

  3. Process Execution • Process execution consists of alternating cycles of CPU execution and I/O wait • Long-term scheduler gets a good mix of CPU-bound & I/O-bound jobs • Given that, how to scheudle ready processes? – Shor-term scheduling: focus of this chapter

  4. Why scheduling is necessary? • Maximize CPU utilization via multiprogramming, while minimizing response time and/or maximizing throughput → Efficient CPU scheduling required

  5. CPU Scheduler • Decide which process to execute next among the ready processes in memory • Order ready processes according to the specified policy such as FCFS (First Come First Serve)

  6. Preemptive vs. nonpreemptive scheduling • Preemptive scheduling: • A hihger priority process can preempt a currently running low priority process to avoid priority inversion • Most real-time scheduling algorithms, e.g., EDF & RM • More details about RT scheduling will be discussed later • Nonpreemptive scheduling: • A higher priority process waits a currently running process to finish regardless of the priority • e.g., FCFS • Others such as I/O & networking devices are not preemptive

  7. Dispatcher • Gives control of the CPU to the process selected by the short-term scheduler • Context switch between the two user processes • Context switch to user mode • Jump to the proper location in the user program to restart it → Dispatch latency : Time for dispatcher to stop one process and start another

  8. Scheduling Criteria • CPU utilization: #CPU cycles used for computation/#total CPU cycles per unit time • Throughput: # of processes that complete the execution per unit time • Response time: Amount of time it takes from when a request was submitted until the first response is produced

  9. Scheduling Criteria • Waiting time • Amount of time a process has been waiting in the ready queue • Response time = waiting time + execution time

  10. Optimization Criteria • Maximize CPU utilization • Maximize throughput • Minimize response time • Minimize waiting time • Sometimes they may conflict with each other • e.g., system can be overloaded while trying to maximize CPU utilization; response time may significantly increase as a result

  11. P1 P2 P3 0 24 27 30 First-Come, First-Served (FCFS) Scheduling ProcessBurst Time (Execution Time) P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17

  12. P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) • Suppose that processes arrive in the order P2 , P3 , P1 • Gantt chart: • Average waiting time: (6 + 0 + 3)/3 = 3 • Much better than previous case • Convoy effect: A short process behind a long process suffer long wating/response time

  13. Shortest-Job-First (SJF) • Schedule the process with the shortest execution time • Two schemes: • Nonpreemptive • Once the CPU is given to a process it cannot be preempted until completes its execution • Preemptive • Shortest-Remaining-Time-First (SRTF) • If a new process arrives with exec time shorter than the remaining time of the currently running process executing process, preempt

  14. Shortest-Job-First (SJF) • SJF is optimal: It minimizes average waiting time for a given set of processes

  15. P1 P3 P2 P4 0 3 7 8 12 16 Example of Non-Preemptive SJF ProcessArrival TimeExec Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • Average waiting time = (0 + 6 + 3 + 7)/4 = 4

  16. P1 P2 P3 P2 P4 P1 11 16 0 2 4 5 7 Example of Preemptive SJF ProcessArrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • Average waiting time = (9 + 1 + 0 +2)/4 = 3

  17. Hard to implement SJF • Don’t know the exact exec time when a process arrives • Can only estimate it by using the length of previous exec time (Take moving average) • Doesn’t always work

  18. Prediction of the Length of the Next CPU Burst

  19. 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

  20. Priority Scheduling • SJF is a priority scheduling where priority is determined based on the exec time • Problem:Starvation • Low priority processes may never execute or suffer very long waiting time • Solution: Aging • As time progresses increase the priority of the process • How to support fairness while preferring high priority processes? • Hard problem

  21. Round Robin (RR) • Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds • After the time quantum, preempt the process and add to the end of the ready queue

  22. Round Robin • 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 large  FIFO • q small  q must be large with respect to context switch, otherwise overhead is too high

  23. P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Example of RR with Time Quantum = 20 ProcessBurst Time P1 53 P2 17 P3 68 P4 24 • Gantt chart:

  24. Time Quantum and Context Switch Time

  25. Multilevel Queue • Ready queue is partitioned into separate queues, e.g., foreground (interactive) &background (batch) • Each queue can have its own scheduling algorithm • foreground – RR • background – FCFS

  26. Multilevel Queue • Scheduling must be done between the queues • Fixed priority scheduling • Serve all foreground process first • Serve background process only if the foreground queue is empty • Possibility of starvation • Time slice • Each queue gets a certain amount of CPU time which it can schedule amongst its processes, e.g., • 80% to foreground in RR • 20% to background in FCFS

  27. Multilevel Queue Scheduling

  28. Multilevel Feedback Queue • A process can move between queues • Aging can be implemented this way • Defined by the following parameters: • number of queues • scheduling algorithms for each queue • method used to determine when to demote/upgrade a process • method used to determine which queue a process will enter when that process needs service

  29. Multilevel Feedback Queues

  30. Example of Multilevel Feedback Queue • Example: • Q0– RR with time quantum 8 milliseconds • Q1– RR time quantum 16 milliseconds • Q2– FCFS • A new job enters queue Q0 . When it gets CPU, it receives 8 ms. If it does not finish in 8 milliseconds, it is moved to Q1. • At Q1 job receives 16 ms. If it does not complete within 16ms, it is preempted and moved to queue Q2.

  31. Real-Time Scheduling • Hard real-time systems • A deadline miss may cause catastrophic results, e.g., air traffic control, flight control, engine control, etc. • Soft real-time systems • A deadline miss does not cause catastrophic results • Meet as many deadlines as possible, e.g., video streaming

  32. Earliest Deadline First • Optimal Dynamic scheduling algorithm • Schedule the task with the earliest absolute deadline • Advantage: 100% utilization bound • Disadvantage: Domino effect under overload

  33. Rate Monotonic Scheduling • Optimal fixed priority scheduling algorithm • Assign the highest priority to the task with the shortest period • Advantage: Easy to implement • Disadvantage • Low utilization bound, i.e., 69% • Blind to deadlines RMS schedules T1 first T1 T2

  34. Multiple Processor Scheduling • Asymmetric multiprocessing • One processor, called master, makes all scheduling decisions • Symmetric multiprocessing (SMP) • Each processor is self-scheduling • Need to avoid for two processors to schedule one process at the same time • Supported by most OS – Linux, Solaris, Mac OS X, Windows XP, Windows 2000 … • Load balancing: Push vs. pull migration

  35. Thread Scheduling • Local Scheduling (process contention scope) • How the threads library decides which thread to put onto an available LWP • Threads belonging to one process compete for a LWP • Global Scheduling (system contention scope) • How the kernel decides which kernel thread to run next • Assign priority • POSIX Thread API • Select scheduling algorithm • Assign priority to threads

  36. Example: Solaris 2 Scheduling • Assign the highest priority to RT threads • Apply fixed priority among thread groups

  37. Algorithm Evaluation • Deterministic model • Queuing model • Little’s Formula: Queue length n = average arrival rate * average waiting time • Simulation • Implementation

  38. 5.15

  39. End of Chapter 5: Any Questions?

More Related