710 likes | 726 Views
Outline. Announcements Process Scheduling– continued Preemptive scheduling algorithms SJN Priority scheduling Round robin Multiple-level queue. Please pick up your quiz if you have not done so Please pick up a copy of Homework #2. Announcements. About the first quiz
E N D
Outline • Announcements • Process Scheduling– continued • Preemptive scheduling algorithms • SJN • Priority scheduling • Round robin • Multiple-level queue Please pick up your quiz if you have not done so Please pick up a copy of Homework #2
Announcements • About the first quiz • If the first quiz is not the worst among all of your quizzes, it will replace the worst you have • If the first quiz is the worst among all of your quizzes, it will be ignored in grading COP4610
Announcements – cont. • A few things about the first lab • How to check a directory that is valid or not • Using opendir and closedir • Using access() • Note that your program should accept both command names only and full path commands • Using strchr to check if ‘/’ occurs in my_argv[0] • If yes, execv(my_argv[0], my_argv) • If no, then you need to search through the directories COP4610
Scheduling - review • Scheduling mechanism is the part of the process manager that handles the removal of the running process of CPU and the selection of another process on the basis of a particular strategy • Scheduler chooses one from the ready threads to use the CPU when it is available • Scheduling policy determines when it is time for a thread to be removed from the CPU and which ready thread should be allocated the CPU next COP4610
Preemption or voluntary yield New Process Ready List Scheduler CPU Done job job “Running” job “Ready” Resource Manager Allocate Request job job “Blocked” Resources Process Scheduler - review COP4610
Voluntary CPU Sharing • Each process will voluntarily share the CPU • By calling the scheduler periodically • The simplest approach • Requires a yield instruction to allow the running process to release CPU COP4610
Involuntary CPU Sharing • Periodic involuntary interruption • Through an interrupt from an interval timer device • Which generates an interrupt whenever the timer expires • The scheduler will be called in the interrupt handler • A scheduler that uses involuntary CPU sharing is called a preemptive scheduler COP4610
Programmable Interval Timer COP4610
Ready List Scheduler Ready to run Release Dispatch Release Dispatch Release Process Dispatch Units of time for a time-multiplexed CPU Scheduler as CPU Resource Manager COP4610
From Other States Process Descriptor Ready Process Enqueuer Ready List Context Switcher Dispatcher CPU Running Process The Scheduler Organization COP4610
Dispatcher • Dispatcher module gives control of the CPU to the process selected by the scheduler; this involves: • switching context • switching to user mode • jumping to the proper location in the user program to restart that program COP4610
Diagram of Process State COP4610
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/new to ready. 4. Terminates. COP4610
CPU Scheduler – cont. • Non-preemptive and preemptive scheduling • Scheduling under 1 and 4 is non-preemptive • A process runs for as long as it likes • In other words, non-preemptive scheduling algorithms allow any process/thread to run to “completion” once it has been allocated to the processor • All other scheduling is preemptive • May preempt the CPU before a process finishes its current CPU burst COP4610
Working Process Model and Metrics • P will be a set of processes, p0, p1, ..., pn-1 • S(pi) is the state of pi • t(pi), the service time • The amount of time pi needs to be in the running state before it is completed • W (pi), the wait time • The time pi spends in the ready state before its first transition to the running state • TTRnd(pi), turnaround time • The amount of time between the moment pi first enters the ready state and the moment the process exists the running state for the last time COP4610
Scheduling Criteria • 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 • 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 (for time-sharing environment) COP4610
0 350 475 900 1200 1275 p0 p1 p2 p3 p4 First-Come-First-Served • First-Come-First-Served • Assigns priority to processes in the order in which they request the processor COP4610
Predicting Wait Time in FCFS • In FCFS, when a process arrives, all in ready list will be processed before this job • Let m be the service rate • Let L be the ready list length • Wavg(p) = L*1/m + 0.5* 1/m = L/m+1/(2m) • Compare predicted wait with actual in earlier examples COP4610
Shortest-Job-Next Scheduling • Associate with each process the length of its next CPU burst. • Use these lengths to schedule the process with the shortest time. • SJN is optimal • gives minimum average waiting time for a given set of processes. COP4610
0 75 200 450 800 1275 p4 p1 p3 p0 p2 Nonpreemptive SJN COP4610
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. COP4610
Examples of Exponential Averaging • =0 • n+1 = n • Recent history does not count. • =1 • n+1 = tn • Only the actual last CPU burst counts. • If we expand the formula, we get: n+1 = tn+(1 - ) tn -1 + … +(1 - )j tn -1 + … +(1 - )n=1 tn 0 • Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor. COP4610
Priority Scheduling • In priority scheduling, processes/threads are allocated to the CPU based on the basis of an externally assigned priority • A commonly used convention is that lower numbers have higher priority • SJN is a priority scheduling where priority is the predicted next CPU burst time. • FCFS is a priority scheduling where priority is the arrival time COP4610
Priority Scheduling – cont. • Starvation problem • low priority processes may never execute. • Solution through aging • as time progresses increase the priority of the process COP4610
i t(pi) Deadline 0 350 575 1 125 550 2 475 1050 3 250 (none) 4 75 200 • Allocates service by deadline • May not be feasible 200 550 575 1050 0 1275 p1 p4 p0 p2 p3 p4 p1 p0 p2 p3 p4 p0 p1 p2 p3 Deadline Scheduling COP4610
Real-Time Scheduling • Hard real-time systems – required to complete a critical task within a guaranteed amount of time. • Soft real-time computing – requires that critical processes receive priority over less fortunate ones. COP4610
Preemption or voluntary yield New Process Ready List Scheduler CPU Done Preemptive Schedulers • Highest priority process is guaranteed to be running at all times • Or at least at the beginning of a time slice • Dominant form of contemporary scheduling • But complex to build & analyze COP4610
Preemptive Shortest Job Next • Also called the shortest remaining job next • When a new process arrives, its next CPU burst is compared to the remaining time of the running process • If the new arriver’s time is shorter, it will preempt the CPU from the current running process COP4610
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 • Average time spent in ready queue = (9 + 1 + 0 +2)/4 = 3 COP4610
P1 P3 P2 P4 0 3 7 8 12 16 Comparison of Non-Preemptive and Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJN (non-preemptive) • Average time spent in ready queue • (0 + 6 + 3 + 7)/4 = 4 COP4610
Round Robin (RR) • Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. • 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. COP4610
Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 50 p0 W(p0) = 0 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 p0 p1 W(p0) = 0 W(p1) = 50 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 p0 p1 p2 W(p0) = 0 W(p1) = 50 W(p2) = 100 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 p0 p1 p2 p3 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 p0 p1 p2 p3 p4 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 p0 p1 p2 p3 p4 p0 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 TTRnd(p4) = 475 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 TTRnd(p1) = 550 TTRnd(p4) = 475 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 p0 p2 p3 p0 p2 p3 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 TTRnd(p1) = 550 TTRnd(p3) = 950 TTRnd(p4) = 475 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 1050 p0 p2 p3 p0 p2 p3 p0 p2 p0 TTRnd(p0) = 1100 TTRnd(p1) = 550 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 COP4610
Round Robin (TQ=50) – cont. i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 1050 1150 1250 1275 p0 p2 p3 p0 p2 p3 p0 p2 p0 p2 p2 p2 p2 TTRnd(p0) = 1100 TTRnd(p1) = 550 TTRnd(p2) = 1275 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 COP4610
Equitable • Most widely-used • Fits naturally with interval timer i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 1050 1150 1250 1275 p0 p2 p3 p0 p2 p3 p0 p2 p0 p2 p2 p2 p2 TTRnd(p0) = 1100 TTRnd(p1) = 550 TTRnd(p2) = 1275 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 TTRnd_avg = (1100+550+1275+950+475)/5 = 4350/5 = 870 Wavg = (0+50+100+150+200)/5 = 500/5 = 100 Round Robin (TQ=50) – cont. COP4610
Round Robin – cont. • Performance • q large FIFO • q small q must be large with respect to context switch, otherwise overhead is too high. COP4610
How a Smaller Time Quantum Increases Context Switches COP4610
Round-robin Time Quantum COP4610
Right Operand Status Registers Left Operand R1 R2 . . . Rn Functional Unit ALU Result PC IR Ctl Unit Process/Thread Context COP4610