CPU SchedulingProf.PrasadSawantLecturer  MCAMACS College PuneOperating System Concepts Prasad Sawant
Alternating Sequence of CPU And I/O BurstsOperating System Concepts Prasad Sawant
CPU SchedulerSelects from among the processes in memory that are ready to execute, and allocates the CPU to one of them.CPU scheduling decisions may take place when a process:1.	Switches from running to waiting state.2.	Switches from running to ready state.3.	Switches from waiting to ready.4.	Terminates.Scheduling under 1 and 4 is nonpreemptive.All other scheduling is preemptive.Operating System Concepts Prasad Sawant
DispatcherDispatcher module gives control of the CPU to the process selected by the short-term scheduler.Operating System Concepts Prasad Sawant
Scheduling CriteriaCPU utilization – keep the CPU as busy as possibleThroughput– # of processes that complete their execution per time unitTurnaround time – amount of time to execute a particular processWaiting time – amount of time a process has been waiting in the ready queueOperating System Concepts Prasad Sawant
Keep  in mind Waiting  time (WT)=start time(ST)-Arrival time (AT)Finish time (FT)=start time(ST)+Burst  Time(BT)Total  turn around time(TOT)=Finish time(FT)-Arrival time (AT )Operating System Concepts Prasad Sawant
Optimization CriteriaMax CPU utilizationMax throughputMin turnaround time Min waiting time Min response timeOperating System Concepts Prasad Sawant
First-Come, First-Served (FCFS) SchedulingOperating System Concepts Prasad SawantSuppose that the processes arrive in the order: P1 , P2 , P3  scheduleschedule          0                 10           20                                       30 scheduleWaiting time for P1  = 0; P2  = 24; P3 = 27Average waiting time:  (0 + 24 + 27)/3 = 17P3  30   P2   27  P1                            24
First-Come, First-Served (FCFS) Scheduling Gantt ChartOperating System Concepts Prasad SawantSuppose that the processes arrive in the order: P1 , P2 , P3  scheduleschedulescheduleWaiting time for P1  = 0; P2  = 24; P3 = 27Average waiting time:  (0 + 24 + 27)/3 = 170              P1                                                      24P2     27P3     30
Exercise  Find Average waiting time for Suppose that the processes arrive in the order: P2 , P3 , P1Operating System Concepts Prasad Sawant
Shortest-Job-First (SJR) SchedulingAssociate with each process the length of its next CPU burst.  Use these lengths to schedule the process with the shortest time.Two schemes: nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst.preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt.  This scheme is know as the Shortest-Remaining-Time-First (SRTF).SJF is optimal – gives minimum average waiting time for a given set of processes.Operating System Concepts Prasad Sawant
Example of Non-Preemptive SJFOperating System Concepts Prasad Sawantscheduleschedulescheduleschedule0    P1                                           7P3      8P2      12P4       16Average waiting time = (0+6+3+7)/4=4
Example of Non-Preemptive SJFOperating System Concepts Prasad SawantP1                                           7P3      8P2      12P4       16Average waiting time = (0+6+3+7)/4=4 Average TOT =(7+10+4+11)/4=8
Preemptive SJFOperating System Concepts Prasad Sawant7Pschedule1010-1=9schedule11-1=0schedule1717-2=15schedule55-3=20 P1             1P2                        5         P4               10P1                  17P3                 26
Priority SchedulingOperating System Concepts Prasad SawantSchedule  66Schedule  00Schedule  1616Schedule  1818Schedule  1141/5=8.2 ms0      P2         1P5                          6P1                                             16P3             18P4    190
SJF Preemptive  Priority Scheduling Operating System Concepts Prasad SawantP5scheduled1919-3=16scheduled2424-1=23scheduled1212-3=9scheduled33-3=048/4=12 ms0      P1        3 P4                12  P3          19P1           24P2             30
Round Robin  Scheduling  Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds.  After this time has elapsed, the process is preempted and added to the end of the ready queue.If there are n processes in the ready queue and the time quantum is q, then each process gets 1/nof the CPU time in chunks of at most q time units at once.  No process waits more than (n-1)q time units.Operating System Concepts Prasad Sawant
Time Quantum and Context Switch Time Operating System Concepts Prasad Sawant00                                          1016          10923456789101
Round Robin  Time  Quantum 4Operating System Concepts Prasad Sawant24-4=2020-4=1616-4=1212-4=88-4=44-4=0scheduled30scheduled7scheduled100    P1  4  P2        7  P3        10  P1        14  P1        18  P1        22  P1        26  P1        30
Questions Define the difference between pre-emptive and non-pre-emptive scheduling. Explain the concept of a priority used in scheduling. Why is priority working usually chosen for real time processes?Define by difference between preemptive and non-emptive scheduling.Comment on the principle disadvantage of each of these scheduling methods: FCFS, SJF, RROperating System Concepts Prasad Sawant
Bibliography Operating System Principle-Peter Galvin Galvin  Prof.S.G.Lakhdive(Dept .Computer Sci )Prof.Ramkirshna More A.C.S College AkurdiMr. AbhishekNagar Web Administrator at SymbioisOperating System Concepts Prasad Sawant
Thanks  you Operating System Concepts Prasad Sawant

