Title: Operating System Concepts
1Operating System Concepts
- Ku-Yaw Chang
- canseco_at_mail.dyu.edu.tw
- Assistant Professor, Department of Computer
Science and Information Engineering - Da-Yeh University
2Chapter 6 CPU Scheduling
- CPU scheduling
- The basis of multiprogrammed OSs
- Make the computer more production
- Switch the CPU among processes
- We introduce
- The basic scheduling concepts
- Several different CPU-scheduling algorithms
- Selecting an algorithm for a particular system
3Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
46.1 Basic Concepts
- Several processes are kept in memory at one time
- When a process has to wait, the OS takes the CPU
away from that process, and gives the CPU to
another process. - Almost all computer resources are scheduled
before use.
56.1.1 CPU-I/O Burst Cycle
- Observed property of processes
- Process execution consists of a cycle of
- CPU execution
- I/O wait
- Process execution begins with a CPU burst.
- That is followed by an I/O burst, then another
CPU burst, then another I/O burst, and so on.
6Alternating sequence ofCPU and I/O bursts
7Histogram of CPU-burst times
86.1.1 CPU-I/O Burst Cycle
- Measure CPU bursts
- An I/O bound program
- Many short CPU bursts
- A CPU bound program
- A few very long CPU bursts
- Help select an appropriate CPU-scheduling
algorithm
96.1.2 CPU Scheduler
- Whenever the CPU becomes idle
- Select one of the processes in the ready queue to
be executed - Carried out by the short-term scheduler, also
called CPU scheduler - A ready queue may be implemented as
- A FIFO queue
- A priority queue
- A tree
- An unordered linked list
106.1.3 Preemptive Scheduling
- CPU scheduling decisions take place when a
process - Switches from running to waiting state
- Switches from running to ready state
- Switches from waiting to ready
- Terminates
- Scheduling only under 1 and 4 is nonpreemptive
- Otherwise is preemptive
- Incur a cost
116.1.4 Dispatcher
- Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler - 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.
12Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
136.2 Scheduling Criteria
- CPU utilization (max)
- keep the CPU as busy as possible
- Throughput (max)
- number of processes that complete their execution
per time unit - Turnaround time (min)
- amount of time to execute a particular process
- Waiting time (min)
- amount of time a process has been waiting in the
ready queue - Response time (min)
- amount of time it takes from when a request was
submitted until the first response is produced,
not output (for time-sharing environment)
14Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
156.3 Scheduling Algorithms
- Dealing the problem of deciding which of the
processes in the ready queue to be allocated the
CPU - First-Come, First-Served Scheduling
- Shortest-Job-First Scheduling
- Priority Scheduling
- Round-Robin Scheduling
- Multilevel Queue Scheduling
- Multilevel Feedback Queue Scheduling
166.3.1 First-Come, First-Served Scheduling
- The process that requests the CPU first is
allocated the CPU first. - The simplest CPU-scheduling algorithm
- Implementation with a FIFO queue
- Add to the tail of the queue
- Remove from the head to the queue
- The code is simple to write and understand
- Average waiting time is often quite long.
176.3.1 First-Come, First-Served Scheduling
- 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 - Waiting time for P1 0 P2 24 P3 27
- Average waiting time (0 24 27)/3 17
186.3.1 First-Come, First-Served Scheduling
- Suppose that the processes arrive in the
order - P2 , P3 , P1
- The Gantt chart for the schedule is
- Waiting time for P1 6 P2 0 P3 3
- Average waiting time (6 0 3)/3 3
- Much better than previous case.
196.3.1 First-Come, First-Served Scheduling
- Convoy effect
- All the other processes wait for one big process
to get off the CPU - Results in lower CPU and device utilization
- If the shorter processes were allowed to go first
- FCFS is non-preemptive
- Once the CPU has been allocated to a process,
that process keeps the CPU until it releases the
CPU. - Particular troublesome in time-sharing system
- Each user needs to get a share of the CPU at
regular intervals
206.3.2 Shortest-Job-First Scheduling
- When the CPU is available, it is assigned to the
process that has the smallest next CPU burst. - Associate each process with the length of the
latters next CPU burst - Not its total length
- Another term shortest next CPU burst
- FCFS scheduling is used to break the tie
- Provably optimal
- Minimum average waiting time for a given set of
processes - Real difficulty
- Knowing the length of the next CPU burst
- Used frequently in long-term scheduling
216.3.2 Shortest-Job-First Scheduling
- Process Burst Time
- P1 6
- P2 8
- P3 7
- P4 3
- The Gantt Chart for the schedule is
- Waiting time for P1 3 P2 16 P3 9 P4
0 - Average waiting time (3 16 9 0) / 4 7
- Using FCFS scheme ( 0 6 14 21) / 4 10.25
226.3.2 Shortest-Job-First Scheduling
- To approximate SJF scheduling
- To predict its value
- Use the length of previous CPU bursts
- exponential average
23Prediction of the length of the next CPU burst
24Examples ofExponential Averaging
- ? 0
- ?n1 ?n
- Recent history does not count.
- ? 1
- ?n1 tn
- Only the actual last CPU burst counts.
- If we expand the formula, we get
- ?n1 ? tn(1 - ?) ? tn -1
- (1 - ? )j ? tn -1
- (1 - ? )n1 tn ?0
- Since both ? and (1 - ?) are less than or equal
to 1, each successive term has less weight than
its predecessor.
256.3.2 Shortest-Job-First Scheduling
- Two schemes
- Nonpreemptive
- Allow the current running process to finish its
CPU burst - Preemptive
- If a new process arrives with CPU burst length
less than remaining time of current executing
process, preempt. - Called as Shortest-Remaining-Time-First (SRTF).
26Example of Preemptive SJF
- Process Arrival Time Burst Time
- P1 0 8
- P2 1 4
- P3 2 9
- P4 3 5
- Preemptive SJF
- Average waiting time ( (10-1) (1-1) (17-2)
(5-3)) / 4 6.5
276.3.3 Priority Scheduling
- The CPU is allocated to the process with the
highest priority. - A priority is associated with each process
- Fixed range of number, such as 0 to 7
- We use low numbers to represent high priority
- Equal-priority processes are scheduled in FCFS
order - SJF is a special case of the general
priority-scheduling algorithm - The priority is the inverse of the predicted next
CPU burst
28Example of priority scheduling
- Process Burst Time Priority
- P1 10 3
- P2 1 1
- P3 2 4
- P4 1 5
- P5 5 2
- Priority scheduling
- Average waiting time ( 6 0 16 18 1 ) /
5 8.2
296.3.3 Priority Scheduling
- Priorities can be defined
- Internally
- Use some measurable quantity
- Time limits, memory requirements
- Externally
- Set by criteria external to OS
- importance, political factors
- Priority scheduling can be
- Preemptive
- Nonpreemptive
- Major problem
- Indefinite blocking or starvation
- Solution aging
- Gradually increase the priority of processes that
wait for a long time
306.3.4 Round-Robin Scheduling
- A small unit of time, called a time quantum ( or
time slice) is defined. - Generally from 10 to 100 milliseconds
- The ready queue is treated as a circular, FIFO
queue. - The CPU scheduler goes around the ready queue
- Allocate the CPU to each process for a time
interval of up to 1 time quantum. - Designed especially for time-sharing systems
- Two cases
- CPU burst less than 1 time quantum
- The process release the CPU voluntarily
- CPU burst longer than 1 time quantum
- Context switch will be executed
31Example of round-robin scheduling
- Process Burst Time
- P1 24
- P2 3
- P3 3
- Round-robin scheduling
- A time-quantum of 4 milliseconds
- Average waiting time ( 6 4 7 ) / 3 5.66
- Often quite long
- RR scheduling is preemptive.
326.3.4 Round-Robin Scheduling
- n processes in the ready queue and the time
quantum is q - 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
- Depend heavily on the size of the time quantum
- q is very large the same as the FCFS policy
- q is very small called processor sharing
- q must be large with respect to context switch,
otherwise overhead is too high.
33A smaller time quantumincreases context switches
346.3.4 Round-Robin Scheduling
- Turnaround time also depends on the size of the
time quantum. - Rule of thumb
- 80 percent of the CPU burst shouldbe shorter
thanthe time quantum
356.3.5 Multilevel Queue Scheduling
- Processes are classified into different groups
- Foreground (or interactive) processes
- Background (or batch) processes
- Multilevel queue-scheduling algorithm
- Partition the ready queue into several separate
groups - Each group has its own scheduling algorithm
- Scheduling among the queues
- Fixed-priority preemptive scheduling
- Possibility of starvation
- Time-slice between the queues
- A certain portion of the CPU time
36Multilevel Queue Scheduling
376.3.6 Multilevel Feedback Queue
- A process can move between the various queues
- Use too much CPU time move to a lower-priority
queue - Wait too long move to a higher-priority queue
- This form of aging prevents starvation
- Multilevel-feedback-queue scheduler defined by
- 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
38Example ofMultilevel Feedback Queue
- Three queues
- Q0 time quantum 8 milliseconds
- Q1 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.
39Multilevel feedback queues
- The most general and complex scheme
Q0
Q1
Q2
40Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
416.4 Multiple-Processor Scheduling
- CPU scheduling is more complex when multiple CPUs
are available - Homogeneous system
- Identical processors
- Load sharing can occur
- Separate queue
- Common queue
- Self-scheduling
- Master-slave structure
- Heterogeneous system
- Different processors
42Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
436.5 Real-Time Scheduling
- Hard real-time systems
- complete a critical task within a guaranteed
amount of time. - Special-purpose software running on dedicated
hardware - Lack the full functionality of modern computers
and operating systems - Soft real-time computing
- Critical processes receive priority over less
fortunate ones - May cause unfair allocation of resources
- A general-purpose system can also support special
tasks
446.5 Real-Time Scheduling
- Soft real-time computing
- Priority scheduling
- Real-time process must have the highest priority
- The dispatch latency must be small
- OSs are forced to wait for a system call to
complete or an I/O block to take place before
doing a context switch - Solution system calls must be preemptible
- Insert preemption points
- Make the entire kernel preemptible
- Effective and complex method
- Solaris 2
- Over 100 ms v.s. 2 ms
45Dispatch Latency
46Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
476.6 Algorithm Evaluation
- How to select a CPU-scheduling algorithm for a
particular system? - Define the criteria
- CPU utilization
- Response time
- Throughput
- A criteria may include several measures
- Maximize CPU utilization under the constraint
that the maximum response time is 1 second. - Maximize throughput such that turnaround time is
linearly proportional to total execution time.
486.6.1 Deterministic Modeling
- Take a particular predetermined workload and
defines the performance of each algorithm for
that workload - Example
- Assume five processes arrive at time 0, in the
order given, with the length of the CPU-burst
time given in milliseconds - Process Burst Time Priority
- P1 10 3
- P2 1 1
- P3 2 4
- P4 1 5
- P5 5 2
- Consider the FCFS, SJF, and RR(quantum10
milliseconds) scheduling algorithms for this set
of processes. - Which gives the minimum average waiting time?
496.6.1 Deterministic Modeling
- Advantage
- Simple and fast
- Exact numbers
- Disadvantage
- Too specific, and require too much exact
knowledge, to be useful - Main use
- Describing scheduling algorithms and providing
examples
506.6.2 Queueing Models
- No static set of processes (and times)
- What can be determined
- The distribution of CPU and I/O bursts
- Measured and approximated by mathematical
formulas - Queueing-network analysis
- CPU is a server with its ready queue
- I/O system is a server with its device queue
- Knowing arrival rates and service rates
- Utilization, average queue length, average wait
time
516.6.2 Queueing Models
- Littles formula n l W
- n average queue length
- W average waiting time
- l average arrival rate for new processes
- Advantage
- Useful in comparing scheduling algorithms
- Disadvantage
- Approximation only
- Fairly limited applicability
526.6.3 Simulations
- Programming a model of the computer system
- Simulator
- A variable representing a clock
- Modify the system state to reflect the activities
of the devices, the processes, and the scheduler - Data generation
- Random-number
- Probability distribution
- Empirically
- Trace tapes
- Monitor the real system
- Expensive
- More detailed simulation provides more accurate
results
53Evaluation of CPU schedulersby simulation
546.6.4 Implementation
- A simulation is of limited accuracy.
- The only completely accurate way
- code it
- put it in the OS
- see how it work
- The cost is high
- In coding
- In the reaction of users
55Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
566.7 Process Scheduling Models
- User-level threads
- Managed by a thread library
- Kernel is unaware of them
- Process local scheduling
- Thread scheduling is done local to the
application - System global scheduling
- Decide which kernel thread to schedule
- Case study
- Solaris 2
- Windows 2000
- Linux
57Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
58Summary
59Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
- Process Scheduling Models
- Summary
- Exercises
60Exercises
61The End