Ch03 - Processes.ppt

download Ch03 - Processes.ppt

of 90

Transcript of Ch03 - Processes.ppt

  • 8/12/2019 Ch03 - Processes.ppt

    1/90

    Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition,

    Processes

    Modified by M.Rebaudengo - 2010

  • 8/12/2019 Ch03 - Processes.ppt

    2/90

    3.2 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Concept

    !An Operating Systemexecutes a variety of programs:

    " Batch system jobs" Time-shared systems user programs or tasks.

  • 8/12/2019 Ch03 - Processes.ppt

    3/90

    3.3 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Definitions of process

    !A program in execution

    ! An istance of a program running on a computer! A unit of activity characterized by the execution of a sequence of

    instructions, a current state and an associated set of system resources.

    !The termsjoband processcan be used almost interchangeably

  • 8/12/2019 Ch03 - Processes.ppt

    4/90

    3.4 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    A process includes

    !text section (program code)

    ! program counter! stack (temporary data: function parameters, return addresses and

    local variables)! data section (global variables)! heap (dynamically allocated memory).

  • 8/12/2019 Ch03 - Processes.ppt

    5/90

    3.5 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process in Memory

  • 8/12/2019 Ch03 - Processes.ppt

    6/90

    3.6 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Processes and programs

    ! The program is a passiveentity; whereas the process is the activeentity! Two processes may be associated to the same programs

    " e.g., two users may be running two copies of the Web browser program" each of these is a separate process" the text sections are equivalent" the data, heap, stack sections vary.

  • 8/12/2019 Ch03 - Processes.ppt

    7/90

    3.7 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    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 processor" terminated: The process has finished execution.

    ! Only one process may be runningon any processor at any instant.! Many processes may be readyand waiting.

  • 8/12/2019 Ch03 - Processes.ppt

    8/90

    3.8 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Diagram of Process State

    5-state model

    timeout

  • 8/12/2019 Ch03 - Processes.ppt

    9/90

    3.9 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Control Block (PCB)

    ! The most important data structure in an OS! Information associated with each process:

    "Process identifier: unique numeric identifier of theprocess

    "Process state: new, ready, running, waiting,terminated

    "Program counter: the address of the next instructionto be executed

    "CPU registers: along with the program counter, thisstate information must be saved when an interrupt

    occurs

  • 8/12/2019 Ch03 - Processes.ppt

    10/90

    3.10 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Control Block (PCB)

    " CPU scheduling information: needed by the OS to perform itsscheduling function!process priority!scheduling-related information (e.g., the amount of time that the

    process has been waiting and the amount of time that the processexecuted the last time it was running)

    !pointers to scheduling queues" Memory-management information:

    !mapping between virtual and physical memory locations." Accounting information: used for billing purposes and performance

    measurements!the amount of CPU and real time used!main memory storage occupancy

    " I/O status information: resources allocated to the process!HW units: the list of I/O devices allocated to the process!the list of open files.

  • 8/12/2019 Ch03 - Processes.ppt

    11/90

    3.11 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Control Block (PCB)

  • 8/12/2019 Ch03 - Processes.ppt

    12/90

    3.12 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    CPU Switch From Process to Process

  • 8/12/2019 Ch03 - Processes.ppt

    13/90

    3.13 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Scheduling

    ! The objective of multiprogramming is to have some processrunning at all times, to maximize the CPU utilization

    ! The objective of time sharing is to switch the CPU amongprocesses so frequently that users can interact with eachprogram while it is running

    ! To meet these objectives, the process scheduler selects anavailable process for program executionon the CPU.

  • 8/12/2019 Ch03 - Processes.ppt

    14/90

    3.14 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    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! Each device has its own device queue! Processes migrate among the various queues.

  • 8/12/2019 Ch03 - Processes.ppt

    15/90

  • 8/12/2019 Ch03 - Processes.ppt

    16/90

    3.16 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Representation of Process Scheduling

    ! Queueing diagram:" each rectangular box represents a queue

    !two types of queues are present: the ready queue a set of device queues

    " the circles represent the resources that serve the queues" the arrows represent the flow of processes in the system.

  • 8/12/2019 Ch03 - Processes.ppt

    17/90

    3.17 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Representation of Process Scheduling

  • 8/12/2019 Ch03 - Processes.ppt

    18/90

    3.18 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Schedulers

    ! Long-term scheduler (or job scheduler) selects which processes shouldbe brought into the ready queue" in a batch system active processes are spooledto a mass-storage

    device, where they are kept for later execution" the long-term scheduler selects processes from this pool and loads

    them into memory for execution!

    Short-term scheduler (or CPU scheduler) selects which process shouldbe executed next and allocates CPU:" it selects from processes that are ready to execute.

  • 8/12/2019 Ch03 - Processes.ppt

    19/90

    3.19 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Schedulers (cont.)

    ! Frequency of execution:" Short-term scheduler is invoked very frequently (once ever 100

    milliseconds)!(must be fast)" Long-term scheduler is invoked very infrequently (seconds,

    minutes)!(may be slow)! The long-term scheduler controls the degree of multiprogramming

    (the number of processes in memory)

    " The long-term scheduler may need to be invoked only when aprocess leaves the system. Because of the longer intervalbetween executions, the long-term scheduler can afford to take

    more time to decide which process should be selected forexecution.

  • 8/12/2019 Ch03 - Processes.ppt

    20/90

  • 8/12/2019 Ch03 - Processes.ppt

    21/90

    3.21 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    OSs without long-term scheduler

    ! The long-term scheduler may be absent or minimal" Time-sharing systems often have no long-term scheduler but simply put

    every new process in memory for the short-term scheduler" the stability of these systems depends either on a physical limitation

    (the number of available terminals) or on the self-adjusting nature of

    users (if the performance declines to unacceptable levels on amultiusers system, some user will simply quit, or on a multitasking

    system, some tasks will be killed).

  • 8/12/2019 Ch03 - Processes.ppt

    22/90

    3.22 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Addition of Medium Term Scheduling

    ! Some OSs may introduce an additional, intermediate level of scheduling: amedium-term scheduler:" it could be advantageous to remove processes from memory and thus

    reduce the level of multiprogramming" later, the process could be reintroduced in memory, and its execution

    can be continued where it left off."

    This scheme is called swapping.

  • 8/12/2019 Ch03 - Processes.ppt

    23/90

  • 8/12/2019 Ch03 - Processes.ppt

    24/90

  • 8/12/2019 Ch03 - Processes.ppt

    25/90

    3.25 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Operations on processes

    ! Most systems provide a mechanism to create and terminate processes.

  • 8/12/2019 Ch03 - Processes.ppt

    26/90

    3.26 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Creation

    ! A process may create several new processes, via a create-process systemcall, during the course of execution:" the creating process is called a parentprocess" the new processes are called the childrenprocesses of that parent.

    ! Parent process create children processes, which, in turn create otherprocesses, forming a treeof processes

    ! Generally, process is identified and managed via a uniqueprocessidentifier (pid), which is an integer number.

  • 8/12/2019 Ch03 - Processes.ppt

    27/90

    3.27 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    A tree of processes on a typical Solaris

  • 8/12/2019 Ch03 - Processes.ppt

    28/90

    3.28 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Creation (cont.)

    ! When a process creates a new process the following possibilitiesexist in terms of:" 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 all of its children have terminated.

  • 8/12/2019 Ch03 - Processes.ppt

    29/90

    3.29 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Process Termination

    ! Process executes last statement and asks the operating system todelete it (exit)" Output data from child to parent (via wait)" Processresources 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/12/2019 Ch03 - Processes.ppt

    30/90

    3.30 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    UNIX examples

    ! Process creation:" forksystem call creates new process" execsystem call used after a forkto replace the processmemory

    space with a new program

  • 8/12/2019 Ch03 - Processes.ppt

    31/90

    3.31 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    fork()

    ! int fork(void);" the return code is zero for the new (child) process" the nonzero and positive process identifier of the child is returned to the

    parent process" a negative value represents an error in the process creation

    ! The two processes share the same program code! The new process consists of a copy of the address space of the original

    process! Both processes share the same value of the Program Counter Register:

    " Both processes continue the execution at the instruction after thefork()

  • 8/12/2019 Ch03 - Processes.ppt

    32/90

    3.32 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Example

    int main()

    {pid_t pid, fork_return;

    /* fork another process */

    fork_return = fork();

    if (fork_return < 0) { /* error occurred */

    printf("Fork Failed");

    }

    else if (fork_return == 0) { /* child process */{

    pid = getpid();

    printf("I'm the children! pid: %d\n", pid);

    /* system call getpid() returns the pid of the calling process */

    }

    }

    else { /* parent process */

    printf("I'm the father! the child pid is %d\n", fork_return);

    }

    }

  • 8/12/2019 Ch03 - Processes.ppt

    33/90

    3.33 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Exercise

    ! Including the initial parent process, how many processes are created by thefollowing program?

    int main()

    {

    /* fork a child process */

    fork();

    /* fork another process */

    fork();

    /* and fork another */

    fork();

    }

  • 8/12/2019 Ch03 - Processes.ppt

    34/90

    3.34 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Exercise

    ! What is the output of this program?

    int main()

    {

    pid_t pid;

    /* fork a child process */

    pid = fork();

    if (pid==0)

    while (1)

    printf (1);

    else

    while (1)

    printf (2);

    }

  • 8/12/2019 Ch03 - Processes.ppt

    35/90

    3.35 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    exec()

    ! Typically the exec()system call is used after a fork()system call by one of thetwo processes to replace the process's memory space with a new program.

    ! The exec()system call loads a binary file into memory (destroying the memoryimage of the program containing the exec()system call) and starts its execution.

    ! As a new process is not created, the PID does not change across an exec(), but thedata, heap and stack of the calling program are replaced by those of the newprogram

    ! There is a family of different exec functions! Syntax:

    " int execlp(char * path, char *arg0, char *arg1, ..., char*argN, (char *) 0);

    " The pathargument specifies the path name of the file to execute as the newprocess image.

    " Arguments arg0through argnare a list of pointers to arguments to be passedto the new process image.

    " The execfunctions do not normally return to the calling process. If an execfunction returns, an error occurred and the return value is 1.

  • 8/12/2019 Ch03 - Processes.ppt

    36/90

    3.36 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    wait()

    ! wait()is a system call that suspends the execution of the parent processwhile the child executes

    ! When the child process terminates, it returns an exit statusto the operatingsystem, which is then returned to the waiting parent process. The parentprocess then resumes execution.

    ! Syntax:"

    int wait (int *status);" statusis the address of the variable containing the exit status of the

    child process" the return value is the pid of the terminated process (or a negative

    number in case of error).

  • 8/12/2019 Ch03 - Processes.ppt

    37/90

    3.37 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Exercise

    ! Using the following program, what is the output at line A and line B?int value = 5;

    int main()

    {

    pid_t pid;

    pid = fork();

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

    { value += 15;

    printf (CHILD: value =%d\n, value); /* LINE A */

    return 0;

    }

    else { /* parent process */

    wait (NULL);

    printf (PARENT: value =%d\n, value); /* LINE B */

    }

    }

  • 8/12/2019 Ch03 - Processes.ppt

    38/90

    3.38 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    exit()

    ! exit()is a system call that terminates the process, asking the operatingsystem to delete the process

    ! The process may return an exit status value to its parent process (via thewait system call)

    ! All the resources of the process are deallocated by the operating system.! Syntax:

    " void exit(int status);" the exit()function causes normal process termination and the value

    of status & 0377is returned to the parent process" the exit()function does not return.

  • 8/12/2019 Ch03 - Processes.ppt

    39/90

  • 8/12/2019 Ch03 - Processes.ppt

    40/90

    3.40 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    C Program Forking Separate Processint 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", "-l", NULL);

    printf("Failed Exec \n");

    exit(-1);

    }

    else { /* parent process */

    /* parent will wait for the child to complete */

    wait (NULL);

    printf ("Child Complete");

    exit(0);

    }

    }

  • 8/12/2019 Ch03 - Processes.ppt

    41/90

    3.41 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Parent termination

    ! In Unix, if the parent terminates, all its children have assigned as their newparent the initprocess.

    ! Thus the children still have a parent to collect their status and executionstatistics.

  • 8/12/2019 Ch03 - Processes.ppt

    42/90

    3.42 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Independent vs. cooperating processes

    ! A process is independentif it cannot be affected by the other processesexecuting in the system

    ! A process is cooperatingif it can affect or be affected by the otherprocesses executing in the systems" any process that shares data with other processes is a cooperating

    process.

  • 8/12/2019 Ch03 - Processes.ppt

    43/90

    3.43 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Cooperating processes

    ! Reasons for cooperating processes:" Information sharing" Computation speedup" Modularity" Convenience

  • 8/12/2019 Ch03 - Processes.ppt

    44/90

    3.44 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Interprocess Communication

    ! Cooperating processes need interprocess communication(IPC) mechanism that will allow them to exchange data and

    information! Two models of IPC

    " Shared memory:!a region of memory that is shared by cooperating

    processes is established!processes can exchange information by reading and

    writing data to the shared region" Message passing:

    !communication takes place by means of messagesexchanged between the cooperating processes.

  • 8/12/2019 Ch03 - Processes.ppt

    45/90

    3.45 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Communications Models

    Shared memoryMessage passing

  • 8/12/2019 Ch03 - Processes.ppt

    46/90

    3.46 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Examples of IPC Systems - POSIX

    ! POSIX Shared Memory" Process first creates shared memory segmentid = shmget(IPC PRIVATE, size, S IRUSR | S IWUSR);

    " Process wanting access to that shared memory must attach to itshared_memory = (char *) shmat(id, NULL, 0);

    " Now the process could write to the shared memorysprintf(shared_memory, "Writing to shared memory");

    " When done a process can detach the shared memory from its addressspace

    shmdt(shared_memory);

    " destroy a segment (only after that all the attached processes aredetached)

    shmctl(id, IPC_RMID, 0);

  • 8/12/2019 Ch03 - Processes.ppt

    47/90

    3.47 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Shared memory and fork/exec/exit

    ! After a fork()the child inherits all the attached shared memory segments! After an exec()all the attached shared memory segments are detached

    (not destroyed)! When exit()all attached shared memory are detached (not destroyed)

  • 8/12/2019 Ch03 - Processes.ppt

    48/90

  • 8/12/2019 Ch03 - Processes.ppt

    49/90

    3.49 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Example (cont.)

    if (pid < 0) exit(-1);

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

    while (shm[SHMSZ-2] != 'z')

    sleep(1);

    for (s = shm; *s != NULL ; s++)

    putchar(*s);

    putchar('\n');

    *shm = '*';

    exit (0);

    }

    else { /* parent process */s = shm;

    for (c = 'a' ; c

  • 8/12/2019 Ch03 - Processes.ppt

    50/90

    3.50 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Shared memory vs. Message Passing

    ! Message Passing is useful for exchanging smaller amounts of data! Message Passing is easier to implement for intercomputer communication! Shared memory is faster than message passing, as ...

    " message passing systems are typically implemented using system callsand thus require the kernel intervention

    " in shared-memory systems, systems calls are required only to establishshared-memory regions and all accesses are treated as classicalmemoy accesses and no assistance from the kernel is required.

  • 8/12/2019 Ch03 - Processes.ppt

    51/90

    3.51 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Producer-Consumer Problem

    ! Paradigm for cooperating processes! producerprocess produces information that is consumed

    by a consumerprocess later! Since a producer and a consumer can work at different

    speeds, a buffer is needed where the producer can

    temporarily store data that can be retrieved by the consumer

    at a more appropriate speed

    bufferProducer Consumer

  • 8/12/2019 Ch03 - Processes.ppt

    52/90

    3.52 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Producer-Consumer Problem

    ! Unbounded-bufferplaces no practical limit on the size of thebuffer" the consumer may have to wait for new items," the producer can always produce new items

    ! Bounded-bufferassumes that there is a fixed buffer size" the consumer must wait if the buffer is empty" the producer must wait if the buffer is full.

  • 8/12/2019 Ch03 - Processes.ppt

    53/90

    3.53 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Bounded-Buffer Shared-Memory Solution

    ! Data shared by the producer and the consumer#define BUFFER_SIZE 10typedef struct {

    . . .

    } item;

    item buffer[BUFFER_SIZE];

    int in = 0;

    int out = 0;

    ! Circular Array:"in

    points to the next freeposition in the buffer" outpoints to the first full position in the buffer" the buffer is empty if in== out" the buffer is full if ((in+1)%BUFFER_SIZE) == out)

  • 8/12/2019 Ch03 - Processes.ppt

    54/90

    3.54 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Bounded-Buffer Producer

    item nextProduced;

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

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

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

    buffer[in] = nextProduced;

    in = (in + 1) % BUFFER SIZE;

    }

  • 8/12/2019 Ch03 - Processes.ppt

    55/90

    3.55 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Bounded Buffer Consumer

    item nextConsumed;

    while (true) {

    while (in == out)

    ; /* do nothing -- nothing to consume */

    /* remove an item from the buffer */

    nextConsumed = buffer[out];

    out = (out + 1) % BUFFER SIZE;

    return nextConsumed;

    }

  • 8/12/2019 Ch03 - Processes.ppt

    56/90

    3.56 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Interprocess Communication Message Passing

    ! Message Passing provides a mechanism for processes to communicateand to synchronize their actions without sharing the same address space

    ! IPC facility provides two operations:" send(message)" receive(message)

    ! If Pand Qwish 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 properties)

  • 8/12/2019 Ch03 - Processes.ppt

    57/90

    3.57 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Direct Communication

    ! Each process that wants to communicate must explicitly name the recipientor sender of the communication:" 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 communicating processes" Between each pair there exists exactly one link" The link may be unidirectional, but is usually bi-directional

  • 8/12/2019 Ch03 - Processes.ppt

    58/90

  • 8/12/2019 Ch03 - Processes.ppt

    59/90

    3.59 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Mailbox

    ! A mailbox can be owned either by a process or by the operating system.

  • 8/12/2019 Ch03 - Processes.ppt

    60/90

    3.60 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Mailbox owned by a process

    ! If the mailbox is owned by a process (that is the mailbox is part of theaddress space of the process) then we distinguish between the owner(which can only receive messages through this mailbox) and the user(which can only send messages to the mailbox)

    ! Since each mailbox has a unique owner, there can be no confusion aboutwhich process should receive a message sent to this mailbox

    ! When a process that owns a mailbox terminates, the mailbox dissapearsand any process that subsequently sends a message to this mailbox mustbe notified that the mailbox no longer exists.

  • 8/12/2019 Ch03 - Processes.ppt

    61/90

    3.61 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Mailbox owned by the OS

    ! A mailbox that is owned by the operating system is independent and is notattached to any particular process. The operaring system must provide amechanism that allows a process to do the following operations:" create a new mailbox" send and receive messages through mailbox" destroy a mailbox.

    !The process that creates a new mailbox is the initial owner of the mailboxthat can receive messages through this mailbox

    ! The ownership and receiving privilege may be passed to other processesthrough appropriate system calls. This provision could result in multiplereceivers for each mailbox.

  • 8/12/2019 Ch03 - Processes.ppt

    62/90

  • 8/12/2019 Ch03 - Processes.ppt

    63/90

    3.63 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Synchronization

    ! Message passing may be either blocking or non-blocking! Blockingis considered synchronous

    " Blocking send: the sender blocks until the message isreceived

    " Blocking receive: the receiver blocks until a message isavailable

    !Non-blockingis considered asynchronous

    " Non-blocking send: the sender sends the message and

    continues" Non-blocking receive: the receiver receives a valid message

    or null

  • 8/12/2019 Ch03 - Processes.ppt

    64/90

    3.64 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Possible combinations

    ! blocking send and blocking receive (rendez-vous)! non blocking send and blocking receive! non blockind send and non blocking receive.

  • 8/12/2019 Ch03 - Processes.ppt

    65/90

    3.65 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Buffering

    ! Queue of messages attached to the link; implemented in one of threeways1. Zero capacity 0 messages#

    Sender must wait for receiver (rendez-vous)2. Bounded capacity finite length of nmessages#

    Sender must wait if link full3. Unbounded capacity infinite length

    Sender never waits

  • 8/12/2019 Ch03 - Processes.ppt

    66/90

    3.66 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Message queue in UNIX

    ! A message queue is an ordered list of messages held in the kernel, whichprocesses can access.

    ! A process can create a new message queue, or it can connect to anexisting one.

    msgrcv()

    msgsnd()7 2 4

    data

    data

    data

    message queuekey = 1841

  • 8/12/2019 Ch03 - Processes.ppt

    67/90

    3.67 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Queue Properties

    ! A queue has a unique key, fixed size, ownership, and access modes.

    ! It must be explicitly deleted.! A message consists of a type (a positive integer), and data (characters).

  • 8/12/2019 Ch03 - Processes.ppt

    68/90

    3.68 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Create/access a queue

    ! A process can createa new Message Queue (MQ), or it can connectto an existingone

    ! int msgget(key_t key, int msgflg)

    " key:! unique key (name) of the message queue. Every other process that wants to

    connect to this queue will have to use the same key.! IPC_PRIVATE: special value for key: it ignores msgflg and returns ID of

    message queue on success" msgflg:

    ! IPC_CREAT: create a new MQ unless it already exists! IPC_EXCL: call of msgget()will fail if MQ already exists

    " returns:! ID of message queue (on success)! -1 (otherwise)

  • 8/12/2019 Ch03 - Processes.ppt

    69/90

    3.69 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    msgsnd()

    ! To send a message" int msgsnd(int msqid, const void *msgp, size_t msgsz,

    int msgflg);

    ! Arguments" msqidis the message queue identifier returned by msgget()" msgpis a pointer to the data to put on the queue" msgszis the size in bytes of the data to add to the queue" msgflgset to IPC_WAIT to avoid blocking; 0 otherwise.

  • 8/12/2019 Ch03 - Processes.ppt

    70/90

  • 8/12/2019 Ch03 - Processes.ppt

    71/90

    3.71 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Data Structure

    ! ptrmust point to a structof the form:#define MSGLEN 100/* any positive integer */

    struct msg {long mtype; /* positive message type */char mtext[MSGLEN]; /* for message data */

    };

    ! Cast it to struct msgbuf in msgsnd()" struct msgbuf {

    long mtype;char mtext[1];

    };

    " msgsnd(msqID, (struct msgbuf *) ptr, nbytes, flag);

  • 8/12/2019 Ch03 - Processes.ppt

    72/90

    3.72 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Blocking

    ! When IPC_WAITis not set.! If the queue is full, then msgsnd()will block until:

    1. the queue gets less full (i.e. a message is removed)2. the queue is deleted

    3. a signal is raised.

  • 8/12/2019 Ch03 - Processes.ppt

    73/90

    3.73 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Non-blocking

    ! IPC_WAITis set

    ! If the queue is full then msgsnd()returns immediately

  • 8/12/2019 Ch03 - Processes.ppt

    74/90

    3.74 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Receiving from the Queue

    ! int msgrcv(int msqid, void *msgp, size_t msgsz,long msgtyp, int msgflg);

    " msgtypcorresponds to the mtypeset in the msgsnd()! msgtypeffect on msgrcv()

    " Zero - Retrieve the next message on the queue, regardless of itsmtype.

    " Positive - Get the next message with a mtypeequal to thespecified msgtyp

    " Negative - Retrieve the first message on the queue whosemtypefield is less than or equal to the absolute value of themsgtypargument.

  • 8/12/2019 Ch03 - Processes.ppt

    75/90

    3.75 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    sz= msgrcv(msqID, ptr, nbytes, type, flag);

    message

    queue ID

    total no. of bytes

    of message field

    set to IPC_WAIT

    to avoid blocking;

    MSG_NOERRORalso possible

    type

    size of

    actual data

    type of

    message wanted

    Receiving from the Queue

  • 8/12/2019 Ch03 - Processes.ppt

    76/90

    3.76 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Message Data Length

    ! If the message data length > nbytesthen:" if MSG_NOERRORis set then the message is removed but

    truncated." if MSG_NOERRORis not set then the message is left on the

    queue

  • 8/12/2019 Ch03 - Processes.ppt

    77/90

    3.77 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    type Values

    Typevalue Message returned#0 first message#> 0 first message whose type == type< 0 first message whose type is the

    lowest value

  • 8/12/2019 Ch03 - Processes.ppt

    78/90

    3.78 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Examples

    ! if type== 5, remove second message! if type== -100 then three calls to msgrcv()remove messages 1, 5, 79

    " can use this to code priorities" 1 is highest, ... 100 is lowest

    79 5 1

    data

    data

    data

  • 8/12/2019 Ch03 - Processes.ppt

    79/90

    3.79 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Blocking

    ! When IPC_WAITis not set.! If the queue is empty, then msgrcv()will block until:

    1. the queue gets some messages#2. the queue is deleted3. a signal is raised

  • 8/12/2019 Ch03 - Processes.ppt

    80/90

    3.80 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Non-blocking

    ! IPC_WAITis set! If the queue is empty then msgrcv()returns immediately, and errnois

    assigned ENOMSG

  • 8/12/2019 Ch03 - Processes.ppt

    81/90

    3.81 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Destroying a message queue

    ! There are two ways of destroying a queue:" Use the Unix command ipcsto get a list of defined message queues,

    then use the command ipcrmto delete the queue." Write a program.

  • 8/12/2019 Ch03 - Processes.ppt

    82/90

    3.82 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    ! Message Control Operation! int msgctl(int msqid, int cmd, struct msqid_ds *buf);! Arguments

    " msqidis the queue identifier obtained from msgget()" cmdtells msgctl()how to behave: IPC_RMID is used to destroy the

    message queue" Buf argument can be set to NULL for the purposes of IPC_RMID.

    msgctl()

  • 8/12/2019 Ch03 - Processes.ppt

    83/90

    3.83 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Communication in Client-Server systems

    ! Sockets! RPC (Remote Procedure Calls)

  • 8/12/2019 Ch03 - Processes.ppt

    84/90

    3.84 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Sockets

    ! Two processes communicating over a network employ a pair of sockets.! A socket is an IP ADDRSS + a PORT NUMBER! They usually work as a client server connection: server listens for

    connections from clients! Specific services have specific ports (telnet: port 23, ftp: port 21, web: port

    80)! All ports below 1024 are used for standard services

    S

  • 8/12/2019 Ch03 - Processes.ppt

    85/90

    3.85 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Sockets

    ! Client initiates a connection request on a known port (e.g. 80)! The server assigns the client a port (> 1024)

    S k t C i ti

  • 8/12/2019 Ch03 - Processes.ppt

    86/90

    3.86 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Socket Communication

    R t P d C ll

  • 8/12/2019 Ch03 - Processes.ppt

    87/90

    3.87 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Remote Procedure Calls

    ! Remote procedure call (RPC) abstracts procedure calls between processeson networked systems.

    ! Stubs client-side proxy for the actual procedure on the server.! The client-side stub locates the server and marshallsthe parameters.! The server-side stub receives this message, unpacks the marshalled

    parameters, and peforms the procedure on the server.

    M h lli P t

  • 8/12/2019 Ch03 - Processes.ppt

    88/90

    3.88 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Marshalling Parameters

    Marshalling(similar to serialization) is the process of transforming thememory representation of an object to a data format suitable for storage or

    transmission.

    E ti f RPC

  • 8/12/2019 Ch03 - Processes.ppt

    89/90

    3.89 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition

    Execution of RPC

    R t M th d I ti

  • 8/12/2019 Ch03 - Processes.ppt

    90/90

    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.