Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

34
Scheduling in Scheduling in Linux and Linux and Windows 2000 Windows 2000 CS 431 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042) Sanjay Kumar Ram (01D07042)

Transcript of Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Page 1: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling in Scheduling in Linux and Linux and

Windows 2000Windows 2000

CS 431CS 431

Sanjay Kumar (01D07041)Sanjay Kumar (01D07041)Sanjay Kumar Ram (01D07042)Sanjay Kumar Ram (01D07042)

Page 2: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Linux kernel Linux kernel

Linux processLinux process

Scheduling policyScheduling policy

Data Structures and Scheduling Algorithm in Data Structures and Scheduling Algorithm in Uniprocessor SystemUniprocessor System

Scheduling algorithm for multiprocessorScheduling algorithm for multiprocessor

Scheduling in windows 2000Scheduling in windows 2000

Page 3: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Linux Kernel Linux Kernel Kernel subsystem overview

Page 4: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Linux ProcessesLinux Processes Three class of processesThree class of processes

Interactive processesInteractive processes Batch processes Batch processes Real-time processesReal-time processes

Linux processes have the following states: Linux processes have the following states: Running Running WaitingWaiting StoppedStopped ZombieZombie

Page 5: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling PolicyScheduling Policy Linux kernel is Linux kernel is non-preemptivenon-preemptive but processes are but processes are preemptivepreemptive

The set of rules used to determine when and how selecting a new process The set of rules used to determine when and how selecting a new process to run is called to run is called scheduling policyscheduling policy

Linux scheduling is based on theLinux scheduling is based on the time-sharingtime-sharing technique -several technique -several processes are allowed to run "concurrently“processes are allowed to run "concurrently“

CPU time is roughly divided into "slices“CPU time is roughly divided into "slices“

scheduling policy is also based on ranking processes according to their scheduling policy is also based on ranking processes according to their prioritypriority

Static priorityStatic priority - - assigned by the users to real-time processes and assigned by the users to real-time processes and ranges from 1 to 99, and is never changed by the schedulerranges from 1 to 99, and is never changed by the scheduler

Dynamic priorityDynamic priority - - the sum of the base time quantum (the the sum of the base time quantum (the base prioritybase priority of the process) and of the number of ticks of CPU time left to the process of the process) and of the number of ticks of CPU time left to the process before its quantum expires in the current epochbefore its quantum expires in the current epoch

Page 6: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling Policy (contd.)Scheduling Policy (contd.) Process PreemptionProcess Preemption

When the When the dynamic prioritydynamic priority of the currently running process is of the currently running process is lower than the process waiting in the lower than the process waiting in the runqueuerunqueue

A process may also be preempted when its time quantum A process may also be preempted when its time quantum expiresexpires

How Long Must a Quantum Last?How Long Must a Quantum Last? Quantum duration too short - system overhead caused by Quantum duration too short - system overhead caused by

task switching becomes excessively hightask switching becomes excessively high Quantum duration too long - processes no longer appear to Quantum duration too long - processes no longer appear to

be executed concurrently, degrades the response time of be executed concurrently, degrades the response time of interactive applications, and degrades the responsiveness of interactive applications, and degrades the responsiveness of the system the system

The rule of thumb adopted by Linux is: choose a The rule of thumb adopted by Linux is: choose a duration as long as possible, while keeping good duration as long as possible, while keeping good system response timesystem response time

Page 7: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Data Structures and Scheduling Data Structures and Scheduling Algorithm in Uniprocessor SystemAlgorithm in Uniprocessor System

The Linux scheduling algorithm works by dividing the CPU time The Linux scheduling algorithm works by dividing the CPU time into into epochsepochs

In a single epoch, every process has a specified time quantumIn a single epoch, every process has a specified time quantum epoch ends when all runnable processes have exhausted their quantumepoch ends when all runnable processes have exhausted their quantum

Each process has a Each process has a base time quantumbase time quantum the time-quantum value assigned by the scheduler to the process if it the time-quantum value assigned by the scheduler to the process if it

has exhausted its quantum in the previous epochhas exhausted its quantum in the previous epoch users can change the base time quantum of their processes by using the users can change the base time quantum of their processes by using the

nice( )nice( ) and and setpriority( )setpriority( ) system calls system calls

