Download CPU Scheduling - Operating System - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
1
Chapter 5:
CPU Scheduling
- Always want to have CPU (or CPU’s) working
- Usually many processes in ready queue
- Ready to run on CPU
- Focus on a single CPU here
- Need strategies for
- Allocating CPU time
- Selecting next process to run
- Deciding what happens after a process completes a system call, or completes I/O
- Short-term scheduling
- Must not take much CPU time to do the scheduling
2
CPU Burst & I/O Burst
Alternation (Fig. 5.1)
What happens when the I/O burst occurs?
What is the process state at that time?
In general, most programs have short CPU bursts
When does/can scheduling occur?
1. When a process terminates
2. When a process switches from running to
waiting state
- E.g., when it does an I/O request
3. When a process switches from running to ready
state
- E.g., when a timer interrupt occurs
4. When a process switches from waiting state to
ready state
Cases 1 & 2: Non-preemptive scheduling
Cases 3 & 4: Preemptive scheduling (also includes case 1 & 2) Docsity.com
When do Batch, Multiprogramming &
Multitasking System do Scheduling?
1. When a process terminates
2. When a process switches from running to
waiting state
- E.g., when it does an I/O request
3. When a process switches from running to ready
state
- E.g., when a timer interrupt occurs
4. When a process switches from waiting state to
ready state
Batch
Multiprogramming Multi-tasking or time-sharing
7
CPU Scheduling Algorithms
• First-Come, First-Served (FCFS)
- Complete the jobs in order of arrival
• Shortest Job First (SJF)
- Complete the job with shortest next CPU burst
• Priority (PRI)
- Processes have a priority
- Allocate CPU to process with highest priority
• Round-Robin (RR)
- Each process gets a small unit of time on CPU
(time quantum or time slice)
8
FCFS: First-Come First-Served
- Ready queue data structure: a FIFO queue
- Assuming we have (at least) a multiprogrammed system
- Example
- Draw Gantt chart
- Compute the average waiting time for processes with the following next CPU burst times and ready queue order: - P1: 20 - P2: 12 - P3: 8 - P4: 16 - P5: 4
- Waiting time:
- Time period spent in the ready queue (assume processes terminate)
- Also calculate average waiting time across processes
- Assume 0 context switch time
CPU burst times (^) E.g., time units are milliseconds
10
FCFS: First-Come First-Served
• Advantage: Relatively simple algorithm
• Disadvantage: long waiting times
11
SJF: Shortest Job First
- The job with the shortest next CPU burst time is
selected
- Example (from before):
- CPU job burst times and ready queue order:
- P1: 20
- P2: 12
- P3: 8
- P4: 16
- P5: 4
- Draw Gantt chart and compute the average waiting time given SJF CPU scheduling
- Assume 0 context switch time
13
SJF
• Provably shortest average wait time
• BUT: What do we need to actually
implement this?
14
SJF
- Requires future knowledge
- But, may estimate or predict
- How to estimate or predict the future?
- Use history to predict duration of next CPU burst
E.g., base on duration of last CPU burst and a number summarizing duration of prior CPU bursts
τn+1 = α * tn + (1 - α) * τn
Where: t (^) n is the actual duration of n th CPU burst value for the process, α is a constant indicating how much to base estimate on last CPU burst, and τn is the estimate of CPU burst duration for time n
16
Time Complexity of SJF with
estimation method?
- Need to keep ready ‘queue’ ordered
- Process with the shortest estimated next
CPU burst must be kept at the beginning of the ‘queue’
- If ‘queue’ is already sorted, time is O(n) or
O(lg n) where n is queue length
17
Priority Scheduling
- Have to decide on a numbering scheme
- 0 can be highest or lowest
- Priorities can be
- Internal
- Set according to O/S factors (e.g., memory requirements)
- External
- Set as a user policy; e.g., User importance
- Static
- Fixed for the duration of the process
- Dynamic
- Changing during processing
- E.g., as a function of amount of CPU usage, or length of time waiting (a solution to starvation)
19
Which CPU Scheduling
Algorithms Can be Preemptive?
- FCFS (First-come, First-Served)
- SJF (Shortest Job First)
- Can be either
- Choice when a new (shorter) job arrives
- Can preempt current job or not
- Priority
- Can be either
- Choice when a processes priority changes or when a higher priority process arrives
Priority Inversion
• What is this?
• Will leave to a question on Homework 3
20