what is interprocess communication shared memory and message passing


Ø  Intercrosses communication –there are two type of intercrosses communication
1.       Independent process
2.     Cooperating process
1)          Independent process – A process is independent if it cannot affect or affected by the other process executing in the system and also this type of process does not share data will any other process.
2)        Cooperating process – a process is cooperating if it can affect or affected by other process executing in the system and also this type of process share data with other process.

v Cooperating process required an interprocess communication (IPC) technique that will allow them to exchange data and information.
v There are two fundamental of IPC –
·        Shared memory
·        Message passing
v Process cooperation is required for following reason  -
·        Information sharing - Information sharing several user maybe interested in the same piece of information (shared file).
·        Computation speedup- Computation speedup for task run faster we must break in into subtasks each of which will be executing in parallel with other.
·        Modularity –dividing the system function into separate process.
·        Convenience –an individual user may work on many tasks at the same time.
·        For EX. A user may be editing, listing to music and computing.

1)     Shared memory model –
·        A region of memory this is shared by cooperating process.
·        Process can them exchange information by reading and writing data to the shared reason.
·        Shared memory can be faster than message passing because message passing system are typically implemented using system call and require more time consuming task of kernel.
·        Ones shared memory is establish all access as routine work and no burden in kernel for sharing.
·        Shared memory reason reside in the address in the address space of the process creating the shared memory segment other process that wish to communicate must attach it to their address space.
·        Shared memory requires that two or more process agree for sharing memory.



                                                                       
                                          
            Shared memory
Example –
  • Producer consumer process this is a classic example, in which one process is producing data and another process is consuming the data. (In this example in the order in which it is produced, although that could vary.)
  • The data is passed via an intermediary buffer, which may be either unbounded or bounded. With a bounded buffer the producer may have to wait until there is space available in the buffer, but with an unbounded buffer the producer will never need to wait. The consumer may need to wait in either case until there is data available.
  • This example uses shared memory and a circular queue. Note in the code below that only the producer changes "in", and only the consumer changes "out", and that they can never be accessing the same array location at the same time.
  • First the following data is set up in the shared memory area:
#define BUFFER_SIZE 10
typedef struct {
     . . .
} item;
item buffer[ BUFFER_SIZE ];
int in = 0;
int out = 0;
  • Then the producer process. Note that the buffer is full when "in" is one less than "out" in a circular sense:
// Code from Figure 3.13
item nextProduced;
while( true ) {
/* Produce an item and store it in nextProduced */
nextProduced = makeNewItem( . . . );

/* Wait for space to become available */
while( ( ( in + 1 ) % BUFFER_SIZE ) == out )
      ; /* Do nothing */

/* And then store the item and repeat the loop. */
buffer[ in ] = nextProduced;
in = ( in + 1 ) % BUFFER_SIZE;
}
  • Then the consumer process. Note that the buffer is empty when "in" is equal to "out":
// Code from Figure 3.14
item nextConsumed;
while( true ) {
/* Wait for an item to become available */
while( in == out )
      ; /* Do nothing */
/* Get the next available item */
nextConsumed = buffer[ out ];
out = ( out + 1 ) % BUFFER_SIZE;

/* Consume the item in nextConsumed
     ( Do something with it ) */
}
2)    Message passing –
·        Message passing provide a technique to allow process to communicate and to synchronize their action without sharing the same address space.
·        In the message passing model communication takes place by mean of message exchanged between the cooperating processes.
·        It is useful in distributed environment where the communication process may reside on different computers connected by network.
·        For example, an internet chat program could be designed so that chat participates. Communicate with one another by exchanging message.
·        Message passing is useful for exchanging smaller amount of data.
·        Message passing is useful for exchanging smaller amount of data.
·        Message passing is slower than shared memory because it’s very time consuming task of kernel invention.
·        A message passing facility provides at least two operations send (message), receive (message).
·        If process p and q want to communicate, they must send message to and receive message from each other communication link must exist between them.
·        And it is a logical implementation, provided by several methods.
·        Direct or indirect communication.
·         Synchronous or asynchronous communication
·         Automatic or expict buffering.
·         For direct communication (naming) -
Send (P, message)-send a message to process P.
Receive (Q, message) – receive a message from process Q.
·        For direct communication (naming)
Send (A, message)-send a message mailbox A.
Receive (A, message)-receive a message mailbox.



            Message passing