The INIT_TASK macro sets the value of the base time quantum The INIT_TASK macro sets the value of the base time quantum of process 0 (of process 0 (swapperswapper) to DEF_PRIORITY, which is approx. ) to DEF_PRIORITY, which is approx. 200ms200ms

Page 8: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Data Structures and Scheduling Algorithm in Data Structures and Scheduling Algorithm in Uniprocessor System (contd.)Uniprocessor System (contd.)

The first entry in the process table is the special The first entry in the process table is the special init processinit process Task_structTask_struct - represents the states of all tasks running in the - represents the states of all tasks running in the

systemssystems schedule policy - schedule policy - SCHED_OTHER, SCHED_FIFO, SCHED_RRSCHED_OTHER, SCHED_FIFO, SCHED_RR field for process state – field for process state – runningrunning, , waitingwaiting, , stoppedstopped, , zombiezombie a field that indicates the processes a field that indicates the processes prioritypriority countercounter - - a field which holds the number of clock ticksa field which holds the number of clock ticks a doubly linked list (a doubly linked list (next_tasknext_task and and prev_taskprev_task) - ) - to keep track to keep track

of all executing processesof all executing processes mm_struct - mm_struct - contains a process's memory management contains a process's memory management

informationinformation process IDprocess ID and and group IDgroup ID are also stored are also stored fs_structfs_struct - - File specific process dataFile specific process data fields that hold timing informationfields that hold timing information

Page 9: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Data Structures and Scheduling Algorithm in Data Structures and Scheduling Algorithm in Uniprocessor System (contd.)Uniprocessor System (contd.)

Each time the scheduler is run it does the following:Each time the scheduler is run it does the following: Kernel workKernel work - - scheduler runs the bottom half handlers and processes the scheduler runs the bottom half handlers and processes the

scheduler task queuescheduler task queue Current processCurrent process

current process must be processed before another process can be current process must be processed before another process can be selected to runselected to run

If the scheduling policy of the current processes is If the scheduling policy of the current processes is round robinround robin then it is then it is put onto the back of the run queueput onto the back of the run queue

If the task is INTERRUPTIBLE and it has received a signal since the last If the task is INTERRUPTIBLE and it has received a signal since the last time it was scheduled then its state becomes RUNNINGtime it was scheduled then its state becomes RUNNING

If the current process has timed out, then its state becomes RUNNINGIf the current process has timed out, then its state becomes RUNNING

If the current process is RUNNING then it will remain in that stateIf the current process is RUNNING then it will remain in that state

Processes that were neither RUNNING nor INTERRUPTIBLE are Processes that were neither RUNNING nor INTERRUPTIBLE are removed from the run queueremoved from the run queue

Page 10: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Data Structures and Scheduling Algorithm in Data Structures and Scheduling Algorithm in Uniprocessor System (contd.)Uniprocessor System (contd.)

Process SelectionProcess Selection most deserving process is selected by the schedulermost deserving process is selected by the scheduler

real time processes are given higher priority than ordinary real time processes are given higher priority than ordinary processesprocesses

when several processes have the same priority, the one when several processes have the same priority, the one nearest the front of the run queue is chosennearest the front of the run queue is chosen

when a new process is created the number of ticks left to the when a new process is created the number of ticks left to the parent is split in two halves, one for the parent and one for parent is split in two halves, one for the parent and one for the childthe child

prioritypriority and and countercounter fields are used both to implement time- fields are used both to implement time-sharing and to compute the process dynamic prioritysharing and to compute the process dynamic priority

Page 11: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Data Structures and Scheduling Algorithm in Data Structures and Scheduling Algorithm in Uniprocessor System (contd.)Uniprocessor System (contd.)

schedule( ) Functionschedule( ) Function implements the scheduler. implements the scheduler. finds a process in the runqueue list and then assigns the CPU to itfinds a process in the runqueue list and then assigns the CPU to it

Direct invocationDirect invocation1. Inserts current in the proper wait queue 1. Inserts current in the proper wait queue 2. Changes the state of current either to TASK_INTERRUPTIBLE or to 2. Changes the state of current either to TASK_INTERRUPTIBLE or to

