Professional Documents
Culture Documents
06 Scheduling
06 Scheduling
Announcements
CS 414 Homework next Wednesday, Feb 7th
CS 415 initial design documents due TODAY, Friday, Feb 2nd
Project due following Monday, February 12th
Review: Threads
Each thread has its own PC, registers, and stack pointer
But shares code, data, accounting info (address space)
Pthreads (POSIX - Portable Operating System Interface for uniX)
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
API specifies behavior of the thread library, implementation is up to development of the library
Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Windows XP Threads
Implements the one-to-one mapping
Each thread contains
A thread id
Register set
Separate user and kernel stacks
Private data storage area
The register set, stacks, and private storage area are known as the context
of the threads
Review Threads
Linux Threads
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system call
clone() allows a child task to share the address space of the parent task
(process)
Java Threads
Java threads are managed by the JVM
Java threads may be created by:
Extending Thread class
Implementing the Runnable interface
CPU Schedulers
Scheduling Algorithms
Algorithm Evaluation Metrics
Algorithm details
Thread Scheduling
Multiple-Processor Scheduling
Real-Time Scheduling
Schedulers
Process migrates among several queues
Device queue, job queue, ready queue
Short-term scheduler:
Select ready process to run on CPU
Should be fast
Process Scheduling
process and thread used interchangeably
Which process to run next
Many processes in ready state
Which ready process to pick to run on the CPU?
0 ready processes: run idle loop
1 ready process: easy!
> 1 ready process: what to do?
New
Ready
Running
Waiting
Exit
Preemptive minimum
All of the above, plus:
Event completes: process moves from blocked to ready
Timer interrupts
Implementation: process can be interrupted in favor of another
New
Running
Ready
Waiting
Exit
Process Model
Process alternates between CPU and I/O bursts
CPU-bound jobs: Long CPU bursts
Matrix multiply
I/O-bound: Short CPU bursts
emacs
emacs
I/O burst = process idle, switch to another for free
Problem: dont know jobs type before running
Problem Cases
Blindness about job types
I/O goes idle
Problem:
Average waiting time depends on arrival order
P1
P2
16
P2
P3
4
P3
time
20
24
P1
8
24
Convoy Effect
A CPU bound job will hold CPU until done,
or it causes an I/O burst
rare occurrence, since the thread is CPU-bound
Problem:
May lead to starvation early processes may never get
CPU
Problem
You work as a short-order cook
Customers come in and specify which dish they want
Each dish takes a different amount of time to prepare
Your goal:
minimize average time the customers wait for their food
P1
P3
15
PP2 2
0 3
Problem:
PP3 3
P2
21 24
P1
9
24
P1
6
P3
0
P1
6
21
10
P2
10 13
P1
24
=1
n+1 = tn
Only the actual last CPU burst counts
Priority Scheduling
Priority Scheduling
Choose next job based on priority
For SJF, priority = expected CPU burst
Can be either preemptive or non-preemptive
Problem:
Starvation: jobs can wait indefinitely
Solution to starvation
Age processes: increase priority as a function of waiting time
Round Robin
Round Robin (RR)
P2
20
P3
37
P4
57
P1
77
P3
P4
P1
P3
P3
Moral:
Context switch is usually negligible (< 1% per timeslice)
otherwise you context switch too frequently and lose all
productivity
Scheduling Algorithms
Multi-level Queue Scheduling
Implement multiple ready queues based on job type
interactive processes
CPU-bound processes
batch jobs
system processes
student programs
Scheduling Algorithms
Multi-level Feedback Queues
Implement multiple ready queues
Different queues may be scheduled using different algorithms
Just like multilevel queue scheduling, but assignments are not static
A Multi-level System
high
priority
CPU bound jobs
low
timeslice
high
Thread Scheduling
Since all threads share code & data segments
Option 1: Ignore this fact
Option 2: Gang scheduling
run all threads belonging to a process together (multiprocessor only)
if a thread needs to synchronize with another thread
the other one is available and active
Real-time Scheduling
Real-time processes have timing constraints
Expressed as deadlines or rate requirements
Postscript
The best schemes are adaptive.
To do absolutely best wed have to predict the future.
Most current algorithms give highest priority to those that need
the least!