Operating Systems -ch4-F06

Embed Size (px)

Citation preview

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

    1/41

    1

    Operating Systems

    404452 Section 1

    9 October 2004

    Chapters 4

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

    2/41

    Chapter 4 Operating Systems 2

    Chapter 4: Processes

    Process Concept

    Process Scheduling

    Operations on Processes

    Cooperating Processes

    Interprocess Communication

    Communication in Client-Server Systems

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

    3/41

    Chapter 4 Operating Systems 3

    Process Concept

    An operating system executes a

    variety of programs: Batch system jobs

    Time-shared systems userprograms or tasks

    Textbook uses the termsjob and

    process almost interchangeably Process a program in execution;

    process execution must progressin sequential fashion

    A process includes:

    program counter

    stack

    data section

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

    4/41

    Chapter 4 Operating Systems 4

    Process State

    As a process executes, it

    changes state new: The process is

    being created

    running: Instructionsare being executed

    waiting: The processis waiting for someevent to occur

    ready: The process iswaiting to be assigned

    to a process terminated: The

    process has finishedexecution

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

    5/41

    Chapter 4 Operating Systems 5

    Process Control Block (PCB)

    Information associated with

    each process Process state

    Program counter

    CPU registers

    CPU scheduling information

    Memory-managementinformation

    Accounting information

    I/O status information

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

    6/41

    Chapter 4 Operating Systems 6

    CPU Switch From Process to Process

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

    7/41

    Chapter 4 Operating Systems 7

    Process Scheduling Queues

    Job queue set of all processes in the system Ready queue set of all processes residing in

    main memory, ready and waiting to execute

    Device queues set of processes waiting for

    an I/O device Processes migrate among the various queues

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

    8/41

    Chapter 4 Operating Systems 8

    Ready Queue And Various I/O DeviceQueues

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

    9/41

    Chapter 4 Operating Systems 9

    Representation of Process Scheduling

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

    10/41

    Chapter 4 Operating Systems 10

    Schedulers

    Long-term scheduler (or job scheduler) selects which processes should be broughtinto the ready queue

    Short-term scheduler (or CPU scheduler) selects which process should be executed nextand allocates CPU

    Medium-term scheduler removes partiallyexecuted (incomplete) processes in and out ofmemory

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

    11/41

    Chapter 4 Operating Systems 11

    Addition of Medium Term Scheduling

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

    12/41

    Chapter 4 Operating Systems 12

    Schedulers (Cont.)

    Short-term scheduler is invoked very

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

    infrequently (seconds, minutes) (may beslow)

    The long-term scheduler controls the degree ofmultiprogramming

    Processes can be described as either:

    I/O-bound process spends more time

    doing I/O than computations, many shortCPU bursts

    CPU-bound process spends more timedoing computations; few very long CPU

    bursts

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

    13/41

    Chapter 4 Operating Systems 13

    Context Switch

    When CPU switches to another process, the

    system must save the state of the old processand load the saved state for the new process

    Context-switch time is overhead; the systemdoes no useful work while switching

    Time dependent on hardware support

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

    14/41

    Chapter 4 Operating Systems 14

    Process Creation

    Parent process create children processes,

    which, in turn create other processes, forminga 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

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

    15/41

    Chapter 4 Operating Systems 15

    Process Creation (Cont.)

    Address space

    Child duplicate of parent (child has the sameprogram and data with parent).

    Child has a program loaded into it

    UNIX examples

    fork system call creates new process exec system call used after a fork to replace the

    process memory space with a new program

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

    16/41

    Chapter 4 Operating Systems 16

    C Program Forking Separate Process

    int main()

    {Pid_t pid;

    /* fork another process */pid = fork();if (pid < 0) { /* error occurred */

    fprintf(stderr, "Fork Failed");exit(-1);

    }else if (pid == 0) { /* child process */

    execlp("/bin/ls", "ls", NULL); //replaceaddress space with new program}

    else { /* parent process *//* parent will wait for the child to

    complete */wait (NULL);printf ("Child Complete");exit(0);

    }

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

    17/41

    Chapter 4 Operating Systems 17

    A tree of processes on a typical Solaris

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

    18/41

    Chapter 4 Operating Systems 18

    Process Termination

    Process executes last statement and asks the

    operating system to delete it (exit) Output data from child to parent (via wait)

    Process resources are deallocated by operatingsystem

    Parent may terminate execution of childrenprocesses (abort)

    Case 1: Child has exceeded allocated resources

    Case 2: Task assigned to child is no longer required

    Case 3: parent is exiting

    Some operating system do not allow child tocontinue if its parent terminates

    All children terminated - cascading termination

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

    19/41

    Chapter 4 Operating Systems 19

    Cooperating Processes

    Independent process cannot affect or be

    affected by the execution of another process Cooperating process can affect or be affected

    by the execution of another process (parallelprogramming)

    Advantages of process cooperation Information sharing (concurrent users interested in

    the same information).

    Computation speed-up

    Modularity

    Convenience (single user can issues several task inthe same time)

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

    20/41

    Chapter 4 Operating Systems 20

    InterProcess Communication (IPC)

    Cooperating processes requires means of managing the

    exchange of information and data.

    This means (ways) are know as IPC which is a mechanismfor processes to communicate and to synchronize theiractions

    Two main ways to establish IPC:

    1) Shared memory

    Part of the memory is shared between the cooperatingprocesses. The cooperating processes can read/write tothis shared space.

    2) Message passing

    The cooperating processes communicate throughoutexchanging messages.

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

    21/41

    Chapter 4 Operating Systems 21

    Shared memory : Producer-Consumer Problem

    Paradigm for cooperating processes,producerprocess produces informationthat is consumed by a consumerprocess

    unbounded-bufferplaces nopractical limit on the size of thebuffer

    bounded-bufferassumes that there

    is a fixed buffer size

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

    22/41

    Chapter 4 Operating Systems 22

    Bounded-Buffer Shared-Memory Solution

    Shared data

    #define BUFFER_SIZE 10

    Typedef struct {

    . . .

    } item;item buffer[BUFFER_SIZE];

    int in = 0;// points to next free location

    int out = 0;// points to the first full location

    Buffer is FULL if (in+1 % BUFFER_SIZE ==out)

    Buffer is Empty if in == out

    Solution is correct, but can only useBUFFER_SIZE-1 elements

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

    23/41

    Chapter 4 Operating Systems 23

    Bounded-Buffer Insert() Method

    while (true) {/* Produce an item */

    while ( (in + 1) % BUFFER_SIZE_count) == out)

    ; /* do nothing -- no free buffers */

    buffer[in] = item;

    in = (in + 1) % BUFFER SIZE;

    {

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

    24/41

    Chapter 4 Operating Systems 24

    Bounded Buffer Remove() Method

    while (true) {

    while (in == out)

    ; // do nothing -- nothing to consume

    // remove an item from the buffer

    item = buffer[out];

    out = (out + 1) % BUFFER_SIZE;

    return item;{

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

    25/41

    Chapter 4 Operating Systems 25

    Message passing

    Message system processes communicate witheach other without resorting to shared variables

    Useful in distributed environments.

    IPC facility provides two operations:

    send(message) message size fixed or variable

    receive(message)

    IfPand Q wish to communicate, they need to: establish a communicationlinkbetween them

    exchange messages via send/receive

    Implementation of communication link

    physical (e.g., shared memory, hardware bus) logical (e.g., logical features: Direct/Indirect

    synchronous/asynchronous )

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

    26/41

    Chapter 4 Operating Systems 26

    Implementation Questions

    How are links established?

    Can a link be associated with more than twoprocesses?

    How many links can there be between everypair of communicating processes?

    What is the capacity of a link? Is the size of a message that the link can

    accommodate fixed or variable?

    Is a link unidirectional or bi-directional?

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

    27/41

    Chapter 4 Operating Systems 27

    Communications Models

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

    28/41

    Chapter 4 Operating Systems 28

    Direct Communication

    Processes must name each other explicitly:

    send (P, message) send a message toprocess P

    receive(Q, message) receive a messagefrom process Q

    Properties of communication link Links are established automatically

    A link is associated with exactly one pair ofcommunicating processes

    Between each pair there exists exactly one link

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

    Disadvantage changing process identifierrequires modifying all references to it.

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

    29/41

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

    30/41

    Chapter 4 Operating Systems 30

    Indirect Communication

    Operations

    create a new mailbox send and receive messages through

    mailbox

    destroy a mailbox

    Primitives are defined as:

    send(A, message) send a message tomailbox A

    receive(A, message) receive a message

    from mailbox A

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

    31/41

    Chapter 4 Operating Systems 31

    Indirect Communication

    Mailbox sharing

    P1, P2, and P3 share mailbox A P1, sends; P2and P3 receive

    Who gets the message?

    Solutions

    Allow a link to be associated with at mosttwo processes

    Allow only one process at a time to executea receive operation

    Allow the system to select arbitrarily thereceiver. Sender is notified who the receiverwas.

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

    32/41

    Chapter 4 Operating Systems 32

    Indirect communication (2)

    Mailbox owned by a process.

    - The process that owns the mailbox canexecutes receive only. This elevates theproblem of who should receive the message.

    - If the process who owns the mailbox

    terminates, the mailbox should be destroyed Mailbox owned by the OS

    - Created and controlled by the OS.

    - Only the OS can destroy the mailbox.

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

    33/41

    Chapter 4 Operating Systems 33

    Synchronization

    Message passing may be either blocking or

    non-blocking Blocking is considered synchronous

    Blocking send has the sender block untilthe message is received

    Blocking receive has the receiver blockuntil a message is available

    Non-blocking is considered asynchronous

    Non-blocking send has the sender send the

    message and continue Non-blocking receive has the receiver

    receive a valid message or null

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

    34/41

    Chapter 4 Operating Systems 34

    Buffering

    Regardless whether the communication is direct or

    indirect the messages must reside in temporaryqueue.

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

    1. Zero capacity the queue has 0 lengthSender must block for receiver

    2. Bounded capacity finite length ofn messages-If queue is not full sender send and continue

    If queue is full

    Sender must block3. Unbounded capacity infinite length

    Sender never waits

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

    35/41

    Chapter 4 Operating Systems 35

    Client-Server Communication

    Sockets

    Remote Procedure Calls Remote Method Invocation (Java)

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

    36/41

    Chapter 4 Operating Systems 36

    Sockets

    A socket is defined as an endpoint for

    communication Any pair of processes wish to communicates

    must employ a pair of sockets. One on eachside.

    Concatenation of IP address and port

    The socket 161.25.19.8:1625 refers to port1625 on host 161.25.19.8

    Communication consists between a pair ofsockets

    Sockets are client-server architecture, whereserver listen to well-known ports. Oncemessage is received on these ports it acceptsthe connection from client socket.

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

    37/41

    Chapter 4 Operating Systems 37

    Socket Communication

    * How a process in different computer can establish a connection with the web server ?

    * How could another process in the samer computer establish a connection with the webserver ?

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

    38/41

    Chapter 4 Operating Systems 38

    Remote Procedure Calls

    Remote procedure call (RPC) abstracts procedure callsbetween processes on networked systems.

    Stubs (Proxy for the remote object). It is a softwarethat acts as an interface with the actual RPC and hidesthe details of real RPC communication.

    Each RPC on the client has a specific stub.

    Marshalling: the process of converting the parametersinto a format the could be transmitted over the networkand understood by the server.

    The client-side stub locates the port on the server andmarshalls the parameters.

    The server-side stub receives this message, unpacks themarshalled parameters, and performs the procedure onthe server.

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

    39/41

    Chapter 4 Operating Systems 39

    Execution of RPC

    RPC steps:1-Client invokes RPC.

    2-The suitable stub on the clientis called with the procedureparameters.

    3-The stub locates procedureport on the server, marshalls theparameters and sends therequest.

    4-The server stub receives the

    request, unmarshalls theparameters, performs therequired procedure and returnsthe result.

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

    40/41

    Chapter 4 Operating Systems 40

    Remote Method Invocation

    Remote Method Invocation (RMI) is a Java

    mechanism similar to RPCs. RMI allows a Java program on one machine to

    invoke a method on a remote object.

    Objects are considered remote if they are

    located in different (JVM).

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

    41/41

    Ch 4 O i S 41

    Marshalling Parameters

    Stub: Client side proxy that is called when the client invokes a

    remote object.Skeleton: server side proxy that is responsible for unmarshallingthe parameters and calling the desired remote method.