Operating Systems Processes Scheduling Basic Concepts Maximum CPU
Operating Systems Processes Scheduling
Basic Concepts • Maximum CPU utilization obtained with multiprogramming • CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait • CPU burst followed by I/O burst and then CPU burst and then I/O burst and finally terminates with a CPU burst. • CPU burst distribution is of main concern 2
Histogram of CPU-burst Times • CPU bursts vary from process to process, and from computer to computer, but an extensive study shows frequency patterns similar to that shown in Figure. • With a large no. of short CPU bursts and a small no. of long CPU bursts 3 • An I/O bound program typically has many short CPU bursts whereas a CPU bound program might have few long CPU bursts. • This distribution can be important in the selection of an appropriate CPU scheduling algorithm.
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: i. iii. iv. Switches from running to waiting state (I/O request) Switches from running to ready state (interrupt) Switches from waiting to ready (completion of I/O) Terminates • Scheduling under i and iv is nonpreemptive • All other scheduling is preemptive 4
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 5
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 (from time of submission to time of completion) • 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) 6
Scheduling Algorithm Optimization Criteria • Maximum CPU utilization • Maximum throughput • Minimum turnaround time • Minimum waiting time • Minimum response time 7
First-Come, First-Served (FCFS) Scheduling • Runs the processes in the order they arrive at the shortterm scheduler, i. e. processes are executed on first come, first serve basis. • Removes a process from the processor only if it blocks (i. e. , goes into the Wait state) or terminates • Wonderful for long processes when they finally get on • Terrible for short processes if they are behind a long process 8 • Easy to understand implement. • Poor in performance as average wait time is high.
First-Come, First-Served (FCFS) Scheduling Process Burst Time P 1 24 P 2 3 P 3 3 • Suppose that the processes arrive in the order: P 1 , P 2 , P 3 • The Gantt Chart for the schedule is: 0 P 1 24 P 2 27 P 3 30 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 9
FCFS Scheduling (Cont) Suppose that the processes arrive in the order P 2 , P 3 , P 1 • The Gantt chart for the schedule is: P 2 0 • • 10 P 3 3 P 1 6 30 Waiting time for P 1 = 6; P 2 = 0; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process (one CPU-bound and more I/O-bound implies lower CPU and I/O utilization).
Shortest-Job-First (SJF) Scheduling • Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time • SJF is optimal – gives minimum average waiting time for a given set of processes – The difficulty is knowing the length of the next CPU request • Impossible to implement 11
Example of SJF Process Arrival Time P 1 P 2 P 3 P 4 • SJF scheduling chart P 4 0 0. 0 2. 0 4. 0 5. 0 6 8 7 3 P 1 3 Burst Time 9 P 2 16 • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 12 24
Determining Length of Next CPU Burst § Can only estimate the length – should be similar to the previous one § Then pick process with shortest predicted next CPU burst § Can be done by using the length of previous CPU bursts, using exponential averaging § Commonly, α set to ½ § Preemptive version called shortest-remaining-time-first 13
Prediction of the Length of the Next CPU Burst 14
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 -j + … +(1 - )n +1 0 • Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor 15
Example of Shortest-remainingtime-first § Now we add the concepts of varying arrival times and preemption to the analysis Process. Aarri Arrival Time. TBurst Time P 1 0 8 P 2 1 4 P 3 2 9 P 4 3 5 § Preemptive SJF Gantt Chart 16 § Average waiting time = [(10 -1)+(17 -2)+5 -3)]/4 = 26/4 = 6. 5 msec
Priority Scheduling • Each process is assigned a priority. Process with highest priority is to be executed first and so on. • Processes with same priority are executed on first come first serve basis. • Priority can be decided based on memory requirements, time requirements or any other resource requirement. 17
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 • SJF is a priority scheduling where priority is the predicted next CPU burst time • Problem Starvation – low priority processes may never execute • Solution Aging – as time progresses increase the priority of the process 18
Example • Process P 1 P 2 P 3 P 4 P 5 • Gantt Chart: P 2 P 5 0 1 6 Burst Time 10 1 2 1 5 • Average waiting time: 8. 2 ms 19 Priority 3 1 3 4 2 P 1 P 3 16 P 4 18 19
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. § Performance § q large FIFO § q small q must be large with respect to context switch, otherwise overhead is too high 20
Example of RR with Time Quantum = 4 Process P 1 P 2 P 3 • The Gantt chart is: Burst Time 24 3 3 • Typically, higher average turnaround than SJF, but better response • q should be large compared to context switch time • q usually 10 ms to 100 ms, context switch < 10 usec 21
Time Quantum and Context Switch Time 22
Turnaround Time Varies With The Time Quantum 80% of CPU bursts should be shorter than q 23
Scheduling Policies Round Robin: very common base policy. • Run each process for its time slice (scheduling quantum) • After each time slice, move the running thread to the back of the queue. • Selecting a time slice: – Too large - waiting time su�ers, degenerates to FCFS if processes are never preempted. – Too small - throughput su�ers because too much time is spent context switching. – Balance the two by selecting a time slice where context switching is roughly 1% of the time slice. • A typical time slice today is between 10 -100 milliseconds, with a context switch time of 0. 1 to 1 millisecond. – Max Linux time slice is 3, 200 ms, Why? 24 • Is round robin more fair than FCFS? A)Yes B)No
Properties of RR • Advantages: simple, low overhead, works for interactive systems • Disadvantages: if quantum is too small, too much time wasted in context switching; if too large (i. e. longer than mean CPU burst), approaches FCFS. • Typical value: 20 – 40 msec • Rule of thumb: Choose quantum so that large majority (80 – 90%) of jobs finish CPU burst in one quantum • RR makes the assumption that all processes are equally important 25 2 5
Deterministic Modelling • Take a given workload and calculate the performance of each scheduling algorithm: – FCFS, SJF, and RR (quantum = 10 ms) 26
Example • At time 0. • Process P 1 P 2 P 3 P 4 P 5 27 Burst Time 10 29 3 7 12
Gantt Charts and Times • 1. FCFS: P 1 0 P 2 10 P 3 39 P 4 42 • Average waiting time: (0 + 10 + 39 + 42 + 49)/5 = 28 ms 28 P 5 49 61
• 2. Non-preemptive SJF: P 3 0 P 4 3 P 1 10 P 5 20 P 2 32 • Average waiting time: (10 + 32 + 0 + 3 + 20)/5 = 13 ms 29 61
• 3. RR: P 1 • 0 P 2 10 P 3 P 4 20 23 P 5 30 • Average waiting time: (0 + 32 + 20 + 23 + 40)/5 = 23 ms 30 P 2 40 P 5 P 2 50 52 61
Multilevel Queue 31 • Ready queue is partitioned into separate queues: foreground (interactive) background (batch) • Each queue has its own scheduling algorithm – foreground – RR – background – FCFS • Scheduling must be done between the queues – Fixed priority scheduling; (i. e. , serve all from foreground then from background). Possibility of starvation. – Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i. e. , 80% to foreground in RR – 20% to background in FCFS
Multilevel Queue Scheduling 32
Multilevel Feedback Queue • A process can move between the various queues; aging can be implemented this way • Multilevel-feedback-queue scheduler defined by the following parameters: – number of queues – scheduling algorithms for each queue – method used to determine when to upgrade a process – method used to determine when to demote a process – method used to determine which queue a process will enter when that process needs service 33
Example of Multilevel Feedback Queue • Three queues: – Q 0 – RR with time quantum 8 milliseconds – Q 1 – RR time quantum 16 milliseconds – Q 2 – FCFS • Scheduling – A new job enters queue Q 0 which is served FCFS • When it gains CPU, job receives 8 milliseconds • If it does not finish in 8 milliseconds, job is moved to queue Q 1 – At Q 1 job is again served FCFS and receives 16 additional milliseconds • If it still does not complete, it is preempted and moved to queue Q 2 34
Thread Scheduling • Distinction between user-level and kernel-level threads • Many-to-one and many-to-many models, thread library schedules user-level threads to run on LWP – Known as process-contention scope (PCS) since scheduling competition is within the process • Kernel thread scheduled onto available CPU is systemcontention scope (SCS) – competition among all threads in system 35
- Slides: 35