Download presentation
Presentation is loading. Please wait.
1
Operating System Concepts
2
Lecture Contents CPU scheduling Dispatcher
Criteria for evaluation of scheduling algorithms performance FCFS scheduling algorithm Shortest job first algorithm Round Robin algorithm Multi-level feedback scheduling Multi-level feedback queues scheduling
3
CPU scheduling Process Life Cycle Multiprogramming Systems
Target of multiprogramming systems is maximum CPU utilization CPU scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them Process Life Cycle Life cycle of a process literally consists of I/O burst and CPU burst I/O burst- (disk read/write, I/O from user, waiting for termination of a child) CPU burst- (using the CPU) Types of programs which are CPU bound, I/O bound .. ?
4
Histogram of CPU-burst Times
5
Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: Dispatch latency – time it takes for the dispatcher to stop one process and start another running
6
Events that lead to invocation of CPU scheduler ..
CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state (e.g., due to interrupt) 3. Switches from waiting to ready (e.g., in completion of I/O) 4. Terminates Scheduling under 1 and 4 is non-preemptive Once CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to “waiting” state All types of schedulers who do this are non-preemptive schedulers All other scheduling is preemptive
7
Scheduling Criteria CPU utilization – keep the CPU as busy as possible (40-90%) Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process, includes: Time spent waiting to get into memory Waiting in the ready queue Executing on CPU Doing I/O Waiting time – amount of time a process has been waiting in the ready queue (this is the only time affected by the scheduling algorithm) 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)
8
Optimization Criteria
Maximum CPU utilization (for all systems) Maximum throughput Minimum turnaround time Minimum waiting time Minimum response time (critical for real-time systems and real-time systems
9
First-Come, First-Served (FCFS) Scheduling
The process that comes first in ready queue is scheduled first Order in which the processes get the CPU depends on time they are submitted in the system Non-preemptive scheduler
10
First-Come, First-Served (FCFS) Scheduling
Process Burst Time P1 24 ms P2 3 ms P3 3 ms Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: Waiting time for P1 = 0 ms; P2 = 24ms; P3 = 27 ms Average waiting time: ( )/3 = 17 ms P1 P2 P3 24 27 30
11
FCFS Scheduling Suppose that the processes arrive in the order
P2 , P3 , P1 . The Gantt chart for the schedule is: Waiting time for P1 = 6 ms; P2 = 0 ms; P3 = 3 ms Average waiting time: ( )/3 = 3 ms Much better than previous case. Convoy effect short process behind long process Performance of FCFS critically depends on order in which processes arrive in system and their next CPU burst P1 P3 P2 6 3 30
12
Shortest-Job-First (SJF) Scheduling
Process with the shortest next CPU burst is scheduled first Two schemes: Non-preemptive – once CPU given to the process it cannot be preempted until completes its CPU burst Preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF)
13
Example of Non-Preemptive SJF
Process Arrival Time Burst Time P P P P SJF (non-preemptive) Waiting time (total time – arrival time - burst time) for P1 = (7-0-7) ms; P2 = (12-2-4) ms; P3 = (8-4-1) ms; P4 = (16-5-4) ms Average waiting time = ( )/4 = 4 P1 P3 P2 7 3 16 P4 8 12
14
Example of Preemptive SJF
Process Arrival Time Burst Time P P P P SJF (preemptive) Waiting time (total time – arrival time - burst time) for P1 = (16-0-7) ms; P2 = (7-2-4) ms; P3 = (5-4-1) ms; P4 = (11-5-4) ms Average waiting time = ( )/4 = 3 P1 P3 P2 4 2 11 P4 5 7 16
15
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 Non-preemptive SJF is a priority scheduling where priority is the predicted next CPU burst time Problem Starvation – low priority processes may never execute (MIT example) Solution Aging – as time progresses increase the priority of the process
16
Example of Priority Algorithm
PROCESS BURST TIME PRIORITY P P P P P P2 P5 P1 P3 P4 AWT = (P1+P2+P3+P4+P5) / 5 = ( ) / 5 = 8.2 ms
17
Round Robin (RR) Designed especially for time-sharing systems
Preemptive algorithm Each process gets a small unit of CPU time (time quantum), usually 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
18
Example of RR with Time Quantum = 20
Process Burst Time P1 53 P2 17 P3 68 P4 24 The Gantt chart is: Typically, higher average turnaround than SJF, but better response time AWT= ( ) + ( ) + ( ) + ( ) / 4 = P1 P2 P3 P4 20 37 57 77 97 117 121 134 154 162 53 17 68 24 33 48 4 13 28 8
19
Time Quantum and Context Switch Time
Performance of RR depends on the size of the time quantum Variable quantum size -> modern OS
20
Multilevel Queue 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
21
Multilevel Queue Scheduling
In this example every queue has full priority over lower-level queues (guaranteed with preemptive scheduling)
22
Multilevel Feedback Queue
The idea – separate processes according to the characteristics of their CPU bursts A process can move between the various queues; aging can be implemented this way
23
Example of Multilevel Feedback Queue
Three queues: Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS Scheduling A new job enters queue Q0 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 Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.
24
Multilevel Feedback Queues
Gets CPU time only if queue 0 is empty Gets CPU time only if queue 1 is empty
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.