Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling 主講人:虞台文
Content Organization of Schedulers  Embedded and Autonomous Schedulers  Priority Scheduling  Scheduling Methods  A Framework for Scheduling  Common Scheduling Algorithms  Comparison of Methods  Priority Inversion
Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling Organization of Schedulers
Process Scheduling/Dispatching Process  scheduling Long term  scheduling Move  process  to   Ready List  (“RL”) after creation (When and in which order?) Process  Dispatching Short term  scheduling Select  process  from   Ready List  to run We use “ scheduling ” to refer to  both .
Organization of Schedulers Embedded Called as function at  end  of  kernel call Runs as  part  of  calling process Autonomous Separate  process May have  dedicated CPU  on a  multiprocessor On  single-processor , run at every  quantum : scheduler  and other  processes   alternate p i : process S   : scheduler
Organization of Schedulers Embedded Called as function at  end  of  kernel call Runs as  part  of  calling process Autonomous Separate  process May have  dedicated CPU  on a  multiprocessor On  single-processor , run at every  quantum : scheduler  and other  processes   alternate S S S S OS S Unix Windows 2000 OS p 1 OS p 2 OS p 3 OS p 4 OS p 1 OS p 2 OS p 3 OS p 4
Priority Scheduling Priority function returns numerical value  P  for process  p : P  =  Priority ( p ) Static  priority:  unchanged  for lifetime of  p Dynamic  priority:  changes  at runtime Who  runs next? Based on priority
Priority-Leveled Processes Priority divides processes into  levels Implemented as  multilevel  ready list, say,  RL p  at  RL [ i ]  run before  q  at  RL [ j ]  if  i  >  j p ,  q  at  same  level are ordered by  other  criteria, e.g., the order of  arrival . RL [ n ]
The Processes in  Ready List Status.Type      { running ,  ready_a ,  ready_s }
The Scheduler
The Scheduler The scheduler may be  invoked   periodically when some  events   affect  the  priorities  of existing processes, e.g., a process is  blocked / suspended a process is  destroyed a new process  created a process is  activated
The Scheduler The scheduler may be  invoked   periodically when some  events   affect  the  priorities  of existing processes, e.g., a process is  blocked / suspended a process is  destroyed a new process  created a process is  activated Some processes in  RL  can get CPU. Running processes may be  preempted . The  scheduler / dispatcher  should always keep the  np  (#processors)  highest-priority active processes  running.
Invoking the Scheduler The  caller  to the  scheduler  may be a process to be blocked / suspended  or awakened / activated
Invoking the Scheduler The  caller  to the  scheduler  may be a process to be blocked / suspended  or awakened / activated
An Embedded Scheduler Scheduler () {  do {  Find highest priority  ready_a  process p;  Find a free cpu;  if (cpu != NIL)  Allocate_CPU (p,cpu);  } while (cpu != NIL);  do {  Find highest priority  ready_a  process p;  Find lowest priority running process q;  if (Priority(p) > Priority(q))  Preempt (p,q);  } while (Priority(p) > Priority(q)); if (self->Status.Type!=’running’)  Preempt (p,self);  } The  caller  to the  scheduler  may be a process to be blocked / suspended  or awakened / activated If free CPUs are available,  allocate  these free CPUs to high-priority processes in  ready_a  state To  preempt  low-priority  running  processes by high-priority  ready_a  ones. If the caller is going to be  idled ,  preempt   itself .
An Embedded Scheduler Scheduler () {  do {  Find highest priority  ready_a  process p;  Find a free  cpu ;  if ( cpu  != NIL)  Allocate_CPU (p, cpu );  } while ( cpu  != NIL);  do {  Find highest priority  ready_a  process p;  Find lowest priority running process q;  if (Priority(p) > Priority(q))  Preempt (p,q);  } while (Priority(p) > Priority(q)); if (self->Status.Type!=’running’)  Preempt (p,self);  } The  caller  to the  scheduler  may be a process to be blocked / suspended  or awakened / activated To  preempt  low-priority  running  processes by high-priority  ready_a  ones. If the caller is going to be  idled ,  preempt   itself .
An Embedded Scheduler Scheduler () {  do {  Find highest priority  ready_a  process p;  Find a free  cpu ;  if ( cpu  != NIL)  Allocate_CPU (p, cpu );  } while ( cpu  != NIL);  do {  Find highest priority  ready_a  process p;  Find lowest priority  running  process q;  if (Priority(p)  >  Priority(q))  Preempt (p,q);  } while (Priority(p)  >  Priority(q)); if (self->Status.Type!=’running’)  Preempt (p,self);  } The  caller  to the  scheduler  may be a process to be blocked / suspended  or awakened / activated If the caller is going to be  idled ,  preempt   itself .
An Embedded Scheduler Scheduler () {  do {  Find highest priority  ready_a  process p;  Find a free  cpu ;  if ( cpu  != NIL)  Allocate_CPU (p, cpu );  } while ( cpu  != NIL);  do {  Find highest priority  ready_a  process p;  Find lowest priority  running  process q;  if (Priority(p)  >  Priority(q))  Preempt (p,q);  } while (Priority(p)  >  Priority(q)); if (self->Status.Type != ’ running ’)  Preempt (p,self);  } The  caller  to the  scheduler  may be a process to be blocked / suspended  or awakened / activated
An Embedded Scheduler Scheduler () {  do {  Find highest priority  ready_a  process p;  Find a free  cpu ;  if ( cpu  != NIL)  Allocate_CPU (p, cpu );  } while ( cpu  != NIL);  do {  Find highest priority  ready_a  process p;  Find lowest priority  running  process q;  if (Priority(p)  >  Priority(q))  Preempt (p,q);  } while (Priority(p)  >  Priority(q)); if (self->Status.Type != ’ running ’)  Preempt (p,self);  } How to  simplify  the scheduler for a  uniprocessor  system?
Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling Scheduling Methods
Scheduling Policy Scheduling based on certain  criteria . For examples: batch  jobs vs.  interactive  users System  processes vs.  user  processes I/O bound  processes vs.  CPU bound  processes
The General Framework When  to schedule? Preemptive Nonpreemptive Who  to schedule? Priority  evaluation Arbitration  to break ties   Decision  Mode Decision  mode Priority  function Arbitration  Rule A scheduling policy is determined by:
The Decision Modes Preemptive : scheduler called periodically  ( quantum-oriented ) or  when system  state changes More  costly Nonpreemptive : scheduler called when process  terminates  or  blocks Not  adequate in  real-time  or  time-shared  systems Decision  mode Priority  function Arbitration  Rule A scheduling policy is determined by:
The Priority Function Possible parameters: Attained   service time  ( a ) Real   time in system  ( r ) Total   service time  ( t ) Period   ( d ) Deadline  (explicit or implied by period) External  priority  ( e ) Memory  requirements (mostly for batch) System  load  ( not  process-specific) Decision  mode Priority  function Arbitration  Rule A scheduling policy is determined by:
The Arbitration Rules Break  ties  among processes of the same priority Random Chronological  (First In First Out =  FIFO ) Cyclic  (Round Robin =  RR ) Decision  mode Priority  function Arbitration  Rule A scheduling policy is determined by:
Common Scheduling Algorithms First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
Example Processes for Scheduling First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2
FIFO First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
FIFO First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
SJF First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
SJF First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
SRT First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2 p 1
SRT First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2 p 1
RR First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Assume a  time-quantum  of 0.1 time units. 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 1 p 2 p 1
RR First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Assume a  time-quantum  of 0.1 time units. 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 1 p 2 p 1
ML First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
ML First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Each process has a  fixed  priority. 1 2 n     1 n front rear Lowest Highest
ML  (Nonpreemptive) First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Each process has a  fixed  priority. 1 2 n     1 n front rear Lowest Highest
ML  (Preemptive) First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Each process has a  fixed  priority. 1 2 n     1 n front rear Lowest Highest
ML  (Nonpreemptive) First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) e p 1  = 15 e p 2  = 14 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
MLF Like ML, but  priority   changes   dynamically Every process  enters  at  highest  level   n Each level   P   prescribes maximum time   t P t P   increases  as   P   decreases Typically:   t n  = T  (constant) t P  =  2    t P+1 First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
MLF The  priority  of a process is a function of  a . Find   P   for given   a :  priority   attained time n a <T n  – 1   a <T+ 2 T n  – 2   a <T+ 2 T+ 4 T . . .  . . . n – i  a < (2 i+1 – 1) T First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) a : attained service time P  =  n  –  i  =  n  –   lg 2 ( a / T +1) 
MLF The  priority  of a process is a function of  a . Find   P   for given   a :  priority   attained time n a <T n  – 1   a <T+ 2 T n  – 2   a <T+ 2 T+ 4 T . . .  . . . n – i  a < (2 i+1 – 1) T First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) a : attained service time P  =  n  –  i  =  n  –   lg 2 ( a / T +1) 
RM & EDF RM Intended for  periodic  (real-time)  processes Preemptive   Highest priority:  shortest period :   P  = – d EDF Intended for  periodic  (real-time)  processes Preemptive   Highest priority:  shortest time to next deadline r    d   number of completed periods r  %  d   time in current period d – r  %  d   time remaining in current period P  = –( d – r % d )   First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
Common Scheduling Algorithms
Comparison of Methods FIFO ,  SJF ,  SRT : Primarily for  batch  systems FIFO  is the  simplest SJF  &  SRT  have better  average turnaround times : r i : the real time that the  i th  process spends in the system.
Example arrival service p 1 p 2 p 3 t t t    3 4 2 1 Comparison of average turnaround time
Comparison of Methods Time-sharing  systems Response time  is critical RR  or  MLF  with  RR  within each queue are suitable
Comparison of Methods Time-sharing  systems Response time  is critical RR  or  MLF  with  RR  within each queue are suitable Choice of  quantum  determines  overhead When   q       ,  RR  approaches  FIFO When   q     0 ,  context switch   overhead      100% When  q  >>   context switch   overhead , n   processes run concurrently at   1/ n   CPU speed
Interactive Systems Most  dynamic   schemes  tend to move  interactive  and  I/O-bound  processes to the  top  of the priority queues and to let  CPU-bound  processes drift to  lower  levels. MLF  puts  I/O-completed  processes into  highest priority queue .
Comparison of Methods Real-time  systems Feasible  = All  deadlines  are  met CPU utilization  is defined as:   U = ∑  t i / d i Schedule is  feasible  if   U     1 EDF   always  yields  feasible  schedule (if   U     1 ) RM  yields feasible schedule if   U     0.7
Example: RM and EDF CPU Utilization 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 p 1 p 2 p 1 p 2 RM p 1 p 2 EDF
Example: RM and EDF CPU Utilization  fail 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 p 2 p 1 p 2 RM p 1 p 2 EDF p 1
Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling Priority Inversion
Priority Inversion Problem Assume priority order   p 1  >  p 2  >  p 3 . p 1   and   p 3   share common resource. p 2  is  independent  of  p 1  and  p 3   . p 1 P(mutex) CS_1 V(mutex) Program1 p 2 Program2 p 3 P(mutex) CS_3 V(mutex) Program3
Priority Inversion Problem Assume priority order   p 1  >  p 2  >  p 3 . p 1   and   p 3   share common resource. p 2  is  independent  of  p 1  and  p 3   .
Priority Inversion Problem Assume priority order   p 1  >  p 2  >  p 3 . p 1   and   p 3   share common resource. p 2  is  independent  of  p 1  and  p 3   .              
Priority Inversion Problem Assume priority order   p 1  >  p 2  >  p 3 . p 1   and   p 3   share common resource. p 2  is  independent  of  p 1  and  p 3   .               (Unrelated)  p 2  may delay  p 1   indefinitely .
Solutions Make CSs  nonpreemptable Practical  is CSs are  short  and  few Unsatisfactory   if  high-priority process  often  found themselves  waiting  for lower-priority process, especially unrelated ones. Naïve “solution”: Always run CS at priority of  highest process  that  shares  the  CS Problem:  p 1   cannot   preempt  lower-priority process inside CS,  even  when it does  not  try to enter CS -- a  different form of priority inversion . Dynamic  Priority  Inheritance See Next
Dynamic Priority Inheritance p 3  is  in  its CS p 1   attempts  to enter its CS p 3   inherits   p 1 ’s (higher) priority for the  duration  of CS Sha, Rajkumar, and Lehocsky 1990 Assume priority order   p 1  >  p 2  >  p 3 . p 1   and   p 3   share common resource. p 2  is  independent  of  p 1  and  p 3   .
Dynamic Priority Inheritance p 3  is  in  its CS p 1   attempts  to enter its CS p 3   inherits   p 1 ’s (higher) priority for the  duration  of CS Assume priority order   p 1  >  p 2  >  p 3 . p 1   and   p 3   share common resource. p 2  is  independent  of  p 1  and  p 3   .
Dynamic Priority Inheritance p 3  is  in  its CS p 1   attempts  to enter its CS p 3   inherits   p 1 ’s (higher) priority for the  duration  of CS Assume priority order   p 1  >  p 2  >  p 3 . p 1   and   p 3   share common resource. p 2  is  independent  of  p 1  and  p 3   .              
References Rensselaer Polytechnic Institute  http://www.cs.rpi.edu/academics/courses/fall04/os/c8/ University of east London Inside the Windows NT Scheduler, Part 1 Inside the Windows NT Scheduler, Part 2
Reading Assignment Chapter 6 CPU Scheduling in Operating System Concepts , Seventh Edition, by Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, John Wiley & Sons, Inc.

