Os Module 3
Os Module 3
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Thread Scheduling
• Multiple-Processor Scheduling
• Operating Systems Examples
• Algorithm Evaluation
Objectives
• To introduce CPU scheduling, which is the basis for
multiprogrammed operating systems
0 24 27 30
P2 P3 P1
0 3 6 30
0 3 9 16 24
• Can only estimate the length – should be similar to the previous one
• Then pick process with shortest predicted next CPU burst
• Can be done by using the length of previous CPU bursts, using exponential
averaging
1. t n actual length of n th CPU burst
2. n 1 predicted value for the next CPU burst
3. , 0 1
4. Define :
n 1 t n 1 n .
• Commonly, α set to ½
• Preemptive version called shortest-remaining-time-first
Prediction of the Length of the
Next CPU Burst
Examples of Exponential Averaging
• =0
• n+1 = n
• Recent history does not count
• =1
• n+1 = tn
• Only the actual last CPU burst counts
• If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
• Now we add the concepts of varying arrival times and preemption to the analysis
0 1 5 10 17 26
P2 P5 P1 P3 P4
0 1 6 16 18 19
0 4 7 10 14 18 22 26 30
• Three queues:
• Q0 – RR with time quantum 8 milliseconds
• Q1 – RR 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
Multilevel Feedback Queues
Thread Scheduling
• Distinction between user-level and kernel-level threads
• Solaris scheduling
• Windows XP scheduling
• Linux scheduling
Solaris
• Priority-based scheduling
• Six classes available
• Time sharing (default)
• Interactive
• Real time
• System
• Fair Share
• Fixed priority
• Given thread can be in one class at a time
• Each class has its own scheduling algorithm
• Time sharing is multi-level feedback queue
• Loadable table configurable by sysadmin
Solaris Dispatch Table
Solaris Scheduling
Solaris Scheduling (Cont.)
• Scheduler converts class-specific priorities into a per-thread global
priority
• Thread with highest priority runs next
• Runs until (1) blocks, (2) uses time slice, (3) preempted by higher-priority
thread
• Multiple threads at same priority selected via RR
Windows Scheduling
• Windows uses priority-based preemptive scheduling
• Highest-priority thread runs next
• Dispatcher is scheduler
• Thread runs until (1) blocks, (2) uses time slice, (3) preempted by higher-priority
thread
• Real-time threads can preempt non-real-time
• 32-level priority scheme
• Variable class is 1-15, real-time class is 16-31
• Priority 0 is memory-management thread
• Queue for each priority
• If no run-able thread, runs idle thread
Windows Priority Classes
• Win32 API identifies several priority classes to which a process can belong
• REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,
ABOVE_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS,
BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS
• All are variable except REALTIME
• A thread within a given priority class has a relative priority
• TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL, BELOW_NORMAL, LOWEST, IDLE
• Priority class and relative priority combine to give numeric priority
• Base priority is NORMAL within the class
• If quantum expires, priority lowered, but never below base
• If wait occurs, priority boosted depending on what was waited for
• Foreground window given 3x priority boost
Windows XP Priorities
Linux Scheduling
• Deterministic modeling
• Type of analytic evaluation
• Takes a particular predetermined workload and defines the
performance of each algorithm for that workload
Queueing Models
• Describes the arrival of processes, and CPU and I/O bursts
probabilistically
• Commonly exponential, and described by mean
• Computes average throughput, utilization, waiting time, etc
• Computer system described as network of servers, each with queue
of waiting processes
• Knowing arrival rates and service rates
• Computes utilization, average queue length, average wait time, etc
Little’s Formula
• n = average queue length
• W = average waiting time in queue
• λ = average arrival rate into queue
• Little’s law – in steady state, processes leaving queue must equal
processes arriving, thus
n=λxW
• Valid for any scheduling algorithm and arrival distribution
while (true) {
// perform CPU-intensive task
...
Thread.yield();
}
• Example
• System has 2 disk drives
• P1 and P2 each hold one disk drive and each needs another
one
• Example
• semaphores A and B, initialized to 1 P0 P1
wait (A); wait(B) wait (B); wait(A)
Bridge Crossing Example
• Process
Pi
Rj
• Pi requests instance of Rj
Pi
• Pi is holding an instance of Rj Rj
Example of a Resource Allocation
Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
• If graph contains no cycles no deadlock
• That is:
• If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished
• When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate
• When Pi terminates, Pi +1 can obtain its needed resources, and so on
Basic Facts
• If a system is in safe state no deadlocks
• Multiple instances
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
Example: P1 Request (1,0,2)
• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
• Detection algorithm
• Recovery scheme
Single Instance of Each Resource Type
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Example (Cont.)
• P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
• State of system?
• Can reclaim resources held by process P0, but insufficient resources
to fulfill other processes; requests
• Deadlock exists, consisting of processes P1, P2, P3, and P4
Detection-Algorithm Usage
• When, and how often, to invoke depends on:
• How often a deadlock is likely to occur?
• How many processes will need to be rolled back?
• one for each disjoint cycle