CPU scheduling1

download CPU scheduling1

of 38

Transcript of CPU scheduling1

  • 8/8/2019 CPU scheduling1

    1/38

    CPU SCHEDULING

  • 8/8/2019 CPU scheduling1

    2/38

    PROCESS CONCEPT

    An operating system executes a variety of

    programs:

    y Batch system jobs

    y Time-shared systems user programs or tasks

    Process a program in execution; processexecution must progress in sequential fashion.

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    3/38

    PROCESS STATE

    As a process executes, it changes statey new: The process is being created.

    y running: Instructions are being executed.

    y waiting: The process is waiting for some event to

    occur.

    y r

    eady: The process is waiting to be assigned to aprocess.

    y terminated: The process has finished execution.

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    4/38

    PROCESS CONTROL BLOCK(PCB)Information associated with each process.

    Process state

    Program counter

    CPU registers

    CPU scheduling information

    Memory-management information

    Accounting information

    I/O status information

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    5/38

    PROCESS SCHEDULING QUEUES Job queue set of all PCBs in the system.

    Ready queue set of all processes residing in mainmemory, ready and waiting to execute.

    Device queues set of processes waiting for an I/Odevice.

    PCB migration between the various queues.

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    6/38

    READYQUEUEANDVARIOUS I/O DEVICE QUEUES

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    7/38

    REPRESENTATION OF PROCESS

    SCHEDULING

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    8/38

  • 8/8/2019 CPU scheduling1

    9/38

    CPU SWITCH FROM PROCESS TO PROCESS

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    10/38

    Long term scheduling: which determines whichprograms are admitted to the system for execution andwhen, and which ones should be exited.(disk tomemory)

    Medium term scheduling: which determines whenprocesses are to be suspended and resumed;

    Short term scheduling (or dispatching): whichdetermines which of the ready processes can have CPU

    resources, and for how long.(disk to CPU)

  • 8/8/2019 CPU scheduling1

    11/38

    DISPATCHER Dispatcher module gives control of the CPU to

    the process selected by the CPU scheduler.

    This involves:

    y Switching context

    y Switching to user modey Setting the PC

    Dispatch latency is the time taken to stop one

    process and start another

    Operating System Concepts

  • 8/8/2019 CPU scheduling1

    12/38

    CPU SCHEDULING Basic Concepts

    Scheduling Criteria

    Scheduling Algorithms

    Thread Scheduling

    Multiple-Processor Scheduling

    Operating Systems Examples

    Algorithm Evaluation

  • 8/8/2019 CPU scheduling1

    13/38

    OBJECTIVES

    To introduce CPU scheduling, which is the basis

    for multiprogrammed operating systems

    To describe various CPU-scheduling algorithms

    To discuss evaluation criteria for selecting aCPU-scheduling algorithm for a particular

    system

  • 8/8/2019 CPU scheduling1

    14/38

    CPU scheduling determines which process to run

    when there are multiple runnable processes

    CPU scheduling is important because it can have

    big effect on resource utilization and overallperformance of the system

  • 8/8/2019 CPU scheduling1

    15/38

  • 8/8/2019 CPU scheduling1

    16/38

    ALTERNATING SEQUENCE OF CPU AND I/O

    BURSTS

  • 8/8/2019 CPU scheduling1

    17/38

    CPU SCHEDULER

    Selects 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 aprocess:

    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

  • 8/8/2019 CPU scheduling1

    18/38

  • 8/8/2019 CPU scheduling1

    19/38

    SCHEDULING CRITERIA CPUutilization keep the CPU as busy as

    possible

    Throughput # of processes that complete

    their execution per time unit

    Turnaround time amount of time to executea particular process

    Waiting time amount of time a process has

    been waiting in the ready queue

    Response time amount of time it takes fromwhen a request was submitted until the first

    response is produced, not output (for time-

    sharing environment)

  • 8/8/2019 CPU scheduling1

    20/38

    SCHEDULINGALGORITHM OPTIMIZATION

    CRITERIA

    Max CPU utilization

    Max throughput

    Min turnaround time

    Min waiting time Min response time

  • 8/8/2019 CPU scheduling1

    21/38

    FIRST-COME, FIRST-SERVED (FCFS) SCHEDULING

    Process Burst Time

    P1 24

    P2 3

    P3 3

    Suppose that the processes arrive in the order: P1, P2 , P3The Gantt Chart for the schedule is:

    Waiting time for P1 = 0; P2 = 24; P3= 27

    Average waiting time: (0 + 24 + 27)/3 = 17

    P1 P2 P3

    24 27 300

  • 8/8/2019 CPU scheduling1

    22/38

    FCFS SCHEDULING (CONT)

    Suppose that the processes arrive in the order

    P2 , P3 , P1

    The Gantt chart for the schedule is:

    Waiting time for P1=6;P2= 0; P3=3

    Average waiting time: (6 + 0 + 3)/3 = 3

    Much better than previous case

    Convoy effect short process behind long process

    P1P3P2

    63 300

  • 8/8/2019 CPU scheduling1

    23/38

    SHORTEST-JOB-FIRST (SJF) SCHEDULING

    Associate with each process the length of its next

    CPU burst. Use these lengths to schedule the

    process with the shortest time

    SJF is optimal gives minimum average waitingtime for a given set of processes

    y The difficulty is knowing the length of the next CPU

    request

  • 8/8/2019 CPU scheduling1

    24/38

  • 8/8/2019 CPU scheduling1

    25/38

    BURST

    Can only estimate the length

    Can be done by using the length of previous CPU

    bursts, using exponential averaging

    :Define4.

    10,3.burstCPUnexttheforvaluepredicted2.

    burstCPUoflengthactual1.

    ee

    !

    !

    EE

    X 1n

    th

    n nt

    .11 nnn

    t XEEX !!

  • 8/8/2019 CPU scheduling1

    26/38

    EXAMPLES OF EXPONENTIALAVERAGING

    E =0

    y Xn+1 =Xny Recent history does not count

    E =1

    y Xn+1 =E tny Only the actual last CPU burst counts

    If we expand the formula, we get:Xn+1 =E tn+(1 - E)E tn -1 +

    +(1 - E )jE tn -j

    +

    +(1 - E )n +1 X0

    Since both E and (1 - E) are less than or equal to1, each successive term has less weight than itspredecessor

  • 8/8/2019 CPU scheduling1

    27/38

  • 8/8/2019 CPU scheduling1

    28/38

    ROUND ROBIN (RR)

    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/n of the CPU time in chunks of at most

    q time units at once. No process waits more

    than (n-1)q time units.

    Performance

    y q large FIFO

    y q small q must be large with respect to context

    switch, otherwise overhead is too high

  • 8/8/2019 CPU scheduling1

    29/38

    EXAMPLE OF RR WITH TIME QUANTUM = 4

    Process Burst Time

    P1 24

    P2 3

    P3 3

    The Gantt chart is:

    Typically, higher average turnaround than SJF,

    but better response

    P1 P2 P3 P1 P1 P1 P1 P1

    0 4 7 10 14 18 22 26 30

  • 8/8/2019 CPU scheduling1

    30/38

    TIME UANTUM AND ONTEXT WITCH

    TIME

  • 8/8/2019 CPU scheduling1

    31/38

    MULTILEVEL QUEUE

    Ready queue is partitioned into separate queues:

    foreground (interactive)

    background (batch)

    Each queue has its own scheduling algorithmy foreground RR

    y background FCFS

    Scheduling must be done between the queues

    y Fixed priority scheduling; (i.e., serve all from

    foreground then from background). Possibility of

    starvation.

    y Time slice each queue gets a certain amount of

    CPU time which it can schedule amongst its

    processes; i.e., 80% to foreground in RR

    y 20% to background in FCFS

  • 8/8/2019 CPU scheduling1

    32/38

    Batch system : In batch systems,typically want

    good throughput or trunaround time.

    Intractive system: In this systems both of this are

    important but response time is given primaryconsideration

  • 8/8/2019 CPU scheduling1

    33/38

    MULTILEVEL QUEUE SCHEDULING

  • 8/8/2019 CPU scheduling1

    34/38

    MULTILEVEL FEEDBACKQUEUE

    A process can move between the various queues;

    aging can be implemented this way

    Multilevel-feedback-queue scheduler defined by

    the following parameters:

    y number of queues

    y scheduling algorithms for each queue

    y method used to determine when to upgrade a

    process

    y

    method used to determine when to demote a processy method used to determine which queue a process

    will enter when that process needs service

    E M F

  • 8/8/2019 CPU scheduling1

    35/38

    EXAMPLE OF MULTILEVEL FEEDBACK

    QUEUE

    Three queues:

    y Q0 RR with time quantum 8 milliseconds

    y Q1 RR time quantum 16 milliseconds

    y Q2 FCFS

    Scheduling

    y 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.y At Q1job is again served FCFS and receives 16

    additional milliseconds. If it still does not complete,

    it is preempted and moved to queue Q2.

  • 8/8/2019 CPU scheduling1

    36/38

    MULTILEVEL FEEDBACKQUEUES

  • 8/8/2019 CPU scheduling1

    37/38

  • 8/8/2019 CPU scheduling1

    38/38

    END