Os5 2

  • 1.
    Operating Systems PrinciplesProcess Management and Coordination Lecture 5: Process and Thread Scheduling 主講人:虞台文
  • 2.
    Content Organization ofSchedulers Embedded and Autonomous Schedulers Priority Scheduling Scheduling Methods A Framework for Scheduling Common Scheduling Algorithms Comparison of Methods Priority Inversion
  • 3.
    Operating Systems PrinciplesProcess Management and Coordination Lecture 5: Process and Thread Scheduling Organization of Schedulers
  • 4.
    Process Scheduling/Dispatching Process scheduling Long term scheduling Move process to Ready List (“RL”) after creation (When and in which order?) Process Dispatching Short term scheduling Select process from Ready List to run We use “ scheduling ” to refer to both .
  • 5.
    Organization of SchedulersEmbedded Called as function at end of kernel call Runs as part of calling process Autonomous Separate process May have dedicated CPU on a multiprocessor On single-processor , run at every quantum : scheduler and other processes alternate p i : process S : scheduler
  • 6.
    Organization of SchedulersEmbedded Called as function at end of kernel call Runs as part of calling process Autonomous Separate process May have dedicated CPU on a multiprocessor On single-processor , run at every quantum : scheduler and other processes alternate S S S S OS S Unix Windows 2000 OS p 1 OS p 2 OS p 3 OS p 4 OS p 1 OS p 2 OS p 3 OS p 4
  • 7.
    Priority Scheduling Priorityfunction returns numerical value P for process p : P = Priority ( p ) Static priority: unchanged for lifetime of p Dynamic priority: changes at runtime Who runs next? Based on priority
  • 8.
    Priority-Leveled Processes Prioritydivides processes into levels Implemented as multilevel ready list, say, RL p at RL [ i ] run before q at RL [ j ] if i > j p , q at same level are ordered by other criteria, e.g., the order of arrival . RL [ n ]
  • 9.
    The Processes in Ready List Status.Type  { running , ready_a , ready_s }
  • 10.
  • 11.
    The Scheduler Thescheduler may be invoked periodically when some events affect the priorities of existing processes, e.g., a process is blocked / suspended a process is destroyed a new process created a process is activated
  • 12.
    The Scheduler Thescheduler may be invoked periodically when some events affect the priorities of existing processes, e.g., a process is blocked / suspended a process is destroyed a new process created a process is activated Some processes in RL can get CPU. Running processes may be preempted . The scheduler / dispatcher should always keep the np (#processors) highest-priority active processes running.
  • 13.
    Invoking the SchedulerThe caller to the scheduler may be a process to be blocked / suspended or awakened / activated
  • 14.
    Invoking the SchedulerThe caller to the scheduler may be a process to be blocked / suspended or awakened / activated
  • 15.
    An Embedded SchedulerScheduler () { do { Find highest priority ready_a process p; Find a free cpu; if (cpu != NIL) Allocate_CPU (p,cpu); } while (cpu != NIL); do { Find highest priority ready_a process p; Find lowest priority running process q; if (Priority(p) > Priority(q)) Preempt (p,q); } while (Priority(p) > Priority(q)); if (self->Status.Type!=’running’) Preempt (p,self); } The caller to the scheduler may be a process to be blocked / suspended or awakened / activated If free CPUs are available, allocate these free CPUs to high-priority processes in ready_a state To preempt low-priority running processes by high-priority ready_a ones. If the caller is going to be idled , preempt itself .
  • 16.
    An Embedded SchedulerScheduler () { do { Find highest priority ready_a process p; Find a free cpu ; if ( cpu != NIL) Allocate_CPU (p, cpu ); } while ( cpu != NIL); do { Find highest priority ready_a process p; Find lowest priority running process q; if (Priority(p) > Priority(q)) Preempt (p,q); } while (Priority(p) > Priority(q)); if (self->Status.Type!=’running’) Preempt (p,self); } The caller to the scheduler may be a process to be blocked / suspended or awakened / activated To preempt low-priority running processes by high-priority ready_a ones. If the caller is going to be idled , preempt itself .
  • 17.
    An Embedded SchedulerScheduler () { do { Find highest priority ready_a process p; Find a free cpu ; if ( cpu != NIL) Allocate_CPU (p, cpu ); } while ( cpu != NIL); do { Find highest priority ready_a process p; Find lowest priority running process q; if (Priority(p) > Priority(q)) Preempt (p,q); } while (Priority(p) > Priority(q)); if (self->Status.Type!=’running’) Preempt (p,self); } The caller to the scheduler may be a process to be blocked / suspended or awakened / activated If the caller is going to be idled , preempt itself .
  • 18.
    An Embedded SchedulerScheduler () { do { Find highest priority ready_a process p; Find a free cpu ; if ( cpu != NIL) Allocate_CPU (p, cpu ); } while ( cpu != NIL); do { Find highest priority ready_a process p; Find lowest priority running process q; if (Priority(p) > Priority(q)) Preempt (p,q); } while (Priority(p) > Priority(q)); if (self->Status.Type != ’ running ’) Preempt (p,self); } The caller to the scheduler may be a process to be blocked / suspended or awakened / activated
  • 19.
    An Embedded SchedulerScheduler () { do { Find highest priority ready_a process p; Find a free cpu ; if ( cpu != NIL) Allocate_CPU (p, cpu ); } while ( cpu != NIL); do { Find highest priority ready_a process p; Find lowest priority running process q; if (Priority(p) > Priority(q)) Preempt (p,q); } while (Priority(p) > Priority(q)); if (self->Status.Type != ’ running ’) Preempt (p,self); } How to simplify the scheduler for a uniprocessor system?
  • 20.
    Operating Systems PrinciplesProcess Management and Coordination Lecture 5: Process and Thread Scheduling Scheduling Methods
  • 21.
    Scheduling Policy Schedulingbased on certain criteria . For examples: batch jobs vs. interactive users System processes vs. user processes I/O bound processes vs. CPU bound processes
  • 22.
    The General FrameworkWhen to schedule? Preemptive Nonpreemptive Who to schedule? Priority evaluation Arbitration to break ties Decision Mode Decision mode Priority function Arbitration Rule A scheduling policy is determined by:
  • 23.
    The Decision ModesPreemptive : scheduler called periodically ( quantum-oriented ) or when system state changes More costly Nonpreemptive : scheduler called when process terminates or blocks Not adequate in real-time or time-shared systems Decision mode Priority function Arbitration Rule A scheduling policy is determined by:
  • 24.
    The Priority FunctionPossible parameters: Attained service time ( a ) Real time in system ( r ) Total service time ( t ) Period ( d ) Deadline (explicit or implied by period) External priority ( e ) Memory requirements (mostly for batch) System load ( not process-specific) Decision mode Priority function Arbitration Rule A scheduling policy is determined by:
  • 25.
    The Arbitration RulesBreak ties among processes of the same priority Random Chronological (First In First Out = FIFO ) Cyclic (Round Robin = RR ) Decision mode Priority function Arbitration Rule A scheduling policy is determined by:
  • 26.
    Common Scheduling AlgorithmsFirst-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
  • 27.
    Example Processes forScheduling First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2
  • 28.
    FIFO First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
  • 29.
    FIFO First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
  • 30.
    SJF First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
  • 31.
    SJF First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
  • 32.
    SRT First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2 p 1
  • 33.
    SRT First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2 p 1
  • 34.
    RR First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Assume a time-quantum of 0.1 time units. 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 1 p 2 p 1
  • 35.
    RR First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Assume a time-quantum of 0.1 time units. 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 1 p 2 p 1
  • 36.
    ML First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
  • 37.
    ML First-In/First-Out (FIFO)Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Each process has a fixed priority. 1 2 n  1 n front rear Lowest Highest
  • 38.
    ML (Nonpreemptive)First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Each process has a fixed priority. 1 2 n  1 n front rear Lowest Highest
  • 39.
    ML (Preemptive)First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) Each process has a fixed priority. 1 2 n  1 n front rear Lowest Highest
  • 40.
    ML (Nonpreemptive)First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) e p 1 = 15 e p 2 = 14 0 1 2 3 4 5 6 7 8 9 p 1 p 2 CPU does other things Start Scheduling p 1 p 2
  • 41.
    MLF Like ML,but priority changes dynamically Every process enters at highest level n Each level P prescribes maximum time t P t P increases as P decreases Typically: t n = T (constant) t P = 2  t P+1 First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
  • 42.
    MLF The priority of a process is a function of a . Find P for given a : priority attained time n a <T n – 1 a <T+ 2 T n – 2 a <T+ 2 T+ 4 T . . . . . . n – i a < (2 i+1 – 1) T First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) a : attained service time P = n – i = n –  lg 2 ( a / T +1) 
  • 43.
    MLF The priority of a process is a function of a . Find P for given a : priority attained time n a <T n – 1 a <T+ 2 T n – 2 a <T+ 2 T+ 4 T . . . . . . n – i a < (2 i+1 – 1) T First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF) a : attained service time P = n – i = n –  lg 2 ( a / T +1) 
  • 44.
    RM & EDFRM Intended for periodic (real-time) processes Preemptive Highest priority: shortest period : P = – d EDF Intended for periodic (real-time) processes Preemptive Highest priority: shortest time to next deadline r  d number of completed periods r % d time in current period d – r % d time remaining in current period P = –( d – r % d ) First-In/First-Out (FIFO) Shortest-Job-First (SJF) Shortest-Remaining-Time (SRT) Round-Robin (RR) Multilevel Priority (ML) Multilevel Feedback (MLF) Rate Monotonic (RM) Earliest Deadline (EDF)
  • 45.
  • 46.
    Comparison of MethodsFIFO , SJF , SRT : Primarily for batch systems FIFO is the simplest SJF & SRT have better average turnaround times : r i : the real time that the i th process spends in the system.
  • 47.
    Example arrival servicep 1 p 2 p 3 t t t  3 4 2 1 Comparison of average turnaround time
  • 48.
    Comparison of MethodsTime-sharing systems Response time is critical RR or MLF with RR within each queue are suitable
  • 49.
    Comparison of MethodsTime-sharing systems Response time is critical RR or MLF with RR within each queue are suitable Choice of quantum determines overhead When q   , RR approaches FIFO When q  0 , context switch overhead  100% When q >> context switch overhead , n processes run concurrently at 1/ n CPU speed
  • 50.
    Interactive Systems Most dynamic schemes tend to move interactive and I/O-bound processes to the top of the priority queues and to let CPU-bound processes drift to lower levels. MLF puts I/O-completed processes into highest priority queue .
  • 51.
    Comparison of MethodsReal-time systems Feasible = All deadlines are met CPU utilization is defined as: U = ∑ t i / d i Schedule is feasible if U  1 EDF always yields feasible schedule (if U  1 ) RM yields feasible schedule if U  0.7
  • 52.
    Example: RM andEDF CPU Utilization 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 p 1 p 2 p 1 p 2 RM p 1 p 2 EDF
  • 53.
    Example: RM andEDF CPU Utilization  fail 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 p 2 p 1 p 2 RM p 1 p 2 EDF p 1
  • 54.
    Operating Systems PrinciplesProcess Management and Coordination Lecture 5: Process and Thread Scheduling Priority Inversion
  • 55.
    Priority Inversion ProblemAssume priority order p 1 > p 2 > p 3 . p 1 and p 3 share common resource. p 2 is independent of p 1 and p 3 . p 1 P(mutex) CS_1 V(mutex) Program1 p 2 Program2 p 3 P(mutex) CS_3 V(mutex) Program3
  • 56.
    Priority Inversion ProblemAssume priority order p 1 > p 2 > p 3 . p 1 and p 3 share common resource. p 2 is independent of p 1 and p 3 .
  • 57.
    Priority Inversion ProblemAssume priority order p 1 > p 2 > p 3 . p 1 and p 3 share common resource. p 2 is independent of p 1 and p 3 .              
  • 58.
    Priority Inversion ProblemAssume priority order p 1 > p 2 > p 3 . p 1 and p 3 share common resource. p 2 is independent of p 1 and p 3 .               (Unrelated) p 2 may delay p 1 indefinitely .
  • 59.
    Solutions Make CSs nonpreemptable Practical is CSs are short and few Unsatisfactory if high-priority process often found themselves waiting for lower-priority process, especially unrelated ones. Naïve “solution”: Always run CS at priority of highest process that shares the CS Problem: p 1 cannot preempt lower-priority process inside CS, even when it does not try to enter CS -- a different form of priority inversion . Dynamic Priority Inheritance See Next
  • 60.
    Dynamic Priority Inheritancep 3 is in its CS p 1 attempts to enter its CS p 3 inherits p 1 ’s (higher) priority for the duration of CS Sha, Rajkumar, and Lehocsky 1990 Assume priority order p 1 > p 2 > p 3 . p 1 and p 3 share common resource. p 2 is independent of p 1 and p 3 .
  • 61.
    Dynamic Priority Inheritancep 3 is in its CS p 1 attempts to enter its CS p 3 inherits p 1 ’s (higher) priority for the duration of CS Assume priority order p 1 > p 2 > p 3 . p 1 and p 3 share common resource. p 2 is independent of p 1 and p 3 .
  • 62.
    Dynamic Priority Inheritancep 3 is in its CS p 1 attempts to enter its CS p 3 inherits p 1 ’s (higher) priority for the duration of CS Assume priority order p 1 > p 2 > p 3 . p 1 and p 3 share common resource. p 2 is independent of p 1 and p 3 .              
  • 63.
    References Rensselaer PolytechnicInstitute http://www.cs.rpi.edu/academics/courses/fall04/os/c8/ University of east London Inside the Windows NT Scheduler, Part 1 Inside the Windows NT Scheduler, Part 2
  • 64.
    Reading Assignment Chapter6 CPU Scheduling in Operating System Concepts , Seventh Edition, by Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, John Wiley & Sons, Inc.