Title: CS 2200
1CS 2200
- Presentation 8
- Process Issues
- Scheduling Algorithms
2Where are we?
Control
Datapath
Timer
I/O
I/O
INT ACK
INT REQ
3Once upon a time...
- Computers did one thing at a time.
4Once upon a time...
- Computers did one thing at a time.
- There is a big difference between the speed of
the processor and speed of the outside world.
Processors Nanoseconds
Peripherals Micro-Milliseconds
People Seconds-Long lazy afternoons
5Once upon a time...
- Computers did one thing at a time.
- There is a big difference between speed on the
processor and speed of the outside world. - So, in the interests of efficiency, systems were
developed to allow the processor to work on may
jobs concurrently.
6Processor Scheduling
- Objective
- Have some job running at all times
7Environment
- Specific goals depend on system environment
- dedicated
- multiprogramming
- interactive or time-sharing
8Definitions
- Program
- Code
- Passive
- Resides on disk
- Process
- Code
- Active
- Current Activity
- PC
- Stack
- Registers
- Data
- Memory (and Disk)
- Resources
Basic Unit of Scheduling
9Process States
New
Terminated
Ready
Running
Waiting
10What do we need to keep track of?
11Process Control Block
Pointer
Process State
Process Number
Program Counter
Registers
Scheduling Info
Memory Limits
I/O Status Info
Accounting Info
12Process Control Block
- enum state_type new, ready, running, waiting,
-
halted - typedef struct _control_block
-
- struct control_block next_pcb
- enum state_type state
- int pid
- address PC
- int reg_fileNumRegs
- int priority
- address page_table
- ...
- control_block
13Scheduling First Principles
- What is a scheduler?
- Program run by OS
- Decides which processes should be in what state
- Where are these processes?
- In memory
- Abstracted in process control blocks
- Managed with a series of queues
14Question
- Assume acme is a single CPU machine and you write
a program in C. - Are the actual instructions that your C program
produced run on the processor (choice 1) or is
there some kind of OS that runs your program to
allow for protection of the system (choice 2) or
thinking about things like this makes your head
hurt (choice 7)?
15Interrupts
16Question?
- Is the operating system running all the time?
- Yes
- No
- Depends
- Add a bit
17Scheduling First Principles
- How do we switch from one running job to another?
- Context switch
- Move all state info from job to be context
switched out into PCB. - Move all state info from job to be context
switched in from PCB into registers, PC etc. - Typical time 1?sec - 1 ms (Hardware dependent)
- Performed by dispatcher
18Types of Schedulers
- Short term
- CPU Scheduler
- Selects from ready-to-run processes
- Runs very frequently (every few milliseconds)
- Must be quick/small
- Timesharing
- Long term scheduler
- Controls degree of multiprogramming
- Number of processes in memory
- Must balance between CPU-bound and I/O-bound
processes
19Bound?
- CPU Bound
- Compute intensive
- Generates I/O Requests Infrequently
- I/O Bound
- Spends most of its time doing I/O
20CPU-I/O Burst Cycle
Compute
Wait for I/O
Compute Bound
I/O Bound
21Types of schedulers
- May also have Medium term scheduler
- Reduce thrashing
- Dispatcher
- Transfer control to a selected process
- Switches context
- Switches to user mode
- Jumps to proper starting point.
22Process Scheduling
23Algorithms
- timer interrupt
- save context and enqueue in ready queue
- goto select
- I/O request
- save context and enqueue in device queue
- goto select
- I/O completion
- move context from device to ready queue
- termination
- free context
- goto select
- select
- get head of queue
- set timer
- dispatch
24Non-Preemptive Scheduling
Long Term Scheduler sending jobs in
Process has control of CPU until it gives it up
voluntarily
CPU
Ready Queue
I/O
I/O Request
I/O Queue
Time Slice Expired
Child Executes
Fork a Child
Wait for an Interrupt
Interrupt Occurs
25Preemptive Scheduling
Long Term Scheduler sending jobs in
Processes may be forced to give up CPU.
CPU
Ready Queue
I/O
I/O Request
I/O Queue
Time Slice Expired
Child Executes
Fork a Child
Wait for an Interrupt
Interrupt Occurs
26Forking
- Technique used to start up a process
- System call fork()
- Essentially duplicates process creating a child
Parent
x fork()
Parent
Child
x child pid
x 0
27Questions?
28Scheduling Algorithms
29Scheduling Algorithms
- Metrics
- CPU utilization ()
- Throughput
- Processes completed per unit time
- Turnaround time
- Submission to completion
- Waiting time
- Time in ready queue
- Response time
- Interactive systems
30Consider a copying machine
31Scheduling Algorithms
- First-Come, First-Served
- Shortest-Job-First
- Priority
- Round-Robin
- Multilevel Queue
- Multilevel Feedback Queue
- Algorithm Evaluation
32First-Come, First-Served
- Non-preemptive
- Process has to voluntarily relinquish by
terminating or by making a blocking system call - Intrinsic property used Arrival time
- Expected performance
- No starvation
- Variance in turnaround time
- Convoy effect CPU followed by I/O bursts
- gt Poor utilization
33Convoy Effect
CPU
Ready Queue
IOB
IOB
IOB
CPUB
I/O
I/O Request
I/O Queue
CPU is running CPUB while I/O Bound jobs wait.
34Convoy Effect
CPU
Ready Queue
IOB
IOB
I/O
I/O Request
I/O Queue
IOB
CPUB
CPUB does small amount of I/O
35Convoy Effect
CPU
Ready Queue
CPUB
IOB
I/O
I/O Request
I/O Queue
IOB
IOB
I/O Bound jobs take very small amount of CPU
time, do some I/O
36Convoy Effect
CPU
Ready Queue
IOB
IOB
CPUB
IOB
I/O
I/O Request
I/O Queue
But most of the time I/O Bound jobs are waiting
in ready queue
37First-Come, First-Served
Process
Burst Time
P1
24
P2
3
P3
3
Start
P1
P3
P2
- average waiting time ?
- 0) 10ms
- 17ms
- 30 ms
38First-Come, First-Served
Process
Burst Time
P1
24
P2
3
P3
3
Start
P1
P3
P2
average waiting time (0 24 27)/3 17 ms
P3
P2
P1
average waiting time (0 3 6)/3 3 ms
39Shortest-Job-First
- Non-preemptive
- Better response for short jobs
- Provably optimal
- min avg. waiting time for a pool of jobs
- Intrinsic property CPU burst time required
- How to know this?
- Ask the user?
- Phone a friend?
- Assume same as last burst?
- Weighted history
40Priority
- Extrinsic property Associate priority with each
process - SJF is a special case of this where
- p 1/burst-time
- One queue for each priority level
- FCFS within each level
- Problem with priority?
- Solution?
- Increase priority with aging
41Priority Scheme
- 15 Emergency 10.0 50.00
- 14 Rush 10.0
- 13 Rush systems n/a
- 12 Friends of Governor n/a
- 11 Rich alumni n/a
- 10 Systems n/a
- 9 Aged High 1.5
- 8 High 1.5
- 7 Aged Normal 1.0
- 6 Normal 1.0
- 5 Aged Overnight 0.5
- 4 Overnight 0.5
- 3 Aged Background 0.3
- 2 Background 0.3
- 1 Idle 0.1
- 0 Hold n/a
42Priority Scheduling
Process
Priority
Burst Time
P1
3
7
P2
1
1
P3
3
2
P4
4
1
P5
2
5
Assume all arrived at T 0 (in order shown)!
P1
P2
P3
P5
P4
43Preemptive Algorithms
- FCFS intrinsically non-preemptive
- SJF and Priority algorithms can be made
preemptive - How?
- SJF Shortest remaining time first
- Priority
- Scheduler for time-sharing systems
- Preemptive or non-preemptive?
44Round-Robin
- Preemptive
- Good for time-sharing systems
- When to preempt?
- Processor assigned in time quantum or time
slice units (q) - FCFS is a special case of RR
- q infinity
- Processor sharing
- q 1?sec
- Appears as though every process is on a processor
running at speed 1/n. - Balancing q with context switch time
45Round-Robin Example
Process
Burst Time
P1
24
P2
3
P3
3
Assume q 4...
2
1
3
1
1
1
1
1
46Multilevel Queues
- Partition Ready Queue
- Each queue has its own scheduling algorithm
- E.g. foreground (interactive, RR) and background
(batch, FCFS) queues - Approaches
- Higher queues have absolute priority over lower
level queues OR - Time slice between the queues
47Multilevel Feedback Queues
- Add feedback to the queues
- Why?
- Adjust priority dynamically
- Move jobs between the queues
Quantum 8
Quantum 16
FCFS
48Evaluation
- Modeling
- Deterministic - Requires exact numbers
- Queueing - Uses distribution of bursts
- Simulation
- Create software models of systems.
- Lots of work
- Very time consuming
- Actual implementation test
- Cost
- May change behavior
49Questions?
50(No Transcript)