Transcript Lecture 15

Operating Systems
Lecture 15
Scheduling
Read Ch 6.1 - 6.3
Operating System Concepts
6.1
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Recall Schedulers
 Short term scheduler: Select a process in the ready






queue for CPU allocation.
Medium term scheduler (swapper): determine which
process to swap in/out of disk to/from memory.
Long term scheduler: Determine which processes are
admitted into the system.
Schedulers determine which processes will wait and
which will progress.
Schedulers affect the performance of the system.
Scheduling is a fundamental O.S. function.
We will discuss short-term schedulers (CPU allocation).
Operating System Concepts
6.2
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
CPU-I/O Burst cycle
 Process execution consists of a cycles of CPU execution








and I/O waiting.
A process begins with a CPU burst.
When a process waits for I/O, it is called an I/O burst.
A process alternates between CPU bursts and I/O bursts.
Eventually a CPU burst will end with process termination.
The length of CPU bursts vary by process and computer.
Typically there are many short bursts and a few long
bursts.
Question: What length of CPU bursts would an I/O bound
process have?
Question: What length of CPU bursts would a CPU bound
process have?
Operating System Concepts
6.3
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Alternating Sequence of CPU And I/O Bursts
Operating System Concepts
6.4
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Histogram of CPU-burst Times
Operating System Concepts
6.5
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Preemptive vs. Non-preemptive
scheduling
 Non-preemptive scheduling:
 Each process completes its full CPU burst cycle before the
next process is scheduled.
 No time slicing or CPU stealing occurs.
 Once a process has control of the CPU, it keeps control until
it gives it up (e.g. to wait for I/O or to terminate).
 Works OK for batch processing systems, but not suitable for
time sharing systems.
 Preemptive scheduling:
 A process may be interrupted during a CPU burst and
another process scheduled. (E.g. if the time slice of the first
process expires).
 More expensive implementation due to process switching.
 Used in all time sharing and real time systems.
Operating System Concepts
6.6
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Costs of Preemptive Scheduling
 Preemptive scheduling leads to some problems that the OS must
deal with:
 Problem 1: inconsistent data:
 Suppose process 1 is updating data when preempted by process 2.
 Process 2 may then try to read the data, which is in an inconsistent
state.
 The OS needs mechanisms to coordinate shared data.
 Problem 2: Kernel preemption:
 Suppose the kernel is preempted while updating data (e.g. I/O
queues) used by other kernel functions. This could lead to chaos.
 UNIX solution: Wait for the system call to complete or have an I/O
block take place if in kernel mode.
 Problem with UNIX solution: Not good for real time computing.
Operating System Concepts
6.7
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Dispatcher
 Process scheduling determines the order in which
processes execute.
 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.
Operating System Concepts
6.8
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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)
Operating System Concepts
6.9
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Optimization Criteria
 The scheduling criteria are optimization problems. We
would like to maximize or minimize each.
 Question: Maximize or Minimize?
 CPU utilization:
 throughput:
 turnaround time:
 waiting time:
 response time:
 Can all criteria be optimized simultaneously?
 Usually try to optimize average times (although
sometimes optimize minimum or maximum)
Operating System Concepts
6.10
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
First-Come, First-Served (FCFS) Scheduling
Process that requests the CPU first is allocated the CPU first.
Easily managed with a FIFO queue.
Often the average waiting time is long.
Process
Burst Time
P1
24
P2
3
P3
3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1
P2
0
 Waiting time for P1 =
24
; P2 =
P3
27
30
; P3 =
 Average waiting time:
Operating System Concepts
6.11
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1 .
 The Gantt chart for the schedule is:
P2
0
P3
3
P1
6
 Waiting time for P1 =
30
; P2 =
; P3 =
 Average waiting time:
 Much better than previous case.
 Convoy effect: short processes line up behind long
process.
 FCFS is not good for time-sharing systems. (Nonpreemptive).
Operating System Concepts
6.12
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
 Two schemes:
 nonpreemptive – 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).
 SJF is optimal – gives minimum average waiting time for
a given set of processes.
Operating System Concepts
6.13
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Example of Non-Preemptive SJF
Process
Arrival Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
 SJF (non-preemptive)
P1
0
3
P3
7
Burst Time
7
4
1
4
P2
8
P4
12
16
 Average waiting time =
Operating System Concepts
6.14
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Example of Preemptive SJF
Process
P1
P2
P3
P4
 SJF (preemptive)
P1
0
P2
2
P3
4
Arrival Time
0.0
2.0
4.0
5.0
P2
5
Burst Time
7
4
1
4
P4
7
P1
11
16
 Average waiting time =
Operating System Concepts
6.15
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
1. tn  actual lenght of nthCPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :
 n 1   t n  1   n .

Operating System Concepts
6.16
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Prediction of the Length of the Next CPU Burst
Operating System Concepts
6.17
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
6.18
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005