Monday, March 5, 2012

IPC -What to use When??

IPC:
  1. Sockets BSD
  2. PIPE
  3. Named PIPE (FIFO)
  4. Signals
SYSTEM V IPC
  1.  Message  Queue
  2. Shared Memory
  3.  Semaphore
--------------------------------------------------------------------------------------------------------
PIPE v/s NAMED PIPE

PIPE
  • Used in related process which must have common ancestry .
  • These are not permanent. If creator process terminates pipe also goes off.
  • Half duplex Data flows in only one direction. 
  • Reading from a pipe whose write end is closed returns 0 indicating EOF
  • Writing on a PIPE whose read end  is closed generates signal SIGPIPE

Named pipe(FIFO) : FIFO is a special file whose semantics differ significantly from those of a regular file
  • Can be used between unrelated Process. Either of them or any other (command mknod) can create FIFO.
  • These are Permanent Need to be Removed Explicitly
  • Treated  just as file have size owner access permissions Can be opended closed or deleted like any other file.
  • Bytes written to a FIFO are always written as though they were written in append mode.
  • Bytes read from a FIFO are always read from the beginning of the file and then removed.  
  • When all processes which have the FIFO open close the FIFO, any bytes remaining in the FIFO are removed.
  • Writing to a FIFO which no process has open for reading results in an error condition.
  • SIGPIPE is generated when writer writes to a FIFO on which either reader is died or have closed reading file descriptor. if reader is not at all created  then by default  open()  fifo=open("fifo_writer",O_WRONLY); call will block. When reader comes writer open() unblocks and writes successfully. Now after reading if reader goes away or just closes reading fd SIGPIPE will be generated.
  • If open is NON_Blocking  fifo=open("fifo_writer",O_WRONLY| O_NONBLOCK);  then even though pipe exists and Reader is not there open will return error -1 No such device or address
On Prompt it can be created by command mknod
mknod ( FIFO_NAME, S_IFIFO | 0666, 0);    0 is the device no.  ignored .
mkfifio(name , permission ) is better

----------------------------------------------------------------------------------------------------------
 System V IPCs :   Message Queue  ,Semaphore, Shared Memory :
 Creation(msgget,semget,shmget) requires  key of type Key_t (long integer defined in sys/types.h). Key is converted in identifier by kernel

 How  to share key between unrelated Processes:
  1.   Key can be created by one process using IPC_PRIVATE and written in file (disk operation is required )
  2.    Key can be put in Header file
  3.    ftok() to generate key based on two arguments passed path name and project UNIQUE ID(any no between 0 -255)
  Each ipc will have associated struct ipc_perm wich can be changed by semctl,msgctl,shmctl functions.
  SYSTEM V IPC are system wide  do not have any refrence count
  They remain in SYSTEM untill they are deleted explixitly (Pipe are deleted as soon as the Last process refrencing Pipe terminates )
  IPC structures are not known as name in file system cant use ls,shmod.rm command on that.
  ipcs ipcrm are created to tdo these operation.
  Dont use File descriptor I/O multiplexing is not possible cant use poll() select().


Shared Memory :
  • Shared Memory is fastest form of IPC. As there is no copy of data form sender to receiver. after shmat  at different process. individudal Virtual pages maps to same physical page from RAM. And it can be accessed as any other buffer.
  • Can be used between any Processes and random access is allowed.
  • In shared memory data has to be deleted explicitly
  • Reader has to be notified for the data.

Why is shared memory the fastest form of IPC?
 Once the memory is mapped into the address space of the processes that are sharing
 the memory region, processes do not execute any system calls into
 the kernel in passing data between processes, which would otherwise be required.
 Shared memory lets two or more processes share a region of memory.
 The processes must, of course, coordinate and synchronize (using Semaphore )their use of the shared memory between themselves to prevent data loss.

Accesses to the shared memory region have to be mutually exclusive; this is achieved via the use of the semaphore

 Generally Shared Memory can be used in three ways to
 mmap ,PoSIX(shm_open, mmap) and System V IPC(shm_get,shmat())

Message Queues: 
  1. The primary difference between a System V message queue and a socket or named pipe is that message queues may have multiple processes reading and writing from and to them, or no readers at all. 
  2. Allows prioritizing of messages.
  3.  Message queues have a size limit and a surprisingly small one.  16K for Linux and 4k for Solaris. 
  4. Individual messages have size limits.  8K for Linux and 2K for Solaris.
  5. Usually faster than pipes
  6. Difference from Shared Memory:   In Message queue Message passed is deleted as soon as its read by Reader . In shared memory data has to be deleted explicitly 


Some Questions :

Q Can many apps listen to same Msg Queue ? Can many app post to came queue ?
Answer:    Many Application can listen on same Msg Queue for their corresponding Message TYPE. Message is removed once listening app reads message to its type.
Yes multiple APP can write also.
Q What to use for two way Communication for two APPS?
Socket has to be used in this case . not possible on Message queue

No comments:

Post a Comment