Lecture 8 Process Management

Embed Size (px)

Citation preview

  • 8/2/2019 Lecture 8 Process Management

    1/20

    Process management

    Information maintained by OS for processmanagement

    process context process control block

    OS virtualization of CPU for each process. Context switching

    Dispatching loop

  • 8/2/2019 Lecture 8 Process Management

    2/20

    Process

    a program in execution We should know about processes by now.

    How does the OS correctly run multiple

    processes concurrently?

    What kind of information to be kept?

    What does the OS have to do in order to run

    processes correctly.

  • 8/2/2019 Lecture 8 Process Management

    3/20

    Process context

    Contains all statesnecessary to run a

    program The information the

    process needs to dothe job: code, data,

    stack, heap. This is known as User

    level context.

    stack

    heap

    data

    text (code)

    0

    MAX

    Process in memory

  • 8/2/2019 Lecture 8 Process Management

    4/20

    User level context(b, *p) - main

    (a) - foo

    heap (p)(char[1000])

    data (aa, buf)

    text (code)

    0

    MAX

    Process memory

    int aa;

    char buf[1000];

    void foo() {

    int a;

    }

    main() {

    int b;char *p;

    p = new char[1000];

    foo();

    }

    stack

  • 8/2/2019 Lecture 8 Process Management

    5/20

    Process context

    Contains all statesnecessary to run aprogram Is the user level

    context sufficient? Only if the system runs

    through one program ata time

    The system typicallyneeds to switch backand forth betweenprograms.

    R0 = 1

    R2 = R0 + 1

    R0 = 2

    R2 = R0

    P1 P2

    R2 in P1 is wrong. How to makeIt correct?

    Save R0 in P1 before switching Restore R0 in P1 when switchingfrom P2 to P1.

    Registers should be a part of process

    context: the register context!

  • 8/2/2019 Lecture 8 Process Management

    6/20

    Process context:

    User level context

    Code, data, stack, heap

    Register context (R0, R1,, PC, stack

    pointer, PSW, etc).

    What else? OS resources. E.g open files, signal related

    data structures, etc.

  • 8/2/2019 Lecture 8 Process Management

    7/20

    Why is process contextimportant?

    To run a process correctly, the processinstructions must be executed within

    the process context!

  • 8/2/2019 Lecture 8 Process Management

    8/20

    Where is the process context stored? User level context is in memory.

    Other context information is stored in a data

    structure called process control block. The OS has a process control block table. For

    each process, there is one entry in the table.

    Process control block also contains otherinformation that the OS needs to manage the

    processes. Process status (running, waiting, etc)

    Process priority

  • 8/2/2019 Lecture 8 Process Management

    9/20

    Figure 3.3

    An example PCB

  • 8/2/2019 Lecture 8 Process Management

    10/20

    OS CPU abstraction

    Hardware reality: One CPU runs the fetch-

    execute algorithm

    OS abstraction: Each process has one

    CPU, running the fetch-execute algorithm for theprocess.

    Each process executeswithin its context.

    Load PC;IR = MEM[PC];While (IR != HALT) {

    PC ++;

    Execute IR;IR = MEM[PC];

    }

  • 8/2/2019 Lecture 8 Process Management

    11/20

    OS CPU abstraction

    What does the OS have to do?

    Embed the process instruction sequence

    into hardware instruction sequence.

    Process X instructions: x0, x1, x2, .Process Y instructions: y0, y1, y2, Process Z instructions: z0, z1, z2,

    Hardware instructions? x0, x1, x2, y0, y1, y2, z0, z1, z2, x3, x4, x5,

    Does this embedding work?

    No!! Instructions in a process should onlybe executed within the processs context to be correct.

  • 8/2/2019 Lecture 8 Process Management

    12/20

    OS CPU abstraction

    Process X instructions: x0, x1, x2, .Process Y instructions: y0, y1, y2, Process Z instructions: z0, z1, z2,

    x0, x1, x2, [store Xs context], [restore Ys context] y0, y1, y2

    OS must do this to keep programs executewithin its context: Context switching

  • 8/2/2019 Lecture 8 Process Management

    13/20

  • 8/2/2019 Lecture 8 Process Management

    14/20

    Dispatching Loop

    The hardware view of the systemexecution: dispatching loop

    LOOP Run process

    Save process states

    Choose a new process to run

    Load states for the chosen process

    Context

    Switch:Dispatchercode

    Scheduling

  • 8/2/2019 Lecture 8 Process Management

    15/20

    Simple? Not Quite

    How does the dispatcher (OS) regaincontrol after a process starts running?

    What states should a process save?

    How does the dispatcher choose thenext thread?

  • 8/2/2019 Lecture 8 Process Management

    16/20

    How Does the DispatcherRegain Control?

    Two ways:

    1. Internal events

    A process is waiting for I/O

    A process is waiting for some other process

    Yielda process gives up CPU voluntarily

    2.

    External events Interruptsa complete disk request

    Timerits like an alarm clock

  • 8/2/2019 Lecture 8 Process Management

    17/20

    What States Should a processsave?

    Anything that the next process maytrash

    Program counter

    Registers

    Etc.

  • 8/2/2019 Lecture 8 Process Management

    18/20

    How Does the DispatcherChoose the Next process?

    The dispatcher keeps a list ofprocesses that are ready to run

    If no processes are ready Dispatcher just loops

    Otherwise, the dispatcher uses a

    scheduling algorithm to find the nextprocess.

  • 8/2/2019 Lecture 8 Process Management

    19/20

    Process States

    A process is typically in one of thethree states

    1. Running: has the CPU

    2. Blocked: waiting for I/O or anotherthread

    3. Ready to run: on the ready list, waitingfor the CPU

  • 8/2/2019 Lecture 8 Process Management

    20/20

    Figure 3.2