Operating Systems -ch6-F06

Embed Size (px)

Citation preview

  • 8/2/2019 Operating Systems -ch6-F06

    1/47

    Chapter 6 Operating Systems 1

    Chapter 6: CPU Scheduling

    Basic Concepts Scheduling Criteria

    Scheduling Algorithms

    Multiple-Processor Scheduling

    Real-Time Scheduling

    Algorithm Evaluation

  • 8/2/2019 Operating Systems -ch6-F06

    2/47

    Chapter 6 Operating Systems 2

    Basic Concepts

    Maximum CPU utilization obtained withmultiprogramming

    CPUI/O Burst Cycle Process executionconsists of a cycle of CPU execution and I/O

    wait. CPU burst distribution

    An I/O bound program typically has manyvery short CPU bursts

    A CPU bound program typically have a fewvery long CPU bursts

  • 8/2/2019 Operating Systems -ch6-F06

    3/47

    Chapter 6 Operating Systems 3

    Alternating Sequence of CPU And I/O Bursts

    Histogram of CPU-burst Times

  • 8/2/2019 Operating Systems -ch6-F06

    4/47

    Chapter 6 Operating Systems 4

    CPU Scheduler

    Selects from among the processes in memory that are readyto 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 state.

    4. Terminates.

    Scheduling under 1 and 4 is nonpreemptive (you dont haveoption)

    A new process must be selected for execution under 1 and 4

    Under 2 and 3, we can:

    Allow the running process to continue: nonpreemptive. Perform scheduling:preemptive.

    Preemptive scheduling can lead to problems: what if aprocess is modifying shared data when it is preempted?

    Solutions in Chapter 7

  • 8/2/2019 Operating Systems -ch6-F06

    5/47

    Chapter 6 Operating Systems 5

    Dispatcher

    Dispatcher module gives control of the CPU tothe process selected by the short-termscheduler; this involves:

    switching context

    switching to user mode jumping to the proper location in the user

    program to restart that program

    Dispatch latency time it takes for the

    dispatcher to stop one process and startanother running process.

  • 8/2/2019 Operating Systems -ch6-F06

    6/47

    Chapter 6 Operating Systems 6

    Dispatch Latency

    Conflict phase:

    1. Preemption of any processes running in the kernel

    2. Low-priority processes release resources needed by the high-priority processes

  • 8/2/2019 Operating Systems -ch6-F06

    7/47

    Chapter 6 Operating Systems 7

    Scheduling Criteria

    CPU utilization: % of time CPU is busy

    Throughput: # of processes completed per time unit

    Depend on process length

    Turnaround time: total time elapsed between process

    creation and termination Include: waiting to get into memory, waiting in the

    ready queue, executing on the CPU, and doing I/O

    Waiting time: time spent in the ready queue

    Affected by CPU scheduling algorithm

    Response time: time between process creation and firstresponse (not final output)

    Important for interaction (for time-sharingenvironment)

  • 8/2/2019 Operating Systems -ch6-F06

    8/47

    Chapter 6 Operating Systems 8

    Optimization Criteria

    Want maximize CPU utilization and throughputWant minimize turnaround time, waiting time,

    and response time

    Usually, want optimize the average measure

    Sometimes, want to optimize MIN or MAXvalues

    E.g. minimize the MAX response time

    For interactive systems, want minimize

    variance in response time

    Lead to predictability

  • 8/2/2019 Operating Systems -ch6-F06

    9/47

    Chapter 6 Operating Systems 9

    CPU Scheduling Algorithms

    1. FCFS scheduling

    2. Shortest-Job-First (SJR) Scheduling

    3. Priority Scheduling

    4. Round Robin Scheduling

    5. Multilevel Priority

  • 8/2/2019 Operating Systems -ch6-F06

    10/47

    Chapter 6 Operating Systems 10

    1. First-Come, First-Served (FCFS) Scheduling

    Process Burst TimeP1 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/2/2019 Operating Systems -ch6-F06

    11/47

    Chapter 6 Operating Systems 11

    FCFS Scheduling (Cont.)

    Suppose that the processes arrive in the orderP2 , 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 effectshort process behind long process

    P1P

    3P

    2

    63 300

  • 8/2/2019 Operating Systems -ch6-F06

    12/47

    Chapter 6 Operating Systems 12

    2. Shortest-Job-First (SJR) Scheduling

    Associate with each process the length of its nextCPU burst. Use these lengths to schedule theprocess with the shortest time first.

    Two schemes:

    nonpreemptive once CPU given to the process

    it cannot be preempted until completes its CPUburst.

    preemptive if a new process arrives with CPUburst length less than remaining time of current

    executing process, preempt. This scheme isknow as the Shortest-Remaining-Time-First(SRTF).

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

  • 8/2/2019 Operating Systems -ch6-F06

    13/47

    Chapter 6 Operating Systems 13

    Process Arrival Time Burst TimeP1 0.0 7

    P2 2.0 4

    P3 4.0 1

    P4 5.0 4

    SJF (non-preemptive)

    Average waiting time = (0 + 3 + 6 + 7)/4 = 4

    Example of Non-Preemptive SJF

    P1 P3 P2

    73 160

    P4

    8 12

  • 8/2/2019 Operating Systems -ch6-F06

    14/47

    Chapter 6 Operating Systems 14

    Example of Preemptive SJF

    Process Arrival Time Burst TimeP1 0.0 7

    P2 2.0 4

    P3 4.0 1

    P4 5.0 4 SJF (preemptive)

    Average waiting time = (9 + 1 + 0 +2)/4 = 3

    P1 P3P2

    42 110

    P4

    5 7

    P2 P1

    16

  • 8/2/2019 Operating Systems -ch6-F06

    15/47

    Chapter 6 Operating Systems 15

    Determining Length of Next CPU Burst

    There is no way to know the length of the nextCPU burst.

    Can only estimate the length.

    Can be done by using the length of previous

    CPU bursts, using exponential averaging.

    :Define4.10,3.

    burstCPUnexttheforvaluepredicted2.

    burstCPUoflenghtactual1.

    1n

    th

    nnt

    ( ) .tnnn+

    - 11

  • 8/2/2019 Operating Systems -ch6-F06

    16/47

    Chapter 6 Operating Systems 16

    Prediction of the Length of the Next CPU Burst

    =0.5

    0=10

  • 8/2/2019 Operating Systems -ch6-F06

    17/47

    Chapter 6 Operating Systems 17

    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 Since both and (1 - ) are less than or equal to 1,

    each successive term has less weight than itspredecessor.

  • 8/2/2019 Operating Systems -ch6-F06

    18/47

    Chapter 6 Operating Systems 18

    3. Priority Scheduling

    A priority number (integer) is associated with eachprocess

    The CPU is allocated to the process with the highestpriority (smallest integer highest priority).

    Preemptive (what information should be available ?)

    nonpreemptive

    SJF is a priority scheduling where priority is thepredicted next CPU burst time.

    Problem Starvation low priority processes may

    never execute. Solution Aging as time progresses increase the

    priority of the process.

  • 8/2/2019 Operating Systems -ch6-F06

    19/47

    Chapter 6 Operating Systems 19

    4. Round Robin (RR)

    Suitable for time sharing systems.

    Each process gets a small unit of CPUtime (time quantum ortime slice),usually 10-100 milliseconds. After thistime has elapsed, the process ispreempted and added to the end of theready queue.

    If there are n processes in the readyqueue and the time quantum is q, theneach process gets 1/n of the CPU timein chunks of at most q time units atonce. No process waits more than (n-1)q time units.

    What will happen after the time sliceexpires ?

    Is RR preemptive or nonpreemptive ?

    Performance q large FIFO q small q must be large with

    respect to context switch,otherwise overhead is too high.

  • 8/2/2019 Operating Systems -ch6-F06

    20/47

    Chapter 6 Operating Systems 20

    Time Quantum and Context Switch Time

    Minimize overhead (context swaps). Processor sharing: make time slice asminimum as possible

    Selecting the suitable time quantum is a trade off.

  • 8/2/2019 Operating Systems -ch6-F06

    21/47

    Chapter 6 Operating Systems 21

    Turnaround Time Varies With The Time Quantum

  • 8/2/2019 Operating Systems -ch6-F06

    22/47

    Chapter 6 Operating Systems 22

    Example of RR with Time Quantum = 20

    Process Burst TimeP1 53

    P2 17

    P3 68

    P4 24

    The Gantt chart is:

    Typically, higher average turnaround thanSJF, but better response why?.

    P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

    0 20 37 57 77 97 117 121 134 154 162

  • 8/2/2019 Operating Systems -ch6-F06

    23/47

    Chapter 6 Operating Systems 23

    5. Multilevel Queue

    Ready queue is partitioned into separate queues:foreground (interactive)background (batch)

    Each queue has its own scheduling algorithm based on thetype of the processes that is allocated to that queue,

    foreground RRbackground FCFS

    Scheduling must be done between the queues.

    Fixed priority scheduling; (i.e., serve all fromforeground then from background). Possibility ofstarvation.

    Time slice each queue gets a certain amount of CPUtime which it can schedule amongst its processes; i.e.,80% to foreground in RR, 20% to background in FCFS

  • 8/2/2019 Operating Systems -ch6-F06

    24/47

    Chapter 6 Operating Systems 24

    Multilevel Feedback Queue

    A process can move between the various queues; agingcan be implemented this way.

    Attacks both efficiency and response time problems.

    Multilevel-feedback-queue scheduler defined by thefollowing parameters:

    number of queues

    scheduling algorithms for each queue

    method used to determine when to upgrade aprocess

    method used to determine when to demote aprocess

    method used to determine which queue a processwill enter when that process needs service

  • 8/2/2019 Operating Systems -ch6-F06

    25/47

    Chapter 6 Operating Systems 25

    Multilevel Queue Scheduling

    Can cause starvation why? How to solve this ?

  • 8/2/2019 Operating Systems -ch6-F06

    26/47

    Chapter 6 Operating Systems 26

    Multilevel Feedback Queues

  • 8/2/2019 Operating Systems -ch6-F06

    27/47

    Chapter 6 Operating Systems 27

    Example of Multilevel Feedback Queue

    Three queues:

    Q0 time quantum 8 milliseconds

    Q1 time quantum 16 milliseconds

    Q2 FCFS

    Scheduling

    A new job enters queue Q0which is

    servedFCFS. When it gains CPU, jobreceives 8 milliseconds. If it doesnot finish in 8 milliseconds, job ismoved to queue Q1.

    At Q1 job is again served FCFS andreceives 16 additional milliseconds.

    If it still does not complete, it ispreempted and moved to queue Q2.

    Usually, processes in Q1 are notserved until Q0 is empty, and Q2processes are not served until Q0and Q1 are empty

  • 8/2/2019 Operating Systems -ch6-F06

    28/47

    Chapter 6 Operating Systems 28

    Multiple-Processor Scheduling

    CPU scheduling more complex when multiple CPUs areavailable (but load sharing is possible)

    Assume homogeneous systems (i.e., the processors areidentical)

    Use a common ready queue

    Two scheduling approaches: Symmetric: Each processor is self-scheduling: each processor

    selects a process to execute from the common ready queue

    Must synchronize the processors to ensure two processorsdo not choose the same process and processes are not lost

    from queue

    Asymmetric: Appoint one processor as scheduler for the otherprocessors

    Data sharing problem solved: only one processor accessesthe queue.

  • 8/2/2019 Operating Systems -ch6-F06

    29/47

    Chapter 6 Operating Systems 29

    Real-Time Scheduling

    Hard real-time systems required to complete acritical task within a guaranteed amount of time.

    Soft real-time computing requires that criticalprocesses receive priority over less fortunateones.

    Priority Inversion Problem: A lower priorityprocess is blocking a higher priority processfrom running.

    Solution: Priority Inheritance protocol

  • 8/2/2019 Operating Systems -ch6-F06

    30/47

    Chapter 6 Operating Systems 30

    Real-Time Scheduling

    Hard real-time systems: required to complete acritical task within a guaranteed amount of time

    A process is submitted with a time requirementfor completion or performing I/O

    Scheduler either admits the process with

    guarantee that the deadline will be met orrejects the request

    Scheduler must know how long each OS functiontakes

    Cant use secondary storage or virtualmemory

    Use special purpose dedicated hardware

  • 8/2/2019 Operating Systems -ch6-F06

    31/47

    Chapter 6 Operating Systems 31

    Real-Time Scheduling

    Soft real-time systems: requires that critical processesalways have top priority, may lead to long delays orstarvation of other processes (less restrictive than hard realtime)

    Implementation issues:

    1- Must use priority scheduling, real-time processes musthave highest priority, need disallow process aging

    2- Dispatch latency must be small

    Problem: many OS do not allow context switch duringa system call (or even an IO), thus the dispatching will

    be delayed leading to high dispatch latency Solution: allow system calls to be preemptible

  • 8/2/2019 Operating Systems -ch6-F06

    32/47

    Chapter 6 Operating Systems 32

    Modified slides link

    http://www.2shared.com/file/12237657/7aee6df4/ch456term_project.html

    Exam 8-4 from 3:30 to 4:30 ch 1-7

    http://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.htmlhttp://www.2shared.com/file/12237657/7aee6df4/ch456term_project.html
  • 8/2/2019 Operating Systems -ch6-F06

    33/47

    Chapter 6 Operating Systems 33

    Algorithm Evaluation

    How do we select a CPU-scheduling algorithmfor a particular system?

    1.Define desired criteria (e.g., maximize CPUutilization with the constraint that maxresponse time < 1 second)

    2.Evaluate/compare algorithms

    Four algorithm evaluation methods

    1.Deterministic modeling

    2.Queuing models3.Simulations

    4.Implementation

  • 8/2/2019 Operating Systems -ch6-F06

    34/47

    Chapter 6 Operating Systems 34

    Deterministic Modeling

    Use a specific predetermined workload anddetermines the performance of each algorithmfor that workload

    What we did in class

    Simple and fast

    Results only valid for specific workload

  • 8/2/2019 Operating Systems -ch6-F06

    35/47

    Chapter 6 Operating Systems 35

    Queuing Models

    The computer system is viewed as a network ofservers, each sever has a queue of waitingprocesses.

    E.g., CPU with ready queue, I/O device withdevice queue

    Given arrival rates and service rates, we cancompute average queue length, average waitingtime, etc. (called queuing-network analysis)

    Requires inaccurate/unrealistic assumptions fornumerical solutions

    E.g. assume processes are independent, arrivalrate follows Poisson distribution, service timefollows exponential distribution

    Queuing models are only an approximation of a realsystem

  • 8/2/2019 Operating Systems -ch6-F06

    36/47

    Chapter 6 Operating Systems 36

    Simulations

    Two ways to generate the data to drive thesimulation

    1.Generate workload according to certainprobability distributions

    2.Use trace tapes that record the sequence ofactual events in the real system

    Gather statistics of the algorithm performanceas the simulation executes

    Non-trivial to design, code, and debug asimulator

    Simulations often require many hours of CPUtime

  • 8/2/2019 Operating Systems -ch6-F06

    37/47

    Chapter 6 Operating Systems 37

    Evaluation of CPU Schedulers by Simulation

  • 8/2/2019 Operating Systems -ch6-F06

    38/47

    Chapter 6 Operating Systems 38

    Implementation

    Implement the scheduling algorithm on a realsystem

    The accurate evaluation technique.

    Cost is tremendous: coding algorithm,modifying kernel data structures, system must

    be taken down Ideally, we want flexible scheduling algorithms

    During OS build-time, boot time, or runtime, variables used by the scheduler can be

    changed by system manager or by user Few operating systems allow tunable

    scheduling. can you think of an example oftuning ?

  • 8/2/2019 Operating Systems -ch6-F06

    39/47

    Chapter 6 Operating Systems 39

    Scheduling in Practice

    Solaris 2 Windows 2000

  • 8/2/2019 Operating Systems -ch6-F06

    40/47

    Chapter 6 Operating Systems 40

    Solaris 2 Scheduling

    Priority based, four classes of scheduling: real-time,system, time sharing, and interactive

    Each class includes a set of priorities

    Default scheduling class for a process is time sharing

    Time sharing: use multilevel feedback queue, the higher

    the priority, the smaller the time slice (inverse relation). Interactive: same as time sharing

    Systems: for kernel processes only

    Priority is fixed, no time-slicing. Thread runs until itblocks or is preempted by a higher priority thread

    Real-time: highest priority (guaranteed response time) In this model, when two processes have the same priority

    number, use RR.

  • 8/2/2019 Operating Systems -ch6-F06

    41/47

    Chapter 6 Operating Systems 41

    Solaris 2 Scheduling

  • 8/2/2019 Operating Systems -ch6-F06

    42/47

    Chapter 6 Operating Systems 42

    Windows 2000 Scheduling

    Priority-based, preemptive scheduling 32 levels of priority, large integer = high priority

    Variable class contains threads having priority from 1 to 15 andreal-time class contains threads having priority from 16 to 31

    Ready queue for each scheduling priority, RR scheduling

    Dispatcher traverses the priority queue from highest to lowestpriorities and select the first process that is ready to bescheduled.

    If no ready process the dispatcher executes Idle process

  • 8/2/2019 Operating Systems -ch6-F06

    43/47

    Chapter 6 Operating Systems 43

    Windows 2000 Scheduling

    There is a relationship between the numeric priority of the windows2000kernel and the WIN32 API

    WIN32 identifies six priority classes: real-time, high, above normal,normal, below normal, idle.

    Except real-time, all priority classes are variable class priority.

    Seven levels of relative priority for each one of the above six classes

    An integer priority is assigned to a thread based on the (class, relativepriority) pair

    Relative priority may be raised or lowered

    Lowered when a threads time quantum runs out

    Increased when a thread is released from a wait operation

    => Give good response time to interactive threads

    When a process moves into the foreground, increase time quantum bysome factor (typically by 3)

  • 8/2/2019 Operating Systems -ch6-F06

    44/47

    Chapter 6 Operating Systems 44

    Windows 2000 Priorities

    Priority-based and preemptive scheduling

    32 Priority levels with two classes:

    Variable class: 1-15

    Real-time class: 16-31

    Relative

    Priority

    Value of each priority class

  • 8/2/2019 Operating Systems -ch6-F06

    45/47

    Chapter 6 Operating Systems 45

    Thread Scheduling

    Local Scheduling How the threads library decides whichthread to put onto an available LWP

    Global Scheduling How the kernel decides which kernelthread to run next

  • 8/2/2019 Operating Systems -ch6-F06

    46/47

    Chapter 6 Operating Systems 46

    Pthread Scheduling API

    #include

    #include #define NUM THREADS 5int main(int argc, char *argv[]){

    int i;pthread t tid[NUM THREADS];

    pthread attr t attr;/* get the default attributes */pthread attr init(&attr);

    /* set the scheduling algorithm to PROCESS or SYSTEM */pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);

    /* set the scheduling policy - FIFO, RT, or OTHER */

    pthread attr setschedpolicy(&attr, SCHED OTHER);/* create the threads */for (i = 0; i < NUM THREADS; i++)

    pthread create(&tid[i],&attr,runner,NULL);

  • 8/2/2019 Operating Systems -ch6-F06

    47/47

    Pthread Scheduling API

    /* now join on each thread */

    for (i = 0; i < NUM THREADS; i++)

    pthread join(tid[i], NULL);

    }

    /* Each thread will begin control in this function */void *runner(void *param)

    {

    printf("I am a thread\n");

    pthread exit(0);}