Operating System : Interprocess Communication - SciComp

Post Top Ad

Responsive Ads Here

Operating System : Interprocess Communication

Share This

Inter Process Communication

Process executing concurrently in the OS may be either

1.    Independent process

2.    Cooperating process

Independent process

It cannot affect or be affected by the other processes executing in the system. No process will share data with any other process.

Cooperating process

It can affect or be affected by other processes executing in the system. Process will share data with other processes.

·        Information sharing

Several users use same piece of information.

·        Computation speedup

We want particular task to run faster, we must break into subtasks, each of which will be executing in parallel.

·        Modularity

Dividing the system functions into separate processes or threads.

·        Convenience

Individual user may work on many tasks in the same time.

Two types of Interprocess Communication :

1.    Shared Memory

2.    Message Passing


Shared Memory System :

A region of memory is shared by cooperating processes. Process can exchange information by reading & writing data to shared region. It resides in the address space of the process creating the shared memory management. The OS tries to prevent one process from accessing another process memory.

Ex. Producer- Consumer Problem :

    A producer process produces information that is consumed by a consumer process.



Client Server :

1.    Client as consumer

2.    Server as producer

To allow producer & consumer processes to run concurrently. We must available a buffer of items that can be filled by the producer & emptied by the consumer.

Two types of buffer:

1.    Unbounded buffer

2.    Bounded buffer

Unbounded buffer

1.    Places no practical limit on the size of the buffer.

2.    Consumer may have to wait for new items, but the producer can always produce new items.

Bounded buffer

1.    Assumes a fixed size buffer.

2.    The consumer have to wait if the buffer is empty & the producer must wait if the buffer is full.

          while(true) {
                     while (((in +1) % BUFFER_SIZE) == OUT);
                     buffer[in]= next_produced;
                     in=(in +1 ) % BUFFER_SIZE;
      }

Producer process using shared memory

                             # define BUFFER_SIZE 10

                            typedef struct   {

                             . . .

                                } item;

                                Item buffer[ BUFFER_SIZE];

                                int in = 0;

                                int out = 0;

Region of memory shared by producer & consumer processes

v  Shared buffer is implemented as a circular array with two a logical pointers: in & out.

v  Variable in points to the next free position in buffer, out points to first full positon in buffer.

v  Buffer is empty when in== out.

v  Buffer is full when ((in +1) % BUFFER_SIZE)== OUT.

v  Producer process has a local variable next_produced in which the new item to be produced is stored.

v  Consumer process has a local variable next_consumed in which the new item to be consumed is stored.

               Item next_consumed;
               while(true) {
                 while(in==out)                           
                        next_consumed=buffer[out];
                        out=(out + 1) % BUFFER_SIZE;
                }

Consumer process using shared memory

Message Passing System:

 Communication take place by means of messages exchange between the cooperating process. It allow processes to communicate & to synchronize their actions without sharing same address space.

For example: An internet that program could be designed so that participants communicate with one another by exchanging messages.

Two operations:

1.    Send(message)

2.    Receive(message)

v  Message sent by a process can be either fixed or variable in size.

v  Fixed size messages, the system-level implementation is straight-forward.

v  Variable-size messages require a complex system-level implementation.

v  If processes P & Q want to communicate, they must send messages to and receive messages from each other.

v  Communication link must exist between them.

Physical Implementation

·        Shared memory

·        Hardware bus

·        Network

Logical Implementation

·        Direct and Indirect

·        Synchronous or Asynchronous

·        Automatic buffering/ Explicit Buffering

Naming/ Direct Communication

Process 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.

·        Link is established automatically.

·        Link is associated with exactly two processes.

·        Between each pair of processes, there exists exactly one link.

Symmetry: Both the sender process and the receiver process must name the other to communicate.

Asymmetry: The sender name the recipient, the recipient is not required to name the sender.

·     Send (P, message) – Send a message to process P.

·     Receive (id, message) – Receive a message from any process.

Disadvantage:

Limited modularity

Indirect communication

The messages are send to & received from Mailboxes or ports.

A process can communicate with another process via a number of different mailboxes.

But two process can communicate only if they have a shared mailbox.

·        Send (A, message) – send a message to mailbox A.

·        Receive (A, message) – receive a message from mailbox A.

Communication link properties:

A link is established between a pair of processes only if both members of the pair have a shared mailbox. A link may be associated with more than two processes. Between each pair of communicating processes, a number of different links may exist, with each link corresponding to one mailbox.

v  Now suppose that processes P1, P2 & P3 all share mailbox A.

v  Process P1 sends a message to A, while both P2 & P3 executed a receive () from A.

v  Which process will receive the message sent by P1 ?

The answer depends on which of the following methods we choose:

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

·        Allow at most one process at a time to execute a receive () operation.

The system may define an algorithm for selecting which processes will receive will receive the message.

A  mailbox may be owned either by a process, then we distinguish between the Owner & the User.

If the mailbox is owned by the OS has an existence of its own. The OS must be provide an mechanism that allows to do the following.

1.    Create a new mailbox.

2.    Send & receive message through the mailbox.

3.    Delete a mailbox.


Synchronization

Communication between processes takes place through calls to send( ) & receive primitives.

Message passing may be either blocking or non-blocking also known as Synchronous & Asynchronous.

Block Send – Sending process is blocked until the message is received by the receiving process or by the mailbox.

Non-Blocking Send – Sending process sends the message and resumes operation.

Blocking Receive – The receiver blocks until a message is available.

Non-Blocking Receive – The receiver retrieves either a valid message or a NULL. When both send( ) and receive( ) are blocking, we have a rendezvous between the sender and the receiver.

Buffering: Whether communication is direct or indirect, messages exchanged by communicating processes reside in a temporary queue.







Message next_produced;    
while(true) {
  send (next_produced);
}

Producer process using Message Passing

Zero Capacity- queue has maximum length of “Zero”, the link cannot have any messages waiting in it.

Sender must block until the recipient receive the message.

Bounded Capacity- Queue has finite length “n”, atmost n message can reside in it. If the queue is not full when a new message is sent, the message is placed in the queue and the Sender can continue the execution without Waiting.

Unbounded Capacity- Queue’s length is potentially infinite, any number of messages can wait in it. The sender never blocks.

No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here

Pages