TASK_UNINTERRUPTIBLE TASK_UNINTERRUPTIBLE 3. Invokes schedule( ) 3. Invokes schedule( ) 4. Checks if the resource is available; if not, goes to step 2 4. Checks if the resource is available; if not, goes to step 2 5. Once the resource is available, removes current from the wait queue5. Once the resource is available, removes current from the wait queue

Lazy invocationLazy invocation When current has used up its quantum of CPU time; this is done by the When current has used up its quantum of CPU time; this is done by the

update_process_times( )update_process_times( ) function function When a process is woken up and its priority is higher than that of the When a process is woken up and its priority is higher than that of the

current process; performed by the current process; performed by the reschedule_idle( )reschedule_idle( ) function, which is function, which is invoked by the invoked by the wake_up_process( )wake_up_process( ) function function

When a When a sched_setscheduler( )sched_setscheduler( ) or or sched_ yield( )sched_ yield( ) system call is issued system call is issued

Page 12: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Data Structures and Scheduling Algorithm in Data Structures and Scheduling Algorithm in Uniprocessor System (contd.)Uniprocessor System (contd.)

Actions performed by Actions performed by schedule( )schedule( ) Before actually scheduling a process, the Before actually scheduling a process, the schedule( )schedule( ) function starts by function starts by

running the functions left by other kernel control paths in various queuesrunning the functions left by other kernel control paths in various queues The function then executes all active unmasked bottom halvesThe function then executes all active unmasked bottom halves

SchedulingScheduling value of current is saved in the value of current is saved in the prevprev local variable and the local variable and the

need_reschedneed_resched field of field of prevprev is set to 0 is set to 0 a check is made to determine whether a check is made to determine whether prevprev is a Round Robin real-time is a Round Robin real-time

process. If so, process. If so, schedule( )schedule( ) assigns a new quantum to assigns a new quantum to prevprev and puts it at and puts it at the bottom of the runqueue listthe bottom of the runqueue list

if state is TASK_INTERRUPTIBLE, the function wakes up the processif state is TASK_INTERRUPTIBLE, the function wakes up the process schedule( )schedule( ) repeatedly invokes the repeatedly invokes the goodness( )goodness( ) function on the function on the

runnable processes to determine the best candidaterunnable processes to determine the best candidate when when counter counter field becomes zero, field becomes zero, schedule( )schedule( ) assigns to all existing assigns to all existing

processes a fresh quantum, whose duration is the sum of the processes a fresh quantum, whose duration is the sum of the prioritypriority value plus half the value plus half the counter counter valuevalue

Page 13: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Data Structures and Scheduling Algorithm in Data Structures and Scheduling Algorithm in Uniprocessor System (contd.)Uniprocessor System (contd.)

goodness( )goodness( ) function function identify the best candidate among all processes in the runqueue list. identify the best candidate among all processes in the runqueue list. It receives as input parameters It receives as input parameters prevprev (the descriptor pointer of the (the descriptor pointer of the

previously running process) and previously running process) and pp (the descriptor pointer of the (the descriptor pointer of the process to evaluate)process to evaluate)

The integer value The integer value cc returned by returned by goodness( )goodness( ) measures the measures the "goodness" of "goodness" of pp and has the following meanings: and has the following meanings:

c = -1000c = -1000, , pp must never be selected; this value is returned when the must never be selected; this value is returned when the runqueue list contains only runqueue list contains only init_taskinit_task

c =0c =0, , pp has exhausted its quantum. Unless has exhausted its quantum. Unless p p is the first process in the is the first process in the runqueue list and all runnable processes have also exhausted their runqueue list and all runnable processes have also exhausted their quantum, it will not be selected for execution.quantum, it will not be selected for execution.

0 < c < 10000 < c < 1000, , pp is a conventional process that has not exhausted its is a conventional process that has not exhausted its quantum; a higher value of quantum; a higher value of cc denotes a higher level of goodness. denotes a higher level of goodness.

c >= 1000c >= 1000,, p p is a real-time process; a higher value of is a real-time process; a higher value of c c denotes a higher denotes a higher level of goodness. level of goodness.

Page 14: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for Scheduling algorithm for multiprocessormultiprocessor

The Linux scheduler is defined in kernel/sched.c. The Linux scheduler is defined in kernel/sched.c. The new scheduler have been designed to accomplish The new scheduler have been designed to accomplish