CPU Sheduling

  • 1.
    CPU SchedulingProf.PrasadSawantLecturer MCAMACS College PuneOperating System Concepts Prasad Sawant
  • 2.
    Alternating Sequence ofCPU And I/O BurstsOperating System Concepts Prasad Sawant
  • 3.
    CPU SchedulerSelects fromamong the processes in memory that are ready to execute, and allocates the CPU to one of them.CPU scheduling decisions may take place when a process:1. Switches from running to waiting state.2. Switches from running to ready state.3. Switches from waiting to ready.4. Terminates.Scheduling under 1 and 4 is nonpreemptive.All other scheduling is preemptive.Operating System Concepts Prasad Sawant
  • 4.
    DispatcherDispatcher module givescontrol of the CPU to the process selected by the short-term scheduler.Operating System Concepts Prasad Sawant
  • 5.
    Scheduling CriteriaCPU utilization– keep the CPU as busy as possibleThroughput– # of processes that complete their execution per time unitTurnaround time – amount of time to execute a particular processWaiting time – amount of time a process has been waiting in the ready queueOperating System Concepts Prasad Sawant
  • 6.
    Keep inmind Waiting time (WT)=start time(ST)-Arrival time (AT)Finish time (FT)=start time(ST)+Burst Time(BT)Total turn around time(TOT)=Finish time(FT)-Arrival time (AT )Operating System Concepts Prasad Sawant
  • 7.
    Optimization CriteriaMax CPUutilizationMax throughputMin turnaround time Min waiting time Min response timeOperating System Concepts Prasad Sawant
  • 8.
    First-Come, First-Served (FCFS)SchedulingOperating System Concepts Prasad SawantSuppose that the processes arrive in the order: P1 , P2 , P3 scheduleschedule 0 10 20 30 scheduleWaiting time for P1 = 0; P2 = 24; P3 = 27Average waiting time: (0 + 24 + 27)/3 = 17P3 30 P2 27 P1 24
  • 9.
    First-Come, First-Served (FCFS)Scheduling Gantt ChartOperating System Concepts Prasad SawantSuppose that the processes arrive in the order: P1 , P2 , P3 scheduleschedulescheduleWaiting time for P1 = 0; P2 = 24; P3 = 27Average waiting time: (0 + 24 + 27)/3 = 170 P1 24P2 27P3 30
  • 10.
    Exercise FindAverage waiting time for Suppose that the processes arrive in the order: P2 , P3 , P1Operating System Concepts Prasad Sawant
  • 11.
    Shortest-Job-First (SJR) SchedulingAssociatewith each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time.Two schemes: nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst.preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF).SJF is optimal – gives minimum average waiting time for a given set of processes.Operating System Concepts Prasad Sawant
  • 12.
    Example of Non-PreemptiveSJFOperating System Concepts Prasad Sawantscheduleschedulescheduleschedule0 P1 7P3 8P2 12P4 16Average waiting time = (0+6+3+7)/4=4
  • 13.
    Example of Non-PreemptiveSJFOperating System Concepts Prasad SawantP1 7P3 8P2 12P4 16Average waiting time = (0+6+3+7)/4=4 Average TOT =(7+10+4+11)/4=8
  • 14.
    Preemptive SJFOperating SystemConcepts Prasad Sawant7Pschedule1010-1=9schedule11-1=0schedule1717-2=15schedule55-3=20 P1 1P2 5 P4 10P1 17P3 26
  • 15.
    Priority SchedulingOperating SystemConcepts Prasad SawantSchedule 66Schedule 00Schedule 1616Schedule 1818Schedule 1141/5=8.2 ms0 P2 1P5 6P1 16P3 18P4 190
  • 16.
    SJF Preemptive Priority Scheduling Operating System Concepts Prasad SawantP5scheduled1919-3=16scheduled2424-1=23scheduled1212-3=9scheduled33-3=048/4=12 ms0 P1 3 P4 12 P3 19P1 24P2 30
  • 17.
    Round Robin Scheduling Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.If there are n processes in the ready queue and the time quantum is q, then each process gets 1/nof the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.Operating System Concepts Prasad Sawant
  • 18.
    Time Quantum andContext Switch Time Operating System Concepts Prasad Sawant00 1016 10923456789101
  • 19.
    Round Robin Time Quantum 4Operating System Concepts Prasad Sawant24-4=2020-4=1616-4=1212-4=88-4=44-4=0scheduled30scheduled7scheduled100 P1 4 P2 7 P3 10 P1 14 P1 18 P1 22 P1 26 P1 30
  • 20.
    Questions Define thedifference between pre-emptive and non-pre-emptive scheduling. Explain the concept of a priority used in scheduling. Why is priority working usually chosen for real time processes?Define by difference between preemptive and non-emptive scheduling.Comment on the principle disadvantage of each of these scheduling methods: FCFS, SJF, RROperating System Concepts Prasad Sawant
  • 21.
    Bibliography Operating SystemPrinciple-Peter Galvin Galvin Prof.S.G.Lakhdive(Dept .Computer Sci )Prof.Ramkirshna More A.C.S College AkurdiMr. AbhishekNagar Web Administrator at SymbioisOperating System Concepts Prasad Sawant
  • 22.
    Thanks youOperating System Concepts Prasad Sawant