Title: Operating System Principles
1Operating System Principles
- Ku-Yaw Chang
- canseco_at_mail.dyu.edu.tw
- Assistant Professor, Department of Computer
Science and Information Engineering - Da-Yeh University
2Chapter 5 Process Scheduling
- CPU scheduling or Process scheduling
- The basis of multiprogrammed OSs
- Make the computer more productive
- Switch the CPU among processes
- We introduce
- The basic scheduling concepts
- Several different CPU-scheduling algorithms
- Select an algorithm for a particular system
3Chapter 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
45.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. - CPU is one of the primary resources
- CPU scheduling is central to OS design
55.1.1 CPU-I/O Burst Cycle
- An observed property of processes
- Process execution consists of a cycle of
- CPU execution
- I/O wait
- Process execution begins with a CPU burst.
- 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 durations
85.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
95.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
105.1.3 Preemptive Scheduling
- CPU scheduling decisions may take place when a
process - Switch from running to waiting state
- Switch from running to ready state
- Switch from waiting to ready state
- Terminate
- Scheduling only under 1 and 4 is nonpreemptive
(or cooperative) - Otherwise is preemptive
- Incur a cost
115.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 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
135.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 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
155.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
165.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.
175.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
185.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
195.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
205.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
215.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
225.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-j
- (1 - ? )n1 ?0
- Since both ? and (1 - ?) are less than or equal
to 1, each successive term has less weight than
its predecessor.
255.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
275.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
295.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
305.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.
325.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
345.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
355.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
375.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 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
41 42Chapter 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
435.5.1 Contention Scope
- Process-contention scope (PCS)
- Competition for the CPU takes place among threads
belonging to the same process - Many-to-one and many-to-many models
- System-contention scope (SCS)
- Competition for the CPU takes place among all
threads in the system - One-to-one model
44Chapter 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
45 46Chapter 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
47Summary
48Chapter 5 Process Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Thread Scheduling
- Operating System Examples
- Algorithm Evaluation
- Summary
- Exercises
49Exercises
50The End