specific goals: specific goals: Implement fully O(1) schedulingImplement fully O(1) scheduling, , Every algorithm in the Every algorithm in the

new scheduler completes in constant-time, regardless of the new scheduler completes in constant-time, regardless of the number of running processes or any other input. number of running processes or any other input.

Implement perfect SMP scalabilityImplement perfect SMP scalability, , Each processor has Each processor has its own locking and individual runqueueits own locking and individual runqueue. .

Implement improved SMP affinityImplement improved SMP affinity, , Naturally attempt to Naturally attempt to group tasks to a specific CPU and continue to run them group tasks to a specific CPU and continue to run them there. Only migrate tasks from one CPU to another to there. Only migrate tasks from one CPU to another to resolve imbalances in runqueue sizes. resolve imbalances in runqueue sizes.

Page 15: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

Provide good interactive performanceProvide good interactive performance,, Even during Even during considerable system load, the system should react and schedule considerable system load, the system should react and schedule interactive tasks immediately. interactive tasks immediately.

Provide fairnessProvide fairness, , No process should find itself starved of No process should find itself starved of timeslice for any reasonable amount of time. Likewise, no timeslice for any reasonable amount of time. Likewise, no process should receive an unfairly high amount of timeslice. process should receive an unfairly high amount of timeslice.

Optimize for the common case of only 1-2 runnable processes, Optimize for the common case of only 1-2 runnable processes, yet scale well to multiple processors each with many processesyet scale well to multiple processors each with many processes

RunqueuesRunqueues It is the basic data structure in the schedulerIt is the basic data structure in the scheduler. . It is defined in kernel/sched.c as struct runqueue. It is defined in kernel/sched.c as struct runqueue. It is the list of runnable processes on a given processor; there is It is the list of runnable processes on a given processor; there is

one runqueue per processorone runqueue per processor. . Each runnable process is on Each runnable process is on exactly one runqueue. exactly one runqueue.

It additionally contains per-processor scheduling informationIt additionally contains per-processor scheduling information. .

Page 16: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

A group of macros is used to obtain specific runqueues. A group of macros is used to obtain specific runqueues. cpu_rqcpu_rq(processor)(processor) returns a pointer to the runqueue returns a pointer to the runqueue

associated with the given processor. associated with the given processor. this_rq()this_rq() returns the runqueue of the current processor. returns the runqueue of the current processor.

task_rq(task)task_rq(task) returns a pointer to the runqueue on which the returns a pointer to the runqueue on which the

given task is queued.given task is queued. this_rq_lock()this_rq_lock() locks the current runqueuelocks the current runqueue rq_unlock(struct runqueue *rq)rq_unlock(struct runqueue *rq) unlocks the given unlocks the given

runqueue. runqueue. double_rq_lock() and double_rq_unlock(),double_rq_lock() and double_rq_unlock(), t to avoid o avoid

deadlock deadlock

Page 17: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

The Priority ArraysThe Priority Arrays Each runqueue contains two priority arraysEach runqueue contains two priority arrays, , the active the active

and the expired array.and the expired array. Each priority array contains one queue of runnable Each priority array contains one queue of runnable

processors per priority level.processors per priority level. Priority arrays are the data structure that provide O(1) Priority arrays are the data structure that provide O(1)

scheduling.scheduling. It also contain a priority bitmap used to efficiently It also contain a priority bitmap used to efficiently

discover the highest priority runnable task in the discover the highest priority runnable task in the system. system.

Each priority array contains a bitmap field that has at Each priority array contains a bitmap field that has at least one bit for every priority on the system. least one bit for every priority on the system.

Page 18: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

Finding the highest priority task on the system is therefore only a Finding the highest priority task on the system is therefore only a matter of finding the first set bit in the bitmap. matter of finding the first set bit in the bitmap.

Each priority array also contains an array called queue of Each priority array also contains an array called queue of struct struct list_headlist_head queues, one queue for each priority. queues, one queue for each priority.

The Linux O(1) scheduler algorithm.The Linux O(1) scheduler algorithm.

Page 19: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

Recalculating TimeslicesRecalculating Timeslices The new Linux scheduler alleviates the need for a recalculate The new Linux scheduler alleviates the need for a recalculate

