330 likes | 598 Views
Lecturer 5: Process Scheduling. Process Scheduling Criteria & Objectives Types of Scheduling Long term Medium term Short term CPU Scheduling Algorithms First-Come, First-Served (FCFS) Shortest Job First (SJF) Priority (PRI) Round-Robin (RR). CPU Scheduling.
E N D
Lecturer 5: Process Scheduling • Process Scheduling • Criteria & Objectives • Types of Scheduling • Long term • Medium term • Short term • CPU Scheduling Algorithms • First-Come, First-Served (FCFS) • Shortest Job First (SJF) • Priority (PRI) • Round-Robin (RR)
CPU Scheduling Scheduling: deciding which process are given access to resources from moment to moment.
Chapter 5: CPU Scheduling • Always want to have CPU (or CPU’s) working • Usually many processes in ready queue • Ready to run on CPU • Focus on a single CPU here • Need strategies for • Allocating CPU time • Selecting next process to run • Deciding what happens after a process completes a system call, or completes I/O • Short-term scheduling • Must not take much CPU time to do the scheduling
CPU & I/O Bursts • Process execution is nothing but a sequence of CPU & I/O bursts • Processes alternate between these two states
CPU Burst & I/O Burst Alternation (Fig. 5.1) In general, most programs have short CPU bursts What happens when the I/O burst occurs? What is the process state at that time?
Types of Scheduling • Long-term scheduling • the decision to add to pool of processes to be executed • Mid-term scheduling • the decision to add to the number of processes that are partially or fully in memory • Short-term scheduling • decision as to which available process will be executed • I/O scheduling • decision as to which process’s pending request shall be handled by an available I/O device
Short termCPU 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 non-preemptive • All other scheduling is preemptive
Preemptive & Non-preemptive Scheduling • SO what is Preemptive & Non-preemptive scheduling? • Non-preemptive • Once CPU is allocated to a process, it holds it till it • Terminates (exits) • Blocks itself to wait for I/O • Requests some OS service
Preemptive & Non-preemptive Scheduling • Preemptive • Currently running process may be interrupted and moved to the ready state by the OS • Windows 95 introduced preemptive scheduling
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 (IP) • Dispatch latency – time it takes for the dispatcher to stop one process and start another running
For schedulingCriteria & Objectives • CPU utilization – keep the CPU as busy as possible • Throughput – # of processes that complete their execution per time unit • Turnaround time – amount of time to execute a particular process (total time spent on the system) • Waiting time – amount of time a process has been waiting in the ready queue • Response time – amount of time it takes from when a request was submitted until the first response is produced, not output
Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time
CPU Scheduling Algorithms • First-Come, First-Served (FCFS) • Complete the jobs in order of arrival • Shortest Job First (SJF) • Complete the job with shortest next CPU burst • Priority (PRI) • Processes have a priority • Allocate CPU to process with highest priority • Round-Robin (RR) • Each process gets a small unit of time on CPU (time quantum or time slice)
FCFS: First-Come First-Served • Ready queue data structure: a FIFO queue • Assuming we have (at least) a multiprogrammed system • Example 1 • Draw Gantt chart • Compute the average waiting time for processes with the following next CPU burst times and ready queue order: • P1: 20 • P2: 12 • P3: 8 • P4: 16 • P5: 4 • Waiting time: • Time period spent in the ready queue (assume processes terminate) • Also calculate average waiting time across processes • Assume 0 context switch time E.g., time units are milliseconds CPU burst times
FCFS: First-Come First-Served P1 P2 P3 P4 P5 0 20 32 40 56 60 Solution: Gantt Chart Method • Waiting times? P1: 0 P2: 20 P3: 32 P4: 40 P5: 56 • Average wait time: 148/5 = 29.6
FCFS: First-Come First-Served Advantage: Relatively simple algorithm Disadvantage: long waiting times
Scheduling Algorithms: First-Come, First-Served (FCFS) • Example 2: Three processes arrive in order P1, P2, P3. • P1 burst time: 24 • P2 burst time: 3 • P3 burst time: 3 • Waiting Time • P1: 0 • P2: 24 • P3: 27 • Completion Time: • P1: 24 • P2: 27 • P3: 30 • Average Waiting Time: (0+24+27)/3 = 17 • Average Completion Time: (24+27+30)/3 = 27 P1 P2 P3 27 30 24 0
Scheduling Algorithms: First-Come, First-Served (FCFS) • What if their order had been P2, P3, P1? • P1 burst time: 24 • P2 burst time: 3 • P3 burst time: 3
Scheduling Algorithms: First-Come, First-Served (FCFS) • What if their order had been P2, P3, P1? • P1 burst time: 24 • P2 burst time: 3 • P3 burst time: 3 • Waiting Time • P1: 0 • P2: 3 • P3: 6 • Completion Time: • P1: 3 • P2: 6 • P3: 30 • Average Waiting Time: (0+3+6)/3 = 3 (compared to 17) • Average Completion Time: (3+6+30)/3 = 13 (compared to 27) P2 P3 P1 6 30 3 0
SJF • Provably shortest average wait time • BUT: What do we need to actually implement this?
P5 P3 P2 P4 P1 0 4 12 24 40 60 SJF Solution • Waiting times (how long did process wait before being scheduled on the CPU?): P1: 40 P2: 12 P3: 4 P4: 24 P5: 0 • Average wait time: 16 (Recall: FCFS scheduling had average wait time of 29.6)
Priority Scheduling • Have to decide on a numbering scheme • 0 can be highest or lowest
Starvation Problem • Priority scheduling algorithms can suffer from starvation (indefinite waiting for CPU access) • In a heavily loaded system, a steady stream of higher-priority processes can result in a low priority process never receiving CPU time • I.e., it can starve for CPU time • One solution: aging • Gradually increasing the priority of a process that waits for a long time
Which CPU Scheduling Algorithms Can be Preemptive? • FCFS (First-come, First-Served) • Non-preemptive • SJF (Shortest Job First) • Can be either • Choice when a new (shorter) job arrives • Can preempt current job or not • Priority • Can be either • Choice when a processes priority changes or when a higher priority process arrives
RR (Round Robin) Scheduling • Used in time-sharing or multi-tasking systems • typical kind of scheduling algorithm in a contemporary general purpose operating system • Method • Give each process a unit of time (time slice, quantum) of execution on CPU • Then move to next process in ready queue • Continue until all processes completed • Necessarily preemptive • Requires use of timer interrupt • Time quantum typically between 10 and 100 milliseconds • Linux default appears to be 100ms
RR (Round Robin) Scheduling: Example • CPU job burst times & order in queue • P1: 20 • P2: 12 • P3: 8 • P4: 16 • P5: 4 • Draw Gantt chart, and compute average wait time • Time quantum of 4 • Like our previous examples, assume 0 context switch time
completes completes completes completes completes P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 Solution • Waiting times: P1: 60 - 20 = 40 P2: 44 - 12 = 32 P3: 32 - 8 = 24 P4: 56 - 16 = 40 P5: 20 - 4 = 16 • Average wait time: 30.4
P1 P2 P3 P1 P1 P1 P1 P1 0 10 14 18 22 26 30 4 7 Example 2of RR with Time Quantum = 4 ProcessBurst Time P1 24 P2 3 P3 3 The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1 0 10 14 18 22 26 30 4 7 Example of RR with Time Quantum = 4 ProcessBurst Time P1 24 P2 3 P3 3 • Waiting Time: • P1: (10-4) = 6 • P2: (4-0) = 4 • P3: (7-0) = 7 • Completion Time: • P1: 30 • P2: 7 • P3: 10 • Average Waiting Time: (6 + 4 + 7)/3= 5.67 • Average Completion Time: (30+7+10)/3=15.67
Example 3 of RR with Time Quantum = 20 • Waiting Time: • P1: (68-20)+(112-88) = 72 • P2: (20-0) = 20 • P3: (28-0)+(88-48)+(125-108) = 85 • P4: (48-0)+(108-68) = 88 • Completion Time: • P1: 125 • P2: 28 • P3: 153 • P4: 112 • Average Waiting Time: (72+20+85+88)/4 = 66.25 • Average Completion Time: (125+28+153+112)/4 = 104.5 A process can finish before the time quantum expires, and release the CPU.
Performance Characteristics of Scheduling Algorithms • Different scheduling algorithms will have different performance characteristics • RR (Round Robin) • Average waiting time often high • Good average response time • Important for interactive or timesharing systems • SJF • Best average waiting time • Some overhead with respect to estimates of CPU burst length & ordering ready ‘queue’