1 / 48

Process Scheduling in CPU - Optimization and Algorithms

Learn about process scheduling in CPU, including the short-term scheduler, goals of scheduling, types of scheduling algorithms, and examples of FCFS, SJF, and RR. Optimize system goals such as fairness, CPU utilization, response time, and turnaround time.

tmead
Download Presentation

Process Scheduling in CPU - Optimization and Algorithms

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. Cosc 4740 Chapter 5 Process Scheduling

  2. CPU Scheduling • Short-term Scheduler • Selects a process from the ready queue when current process releases the CPU • Very Fast to avoid wasting CPU time • Very efficient • the problem: selecting a CPU algorithm for a particular system • Also updates PCBs and controls the queues.

  3. Basic concept of a Process • CPU–I/O Burst Cycle • Process execution consists of a cycle of CPU execution and I/O wait

  4. How to decide which ready process to put on the CPU next?

  5. Goals • Fairness or Waiting time • amount of time a process has been waiting in the ready queue • – No process will wait “too” long • CPU utilization – keep it busy most of the time • normally about 40-90% of the time • Response time – for interactive users • Turnaround time • the time of completion from submission • Throughput: • # of processes that complete their execution per time unit Consider if it is possible to Max all 5 goals

  6. We want to optimize all the goals • This is not possible: • Fairness vs CPU utilization • All schedulers favor a couple of goals. • Normally those goals that help the kind of system it written for.

  7. 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 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates • Scheduling under 1 and 4 is nonpreemptive • All other scheduling is preemptive

  8. 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. Types of Scheduling Algorithms • Non-preemptive Scheduling • A running process is only suspended when it blocks or terminates • Pros: • Decreases turnaround time, high CPU utilization • Does not require special HW (like a timer) • Cons: • Poor performance for response time and fairness • Limited choice of scheduling Algorithms

  10. Preemptive scheduling • A Running process can be taken off the CPU for any (or no) reason. Suspended by the scheduler • Pros: • No limitations on the choice of scheduling algorithms • Increased Fairness and response time • Cons • Addition overheads (frequent context switching) • deceased CPU utilization and longer turnaround time

  11. FCFS scheduling • First-Come First Served (non-preemptive) • CPU is allocated to the process that requests it first • Pros: • Simple to understand and implement • Very fast selection for scheduling • Cons: • Avg. waiting time is long and varies • Not suitable for time-sharing OS

  12. P1 P2 P3 0 24 27 30 FCFS Example 1 ProcessBurst 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

  13. P2 P3 P1 0 3 6 30 FCFS Example 2 Suppose that the processes arrive in the order P2 , P3 , P1 • The Gantt chart for the schedule is: • Waiting time for P1 = 6;P2 = 0; P3 = 3 • Average waiting time: (6 + 0 + 3)/3 = 3 • Much better than previous case • Convoy effect short process behind long process

  14. SJF Scheduling • Shortest Job Next: Preemptive or Non-Preemptive • Pros: • Yields minimum avg. waiting time for a given set of processes • Cons: • Difficult to estimate CPU burst time • Starvation (indefinite blocking)

  15. P1 P3 P2 P4 0 3 7 8 12 16 Example of Non-Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (non-preemptive) • 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 Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (preemptive) • Average waiting time = (9 + 1 + 0 +2)/4 = 3

  17. 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. Round Robin Scheduling • Preemptive scheduling • Most widely used algorithm • Very simple – CPU isn’t taxed by scheduler • Fair • Each Process is allowed a time interval call a “quantum” or “time slice” • Max time process can run on CPU

  19. If a process is still running at the end of a quantum, the scheduler preempts it. • If a process blocks or terminates before it quantum expires, another is allocated immediately CPUReady queue O PQRS (Then quantum expiries) P QRSO

  20. P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Example of RR Time Quantum = 20 ProcessBurst Time P1 53 P2 17 P3 68 P4 24 • The Gantt chart is: • Typically, higher average turnaround than SJF, but better response time

  21. Quantum • How long should a quantum be? • Problems: • quantum is too small (20 ms) • CPU is under utilized – to much context switching • quantum is too large (1 second) • Not fair, poor response time

  22. Typical quantum between 100ms and 200ms • When O/S is installed it is tested and adjusted for specific environment • OS could monitor itself, but it is complex • scheduler would not be simple • So normally done by “hand”.

  23. Priority Scheduling • Not all processes are equally important • Each process is assigned a priority (integer value) • Process with highest priority runs • If more than 1 with same highest priority, then Round Robin among those processes

  24. Priority Scheduling (2) • 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

  25. Priority Scheduling (3) • Array of priority queues Highest 0 PQR 1 OS 2 3 A Lowest 4  C • Priority must change over time, or low priority processes will suffer (starve) • Solution  Aging – as time progresses increase the priority of the process

  26. Multiple Queue with Dynamic Priority • Priority of processes changes dynamically • Processes are divided into 2 classes • CPU bound – computation intensive • I/O bound – most of time I/O is used • Who should be giving higher priority?

  27. If you know which the process is: • I/O bound uses processor for short time, then I/O operations • So I/O bound will leave the CPU sooner, then CPU bound processes • So give I/O bound higher priority.

  28. Should quantum size be the same for I/O and CPU bound processes?

  29. Vary quantum size w/ dynamic priority • Higher priority will get lower quantum, since it is I/O bound and won’t need it for long Priority Quantum 0 1 Unit Highest priority/lowest quantum 1 2 Units 2 4 Units 3 8 Units Lowest priority/highest quantum

  30. How do you know if the process is I/O or CPU bound?? • It can even change as a processes runs!

  31. We figure it dynamically • If a running process uses it’s quantum, then lower the priority by 1 • If a running process blocks, the raise priority by 1 • Start new process with priority of 0 (highest) • If I/O bound – we did it right • If CPU bound – quantum is small anyway.

  32. Interactive process respond quickly and are usually I/O bound. • So good response time • Fair, since CPU bound process get a longer quantum as they run. • Works well when a process changes from CPU to I/O bound or vise versa.

  33. Multi-Processor Scheduling • Very complex • Depends on the underlying HW structure • Heterogeneous processors: Difficult! • Process may have code compiled for only 1 CPU. • Homogeneous processors with separated ready queues • load balancing problem

  34. Multi-Processor Scheduling (2) • Homogeneous processors with shared ready queue • Symmetric: Processors are self-scheduling • Need to be synchronized to access the shared ready queue to prevent multiple processors trying to load the same process in the queue. • Asymmetric: There is one master processor who distributes next processes to the other processors. • Simpler than symmetric approach

  35. NUMA and CPU Scheduling Note that memory-placement algorithms can also consider affinity

  36. Multi-Processor Scheduling (3) • Processor affinity – process has affinity for processor on which it is currently running • soft affinity • hard affinity • Variations including processor sets

  37. Real Time Systems • An absolute deadline for process execution must be met. • these take the additional parameter of deadline into account in scheduling ie: P1 P2 P3 P4 P5 deadline 10:00 10:05 9:00 11:00 11:10 • Assign CPU to the process that is in the greatest danger of missing it’s deadline.

  38. Hard real-time systems • guaranteed amount of time. Special purpose hardware/software machines • Soft real-time systems • Based on priority based scheduling. • Even kernel must be pre-emptive so a critical process can run. • Resource allocation is “pre-emptive”. If a critical process (A) needs a resourced owned by another process (B), that process B gets process A priority, so it can release the resource. Once processes B released the resource it priority is returned to “normal”.

  39. Windows Scheduling • Windows uses priority-based preemptive scheduling • Highest-priority thread runs next • Thread runs until (1) blocks, (2) uses time slice, (3) preempted by higher-priority thread • Real-time threads can preempt non-real-time • 32-level priority scheme • Variable class is 1-15, real-time class is16-31 • Priority 0 is memory-management thread • Queue for each priority • If no run-able thread, runs idle thread

  40. Windows Priority Classes • Win32 API identifies several priority classes to which a process can belong • REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS • All are variable except REALTIME • A thread within a given priority class has a relative priority • TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL, BELOW_NORMAL, LOWEST, IDLE • Priority class and relative priority combine to give numeric priority • Base priority is NORMAL within the class • If quantum expires, priority lowered, but never below base • If wait occurs, priority boosted depending on what was waited for • Foreground window given 3x priority boost

  41. Windows XP Priorities

  42. Linux Scheduling • Constant order O(1) scheduling time • Preemptive, priority based • Two priority ranges: time-sharing and real-time • Real-time range from 0 to 99 and nice value from 100 to 140 • Map into global priority with numerically lower values indicating higher priority • Higher priority gets larger q • Task run-able as long as time left in time slice (active) • If no time left (expired), not run-able until all other tasks use their slices • All run-able tasks tracked in per-CPU runqueuedata structure • Two priority arrays (active, expired) • Tasks indexed by priority • When no more active, arrays are exchanged

  43. Linux Scheduling (Cont.) • Real-time scheduling according to POSIX.1b • Real-time tasks have static priorities • All other tasks dynamic based on nice value plus or minus 5 • Interactivity of task determines plus or minus • More interactive -> more minus • Priority recalculated when task expired • This exchanging arrays implements adjusted priorities

  44. Priorities and Time-slice length

  45. List of Tasks Indexed According to Priorities

  46. Choosing an Algorithm • There is no “best” algorithm, just better suited. • Need to choose the goals that are more important to the environment.

  47. Memory and Processes • When main memory is insufficient, some processes will be “swapped out” and ready to run. • Short-term schedulers works with medium-term scheduler to bring these process back into memory. • Why not just have the Short-term Scheduler do the work?

  48. Q A &

More Related