loop. Instead, it maintains two priority arrays for each processor: loop. Instead, it maintains two priority arrays for each processor: 1. 1. active arrayactive array, , contains all the tasks in the associated contains all the tasks in the associated

runqueue runqueue that have timeslice left. that have timeslice left. 2.2. expired array , expired array , contains all the tasks in the associated contains all the tasks in the associated runqueue that have exhausted their timeslice.runqueue that have exhausted their timeslice.

When each task's timeslice reaches zero, its timeslice is When each task's timeslice reaches zero, its timeslice is recalculated before it is moved to the expired array. recalculated before it is moved to the expired array.

Page 20: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

• Recalculating all the timeslices is then as simple as just Recalculating all the timeslices is then as simple as just switching the active and expired arrays. Because the switching the active and expired arrays. Because the arrays are accessed only via pointer, switching them is arrays are accessed only via pointer, switching them is as fast as swapping two pointers. as fast as swapping two pointers.

• This swap is a key feature of the new O(1) scheduler.This swap is a key feature of the new O(1) scheduler. Instead of recalculating each process's priority and Instead of recalculating each process's priority and timeslice all the time, the O(1) scheduler performs a timeslice all the time, the O(1) scheduler performs a simple two-step array swap. simple two-step array swap.

Page 21: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

Sleeping and waking up Sleeping and waking up

Page 22: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling algorithm for multiprocessor Scheduling algorithm for multiprocessor (contd.)(contd.)

The load balancer The load balancer

Page 23: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling in windows 2000Scheduling in windows 2000

Windows 2000 scheduler:Windows 2000 scheduler: Concern is with threads not processesConcern is with threads not processes

• i.e. threads are scheduled, not processesi.e. threads are scheduled, not processes Threads have priorities 0 through 31Threads have priorities 0 through 31

31

1615

10i

16 “real-time” levels

15 variable levels

Used by zero page thread

Used by idle thread(s)

Page 24: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Manual Process Priority Manual Process Priority AdjustmentsAdjustments

Page 25: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Thread SchedulingThread Scheduling

Strictly priority drivenStrictly priority driven 32 queues (FIFO lists) of ready threads32 queues (FIFO lists) of ready threads

• One queue for each priority levelOne queue for each priority level• Queues are common to all CPUsQueues are common to all CPUs

When thread becomes ready, it:When thread becomes ready, it:• either runs immediately, oreither runs immediately, or• is inserted at the tail end of the Ready queue for its current is inserted at the tail end of the Ready queue for its current

prioritypriority On a uniprocessor, highest priority Ready thread On a uniprocessor, highest priority Ready thread

always runsalways runs Time-sliced, round-robin within a priority levelTime-sliced, round-robin within a priority level

Page 26: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Thread Scheduling Thread Scheduling Multiprocessor IssuesMultiprocessor Issues

On a multiprocessor, highest-priority On a multiprocessor, highest-priority n n threads threads will always run (subject to Affinity, explained will always run (subject to Affinity, explained later)later)

No attempt is made to share processors “fairly” No attempt is made to share processors “fairly” among processes, only among threadsamong processes, only among threads

Tries to keep threads on the same CPUTries to keep threads on the same CPU

Page 27: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling ScenariosScheduling Scenarios

PreemptionPreemption A thread becomes ready at a higher priority A thread becomes ready at a higher priority

than the running threadthan the running thread• Lower-priority thread is preemptedLower-priority thread is preempted• Preempted thread goes to the tail of its Ready Preempted thread goes to the tail of its Ready

queuequeue

Page 28: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Scheduling Scenarios (contd.)Scheduling Scenarios (contd.)

Voluntary switchVoluntary switch When the running thread gives up the CPUWhen the running thread gives up the CPU

• Waiting on a dispatcher objectWaiting on a dispatcher object• TerminationTermination• Explicit lowering of priorityExplicit lowering of priority

Schedule the thread at the head of the next non-empty Ready Schedule the thread at the head of the next non-empty Ready queuequeue

Running thread experiences quantum endRunning thread experiences quantum end Priority is decremented unless at thread base priorityPriority is decremented unless at thread base priority Thread goes to tail of Ready queue for its new priorityThread goes to tail of Ready queue for its new priority May continue running if no equal or higher-priority threads are May continue running if no equal or higher-priority threads are

