Processes & CPU Scheduling

Embed Size (px)

Citation preview

  • 8/7/2019 Processes & CPU Scheduling

    1/27

    1

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 1

    Networks & Operating Systems

    Lecture 4: Processes & CPU Scheduling

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 2

    Last week

    Concepts introduced and discussed:

    System call

    API

    Parameters passing: Registers, Block, Stack

    System programs

    Modules and Layers Kernel

    Virtual Machine

    Boot loader, MBR

  • 8/7/2019 Processes & CPU Scheduling

    2/27

    2

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 3

    This week

    Processes

    Process Concept

    Process Scheduling

    Operations on Processes

    Cooperating Processes

    Inter-process communication

    CPU Scheduling

    Basic Concepts

    Scheduling Criteria

    Scheduling Algorithms

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 4

    Process Concept

    An operating systemexecutes a variety ofprograms:

    Batch system jobs

    Time-shared systems userprograms or tasks

    Textbook uses the termsjoband processalmostinterchangeably

    Process a program inexecution; process executionmust progress in sequentialfashion

    A process includes:

    program counter ,stack,data section

  • 8/7/2019 Processes & CPU Scheduling

    3/27

    3

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 5

    Process State

    As a process executes, it changes state

    new: The process is being created

    running: Instructions are being executed

    waiting: The process is waiting for some event to occur

    ready: The process is waiting to be assigned to a process

    terminated: The process has finished execution

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 6

    Process State Transition Diagram

  • 8/7/2019 Processes & CPU Scheduling

    4/27

    4

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 7

    Process Control Block (PCB)

    Structure in the OS representingInformation associated witheach process

    Process state (state diagram)

    Program counter

    CPU registers

    CPU scheduling information

    Memory-managementinformation

    Accounting information I/O status information

    Pointers to other data structuresin OS.

    Process table: logically contains aPCB for all current processes ina system

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 8

    CPU Switch From Process

    to Process

  • 8/7/2019 Processes & CPU Scheduling

    5/27

    5

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 9

    Process Scheduling Queues

    Job queue set of allprocesses in the system

    Ready queue set of allprocesses residing in mainmemory, ready and waitingto execute

    Device queues set ofprocesses waiting for an I/Odevice

    Processes migrate among

    the various queues

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 10

    Representation of Process

    Scheduling

  • 8/7/2019 Processes & CPU Scheduling

    6/27

    6

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 11

    In General

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 12

    Schedulers

    Long-term scheduler (or job scheduler) selects whichprocesses should be brought into the ready queue

    Short-term scheduler (or CPU scheduler or Dispatcher) selects which process should be executed next and allocatesCPU (i.e next after clock or IO interrupt, system call or signal)

    Addition of medium term scheduling (presents system withvirtual memory), swapping from memory to disk, vice versa,e.g low priority, or inactive processes.

  • 8/7/2019 Processes & CPU Scheduling

    7/27

    7

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 13

    Schedulers (Cont.)

    Short-term scheduler is invoked very frequently (milliseconds) (must be fast)

    Long-term scheduler is invoked very infrequently (seconds,minutes) (may be slow)

    The long-term scheduler controls the degree ofmultiprogramming

    Processes can be described as either:

    I/O-bound process spends more time doing I/O thancomputations, many short CPU bursts

    CPU-bound process spends more time doing computations; fewvery long CPU bursts

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 14

    Context Switch

    When CPU switches to another process, the system must savethe state of the old process and load the saved state for the newprocess

    Context-switch time is overhead; the system does no useful workwhile switching

    Time dependent on hardware support

    e.g Switching CPU betweentwo Process 1 and Process 2

  • 8/7/2019 Processes & CPU Scheduling

    8/27

    8

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 15

    Process Creation

    Parent process create children processes, which, in turn createother processes, forming a tree of processes

    Resource sharing

    Parent and children share all resources

    Children share subset of parents resources

    Parent and child share no resources

    Execution

    Parent and children execute concurrently

    Parent waits until children terminate

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 16

    Process Creation (Cont.)

    Address space

    Child is duplicate of parent (i.e child is an exact copy of parent)

    Child has a program loaded into it

    1. UNIX examples**

    fork system call creates new process .

    ~ A parent can continue and get SIGCHLD from children, parentmust have a handler to act on signal

    ~ Or parent cat wait for child and get exit code

    exec system call used after a fork to replace the process memoryspace with a new program, this is called overlaying

    ** http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html

  • 8/7/2019 Processes & CPU Scheduling

    9/27

    9

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 17

    exit code

    Parent, program A

    program B

    If a process wishes toregain control afterexecing a secondprogram, it should forka child process, havethe child exec thesecond program and theparent wait for the child

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 18

    Process Termination

    Process executes last statement and asks the operating systemto delete it (exit)

    Output data from child to parent (via wait)

    Process resources are deallocated by operating system

    Parent may terminate execution of children processes (abort)

    Child has exceeded allocated resources Task assigned to child is no longer required

    If parent is exiting

    ~ Some operating system do not allow child to continue if itsparent terminates

    All children terminated - cascading termination

  • 8/7/2019 Processes & CPU Scheduling

    10/27

    10

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 19

    A Tree of Processes

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 20

    Cooperating Processes

    Independent process cannot affect or be affected bythe execution of another process

    Cooperating process can affect or be affected by theexecution of another process

    Advantages of process cooperation Information sharing

    Computation speed-up

    Modularity

    Convenience

  • 8/7/2019 Processes & CPU Scheduling

    11/27

    11

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 21

    Processes & Sharing of Information

    kernel

    process process process process process processSharedmemory

    Sharedinfo

    filesystem

    e.g pipes,

    msg queues,

    semaphores

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 22

    Producer-Consumer Problem

    Paradigm for cooperatingprocesses, producerprocess producesinformation that is consumedby a consumerprocess

    unbounded-bufferplacesno practical limit on the sizeof the buffer

    bounded-bufferassumesthat there is a fixed buffersize

  • 8/7/2019 Processes & CPU Scheduling

    12/27

    12

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 23

    Bounded-Buffer Shared-

    Memory Solution

    Shared data

    #define BUFFER_SIZE 8

    Typedef struct {

    . . .

    } item;

    item buffer[BUFFER_SIZE];

    int in = 0;

    int out = 0;

    Solution is correct, but can only useBUFFER_SIZE-1 elements

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 24

    Bounded-Buffer Producer

    Process

    item nextProduced;

    while (1) {

    while (((in + 1) % BUFFER_SIZE) == out);

    /* do nothing */

    buffer[in] = nextProduced;

    in = (in + 1) % BUFFER_SIZE;

    }

  • 8/7/2019 Processes & CPU Scheduling

    13/27

    13

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 25

    Bounded-Buffer

    Consumer Process

    item nextConsumed;

    while (1) {

    while (in == out);

    /* do nothing */

    nextConsumed = buffer[out];

    out = (out + 1) % BUFFER_SIZE;

    }

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 26

    Inter-process

    Communication (IPC)

    Mechanism for processes to communicate and to synchronize theiractions

    Message system processes communicate with each other withoutresorting to shared variables

    IPC facility provides two operations: send(message) message size fixed or variable

    receive(message)

    If Pand Qwish to communicate, they need to: establish a communication linkbetween them

    exchange messages via send/receive

    Implementation of communication link physical (e.g., shared memory, hardware bus)

    logical (e.g., logical properties)

    Direct communication

    Indirect communication (via a shared mailbox)

  • 8/7/2019 Processes & CPU Scheduling

    14/27

    14

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 27

    Implementation Questions

    How are links established?

    Can a link be associated with more than two processes?

    How many links can there be between every pair ofcommunicating processes?

    What is the capacity of a link?

    Is the size of a message that the link can accommodate fixed orvariable?

    Is a link unidirectional or bi-directional?

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 28

    Direct Communication

    Processes must name each other explicitly:

    send (P, message) send a message to process P

    receive(Q, message) receive a message from process Q

    Properties of communication link

    Links are established automatically.

    A link is associated with exactly one pair of communicatingprocesses.

    Between each pair there exists exactly one link.

    The link may be unidirectional, but is usually bi-directional.

  • 8/7/2019 Processes & CPU Scheduling

    15/27

    15

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 29

    Indirect Communication

    Messages are directed and received from mailboxes(also referred to as ports).

    Each mailbox has a unique id.

    Processes can communicate only if they share a mailbox.

    Properties of communication link

    Link established only if processes share a common mailbox

    A link may be associated with many processes.

    Each pair of processes may share several communicationlinks.

    Link may be unidirectional or bi-directional.

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 30

    Indirect Communication

    (cont)

    Operations

    create a new mailbox

    send and receive messages through mailbox

    destroy a mailbox

    Primitives are defined as:send(A, message) send a message to mailbox A

    receive(A, message) receive a message from mailbox A

  • 8/7/2019 Processes & CPU Scheduling

    16/27

    16

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 31

    Indirect Communication

    (cont)

    Mailbox sharing

    P1, P2, and P3share mailbox A.

    P1, sends; P2and P3 receive.

    Who gets the message?

    Solutions

    Allow a link to be associated with at most two processes.

    Allow only one process at a time to execute a receive operation.

    Allow the system to select arbitrarily the receiver. Sender is notifiedwho the receiver was.

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 32

    Synchronization

    Message passing may be either blocking or non-blocking.

    Blocking is considered synchronous

    Non-blocking is considered asynchronous

    send and receive primitives may be either blocking or non-blocking.

  • 8/7/2019 Processes & CPU Scheduling

    17/27

    17

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 33

    Buffering

    Queue of messages attached to the link; implemented in one ofthree ways.

    1. Zero capacity 0 messagesSender must wait for receiver (rendezvous).

    2. Bounded capacity finite length of nmessagesSender must wait if link full.

    3. Unbounded capacity infinite lengthSender never waits.

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 34

    CPU Scheduling

    Basic Concepts

    Scheduling Criteria

    Scheduling Algorithms

  • 8/7/2019 Processes & CPU Scheduling

    18/27

    18

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 35

    Basic Concepts

    Maximum CPU utilizationobtained withmultiprogramming

    CPUI/O Burst Cycle Process execution consistsof a cycleof CPU executionand I/O wait

    CPU burst distribution:

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 36

    CPU Scheduler

    Selects from among the processes in memory that are ready toexecute, 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 non-preemptive

    All other scheduling is preemptive

  • 8/7/2019 Processes & CPU Scheduling

    19/27

    19

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 37

    Dispatcher

    Dispatcher module gives control of the CPU to the processselected by the short-term scheduler; this involves:

    switching context

    switching to user mode

    jumping to the proper location in the user program to restartthat program

    Dispatch latency time it takes for the dispatcher to stopone process and start another running

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 38

    Scheduling and

    Optimisation Criteria

    Scheduling criteria:

    CPU utilization keep theCPU as busy as possible

    Throughput # of processesthat complete theirexecution per time unit

    Turnaround time amountof time to execute aparticular process

    Waiting time amount oftime a process has beenwaiting in the ready queue

    Response time amount oftime it takes from when arequest was submitted untilthe first response isproduced, not output (fortime-sharing environment)

    Optimisation criteria

    Max CPU utilization

    Max throughput

    Min turnaround time

    Min waiting time

    Min response time

  • 8/7/2019 Processes & CPU Scheduling

    20/27

    20

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 39

    First-Come, First-Served

    (FCFS) Scheduling

    Suppose that the processes arrive in the order: P1, P2, P3Process Arrival Time Burst Time

    P1 0.0 24

    P2 1.0 3

    P3 2 .0 3

    The Gantt Chart for the schedule is:

    Waiting time for P1 = 0; P2 = 24 1 = 23; P3= 27 2 = 25

    Average waiting time: (0 + 23 + 25)/3 = 16

    P1 P2 P3

    24 27 300

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 40

    FCFS Scheduling (Cont.)

    Suppose that the processes arrive in the order: P2, P3 P1

    Process Arrival Time Burst Time

    P1 2.0 24

    P2 0.0 3

    P3 1.0 3

    The Gantt chart for the schedule is:

    Waiting time for P1 =6 - 2 = 4;P2= 0; P3=3 1 = 2

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

    Much better than previous case

    Convoy effectshort process behind long process

    P1P3P2

    63 300

  • 8/7/2019 Processes & CPU Scheduling

    21/27

    21

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 41

    Waiting time

    Pi is a process

    Turnaround Time(Pi) = T(Pi terminated) - T(Pi arrived)

    T(Pi waiting) = Turnaround Time (Pi) - t(CPU bursts for Pi) t(I/O bursts for Pi)

    Example :

    If T(P3 CPU Bursts) =68; T(P3 arrived)=20, then

    T(P3 waiting) = 162 - 20 - 68 - 0 = 74

    P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

    0 20 37 57 77 97 117 121 134 154 162

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 42

    Shortest-Job-First (SJR)

    Scheduling

    Associate 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 bepreempted until completes its CPU burst

    preemptive if a new process arrives with CPU burst length less

    than remaining time of current executing process, preempt. Thisscheme is know as theShortest-Remaining-Time-First (SRTF)

    SJF is optimal gives minimum average waiting time for a givenset of processes

  • 8/7/2019 Processes & CPU Scheduling

    22/27

    22

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 43

    Example of

    Non-Preemptive SJF

    Process Arrival Time Burst Time

    P1 0.0 7

    P2 2.0 4

    P3 4.0 1

    P4 5.0 4

    SJF (non-preemptive)

    Average waiting time = ((7-0-7)+(12-2-4)+(8-4-1)+(16-5-4))/4 = 4

    P1 P3 P2

    73 160

    P4

    8 12

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 44

    Example of Preemptive SJF

    Process Arrival Time Burst Time

    P1 0.0 7

    P2 2.0 4

    P3 4.0 1

    P4 5.0 4

    SJF (preemptive)

    Average waiting time = ((16-7) + (7-2-4) + (5-4-1) +(11-5-4))/4 = 3

    P1 P3P2

    42 110

    P4

    5 7

    P2 P1

    16

  • 8/7/2019 Processes & CPU Scheduling

    23/27

    23

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 45

    Determining Length of

    Next CPU Burst

    Can only estimate the length

    Can be done by using the length of previous CPU bursts, usingexponential averaging:

    :Define

    10,

    burstCPUnextfor thevaluepredicted

    burstCPUoflenghtactual

    1

    =

    =

    +

    n

    th

    nnt

    ( ) .11 nnn t +=+ = 0

    n+1 = n Recent history does not count

    = 1

    n+1 = tn Only the actual last CPU burst counts

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 46

    Examples of Exponential

    Averaging (cntd)

    1 2 3 4 5 6 7 8

    4

    6

    8

    10

    12

    Time (t)

    CPUB

    urst

    i+1 = ti + (1 ) itii, = 0.25

    i, = 0.5i, = 0.75

  • 8/7/2019 Processes & CPU Scheduling

    24/27

    24

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 47

    Priority Scheduling

    A priority number (integer) is associated with each process

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

    Preemptive

    nonpreemptive

    SJF is a priority scheduling where priority is the predicted nextCPU burst time

    Problem Starvation low priority processes may neverexecute

    Solution Aging as time progresses increase the priority of theprocess

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 48

    Round Robin (RR)

    Each process gets a small unit of CPU time (timequantum), usually 10-100 milliseconds. After this timehas elapsed, the process is preempted and added to theend of the ready queue.

    If there are nprocesses in the ready queue and the timequantum is q, then each process gets 1/nof the CPUtime in chunks of at most qtime units at once. No

    process waits more than (n-1)qtime units. Performance

    qlarge FCFS

    qsmall qmust be large with respect to context switch,otherwise overhead is too high

  • 8/7/2019 Processes & CPU Scheduling

    25/27

    25

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 49

    Example of RR

    with Time Quantum = 20

    Process Burst Time

    P1 53

    P2 17

    P3 68

    P4 24

    The Gantt chart is:

    Typically, higher average turnaround than SJF, but betterresponse

    P1

    P2

    P3

    P4

    P1

    P3

    P4

    P1

    P3

    P3

    0 20 37 57 77 97 117 121 134 154 162

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 50

    Time Quantum and

    Context Switch Time

  • 8/7/2019 Processes & CPU Scheduling

    26/27

    26

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 51

    Turnaround Time Varies With

    The Time Quantum

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 52

    Multilevel Queue

    Ready queue is partitioned intoseparate queues:

    foreground (interactive) background (batch)

    Each queue has its own schedulingalgorithm

    foreground RR

    background FCFS Scheduling must be done between

    the queues

    Fixed priority scheduling; (i.e., serveall from foreground then from

    background). Possibility of

    starvation.

    Time slice each queue gets acertain amount of CPU time which it

    can schedule amongst its processes;

    i.e., 80% to foreground in RR

    20% to background in FCFS

  • 8/7/2019 Processes & CPU Scheduling

    27/27

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 53

    Summary

    Concepts introduced anddiscussed:

    PCB, context switch

    fork(), exec(), exit()

    Producer Consumerproblem

    IPC

    ~ Direct IPC

    ~ Indirect IPC

    CPU burst, I/O burst

    Dispatch latency

    Throughput, Turnaroundtime, Waiting time,Responsetime

    Preemptive and Non-preemptive scheduling

    FCFS, SJF, PriorityScheduling, RR

    Exponential averaging

    Starvation and Aging

    Multilevel queue

    What to read:

    These notes

    OSC6:

    ~ 4.3 - 5,

    ~ 6.1 - 3

    Networks and Operating Systems

    Based on the slides provided by John Wiley and Sons Inc 54

    Questions / Exercises

    What are the states of anyprocess? How does theprocess switch its state?

    What information is providedby the process control block(PCB)?

    How does the CPU switchfrom process to process?

    What are the schedulingqueues for?

    Explain the differencebetween short-term,medium-term and long-termschedulers

    What is direct IPC? Howdoes it work?

    What is indirect IPC? Howdoes it work?

    Reconstruct the calculationsresulting in the figure on slide38

    What are starvation andaging in CPU scheduling?

    What is the multilevelqueue? What is it for?