5. Process Scheduling 1 CPU-I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait. Process alternates between those two states. 2 Alternating sequence of CPU and I/O bursts 3 Usually large number of short CPU bursts and a small number of long CPU bursts. I/O-bound process: typically many short CPU bursts. CPU-bound process: a few long CPU bursts. 4 Histogram of CPU-burst durations of a sample process 5 CPU (short-term) scheduler Choose a process from the ready queue to execute on a CPU. The ready queue is not necessarily first-in first-out (FIFO). Records in the queues are generally the PCBs of the processes. Can be either nonpreemptive or preemptive. 6 Nonpreemptive scheduling Invoke CPU scheduler only when it is absolutely needed. 1. When a running process terminates, 2. When a running process changes to waiting state (waiting for I/O or an event). Old operating system like windows 3.11 use nonpreemptive scheduling. More like cooperative scheduling Processes must be willing to give up CPU by itself. 7 More on nonpreemptive scheduling Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU. Used by Windows 3.x, previous versions of Mac OS. For certain hardware platforms that lacks some special hardware (e.g. a timer), only nonpreemptive scheduling can be used. 8 Some issues from nonpreemtive If a process crashes If a process runs indefinitely (infinite loop) Responsiveness 9 Preemptive scheduling In addition to the two cases above, also invoke CPU scheduler when it is not absolutely needed, e.g. ◦ When a new process enters the ready queue. ◦ When the currently running process runs for certain amount of time. ◦ Process maybe forcefully/unwillingly swap out of CPU right. 10 More on preemptive scheduling Used by Windows 95 and newer, Mac OS X. OS needs to make sure that a process is not preempted while it is updating shared data. ◦ => Overhead in handling this. 11 Preemption also affects the design of the kernel. ◦ Example: a process is preempted while the system is modifying the I/O queue for the process. So needs mechanism to deal with such problem. 12 Dispatcher The dispatcher is the module that gives control of the CPU to the process selected by the short-term scheduler. The dispatcher must be able to do the following: ◦ Switching context ◦ Switching to user mode ◦ Jumping to the proper location in the user program to resume that program. 13 The dispatcher should be as fast as possible because it is invoked often (i.e. every process switch). The time it takes for the dispatcher to stop one process and start another running is known as the dispatch latency. 14 Scheduling criteria There are different criteria in choosing the “best” process by the CPU scheduler. There are also many different CPU scheduling algorithms. 15 Some of the criteria for good scheduling algorithms are: CPU utilization. ◦ We want to keep the CPU as busy as possible. ◦ Can range from 0 to 100 percent (conceptually). 16 Throughput. ◦ Throughput is the number of processes that are completed per time unit. 17 Turnaround time. ◦ How long does it take to execute a process? From the process’s point-of-view, it should be short 18 The turnaround time is the interval from the time of submission of a process to the time of completion. This includes the time ◦ ◦ ◦ ◦ Spent waiting to get into memory, Waiting in the ready queue, Executing on a CPU, and Doing I/O. 19 Waiting time. ◦ Waiting time is the sum of the periods (time) spent waiting in the ready queue. 20 Response time. ◦ In an interactive system, turnaround time may not be the best criterion. ◦ E.g. users may prefer to get one output after 5 minutes then the rest after another 5 minutes than to get all output after 9 minutes. 21 Response time is the time from the submission of a request until the first response is produced. This is the time it takes to start responding, not the time it takes to output all of the response. The turnaround time is generally limited by the speed of the output device. 22 Usually, it is desirable to maximize ◦ CPU utilization and ◦ Throughput And to minimize ◦ Turnaround time, ◦ Waiting time, and ◦ Response time. 23 In most cases, we optimize the average measure (over all processes). ◦ E.g. Maximize the average CPU utilization over all processes in the system. Minimize the average turnaround time. 24 Under some circumstances, however, better to optimize the minimum or maximum values rather than the average. ◦ For example, to guarantee that all users get good service. -> minimize the maximum response time. 25 For interactive system, perhaps more important to minimize the variance in the response time than to minimize the average response time. Reason: a system with reasonable and predictable response time may be considered more desirable than one that is faster but highly variable. However, minimizing variance is difficult. 26 CPU/IO Queues Scheduling algorithms can be used in different parts of OS/other software Printing queue Balancing (while maximize the utilization) ◦ CPU ◦ I/O 27