Ready – i.e. it “gets” the new quantumReady – i.e. it “gets” the new quantum

Page 29: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Priority AdjustmentsPriority Adjustments

Threads in “dynamic” classes can have priority Threads in “dynamic” classes can have priority adjustments applied to them (Boost or Decay)adjustments applied to them (Boost or Decay) Idle, Below Normal, Normal, Above Normal and High Idle, Below Normal, Normal, Above Normal and High Carried out upon wait completionCarried out upon wait completion Used to avoid CPU starvationUsed to avoid CPU starvation

No automatic adjustments in “real-time” classNo automatic adjustments in “real-time” class Priority 16 and abovePriority 16 and above Scheduling is therefore “predictable” with respect to Scheduling is therefore “predictable” with respect to

the “real-time” threadsthe “real-time” threads• Note though that this does not mean that there are absolute Note though that this does not mean that there are absolute

latency guaranteeslatency guarantees

Page 30: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Priority BoostingPriority Boosting

Priority boost takes place after a waitPriority boost takes place after a wait Occurs when a wait (usually I/O) is resolvedOccurs when a wait (usually I/O) is resolved

• Slow devices / long waits = big boostSlow devices / long waits = big boost• Fast devices / short waits = small boostFast devices / short waits = small boost

Boost is applied to thread’s base priorityBoost is applied to thread’s base priority• Does not go over priority 15Does not go over priority 15

Keeps I/O devices busyKeeps I/O devices busy

Page 31: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

CPU StarvationCPU Starvation

Balance Set Manager looks for “starved” threadsBalance Set Manager looks for “starved” threads BSM is a thread running at priority 16, waking up BSM is a thread running at priority 16, waking up

once per secondonce per second Looks at threads that have been Ready for 4 seconds Looks at threads that have been Ready for 4 seconds

or moreor more Boosts up to 10 Ready threads per passBoosts up to 10 Ready threads per pass

Special boost appliedSpecial boost applied Priority 15Priority 15 Quantum is doubledQuantum is doubled

Does not apply in real-time rangeDoes not apply in real-time range

Page 32: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Multiprocessor SupportMultiprocessor Support

By default, threads can run on any available By default, threads can run on any available processorprocessor

Soft affinity (introduced in NT 4.0)Soft affinity (introduced in NT 4.0) Every thread has an “ideal processor”Every thread has an “ideal processor” When thread becomes ready:When thread becomes ready:

• if “ideal” is idle, it runs thereif “ideal” is idle, it runs there• else, if previous processor is idle, it runs thereelse, if previous processor is idle, it runs there• else, may look at next thread, to run on its ideal processorelse, may look at next thread, to run on its ideal processor

Page 33: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

Multiprocessor Support (contd.)Multiprocessor Support (contd.)

Hard affinityHard affinity Restricts thread to a subset of the available Restricts thread to a subset of the available

CPUsCPUs Can lead to:Can lead to:

• threads getting less CPU time that they normally threads getting less CPU time that they normally wouldwould

Page 34: Scheduling in Linux and Windows 2000 CS 431 Sanjay Kumar (01D07041) Sanjay Kumar Ram (01D07042)

ReferencesReferences

http://iamexwiwww.unibe.ch/studenten/schlpbch/linuxSchhttp://iamexwiwww.unibe.ch/studenten/schlpbch/linuxScheduling/LinuxScheduling.htmleduling/LinuxScheduling.html

http://oopweb.com/OS/Documents/tlk/VolumeFrames.hthttp://oopweb.com/OS/Documents/tlk/VolumeFrames.htmlml

http://www.samspublishing.com/articles/article.asp?p=10http://www.samspublishing.com/articles/article.asp?p=101760&rl=11760&rl=1

http://www.oreilly.com/catalog/linuxkernel/chapter/ch10.hhttp://www.oreilly.com/catalog/linuxkernel/chapter/ch10.htmltml

G. M. Candea and M. B. Jones, “ G. M. Candea and M. B. Jones, “ Vassal: Loadable Scheduler Support for Multi-Policy Scheduling,” Proceedings of the 2nd USENIX Windows NT Symposium Seattle, Washington, August 3–4, 1998