IPC (Inter-Process Communication) on Linux


IPC
1.  Pipes
2.  Named pipes (also called as FIFO)
3.  Signals
4.  Message queue
5.  Shared memory
6.  Semaphores
7.  Sockets

1. Pipes:
    Types:
            a. Anonymous pipes
            b. Named pipes OR FIFO

    a. Anonymous pipes:
        - Created b/w related processes
        - Generally, parent ancestor create pipe and the fork child process. Each child process get access             to pipe.
        - one end of pipe is read only and other is write only. This means pipe is half duplex.
        - generally used to send data stream from one process to another (like | command in Linux)
        - pipe is automatically deleted at the end of proccess
        - Once data is read, cant be read again(is removed from pipe), but required bytes of data stream             can be read.
        - are unidirectional
        - No need of synchronization primitives
        - To create use,  pipe()
   

   b. Named pipes OR FIFO:
       -  Allows communication b/w proccesses that are not related to each other on same host
       -  The processes communicate using named pipe by opening a special file (FIFO)
       -  One process opens a file for writing while other process at the same time for reading
       -  The FIFO file on disk is created
       -  Opening FIFO file for reading blocks opening of the same file for writing (are unidirectional) 
       -  To create, mkfifo()
       -  you don't have to start the reading/writing processes at the same time
       -  Shared memory also gives you more control over buffering and resource use - within limits                  allowed by the OS it's you who decides how much memory to allocate and how to use it.                      With pipes, the OS controls things automatically, so once again you loose some flexibility                    but are relieved of much work.

***With pipe, communication is like phone.

3.  Signals :
       -  Also called as S/W interrupt
       -  Signals are the cheapest forms of IPC
       -  primary use is to notify processes of change in states or events that occur
       -  Notifies the process when an event occurs by interrupting process's normal flow of execution              and invoking one of the signal handler function
       -  Signal is a small int starting from 1
       -  To register signal handler, use signal()

4. Message queue: (Talking about System V MQ only)
       -  One process can write message to message queue and exits
       -  Another process can access the message from the same message queue at latter point of time
       -  The advantage of message queues over pipes/FIFOs are that the sender (or writer) processes                do not have to wait for the receiver (or reader) processes to connect
       -  Not possible to read a part of a message or to read multiple messages at a time ( This is       
          possible in pipe)
       -  Each message has integer type and messages can be retrieved from a queue in FIFO order or   
          retrieved by type
       -  New applications should not use message queue. When MQ was implemented, that time no
           other mechanism was available. It has some disadvantage.
       -  To create, use msgget() -- create msgq id used for communication
       -  To exchange message:
          a. msgsnd(): To send message
b. msgrcv(): To read message and is deleted from queue
       -  To delete MQ, use msgctl()
       -  Disadvantages:
i. numeric identifier is required. ftok() does not give gurantee of returning unique one.
ii. There is configurable limit on number of messages on MQ
       - Unidirectional

  POSIX Message Queue:
       - Puporse is same as above
       - Message is associated with priority
       - Messages are queued and received in priority order
       - MQ provide a feature which allows a process to be asynchronously notified that a message has
         arrived on MQ
       - added in kernel version 2.6.6
       - Functions:
mq_open() - creates a new MQ or opens an existing queue and returns a message queue
           descriptor
          mq_send() - writes a message to a queue
mq_receive() - reads a message from a queue
mq_close() - closes a MQ  that the process previously opened
mq_unlink() - removes a MQ name and marks the queue for deletion when all processes have
                                  closed it
mq_notify() - allows a process to register for message notification from a queue

   
5.  POSIX Shared memory:
       -  allows to us to share a mapped region between unrelated processes
       -  Linux uses a tmpfs file system mounted under the directory /dev/shm to create shared object
       -  Shared object will persist even though no one is using it
       -  This provides fast IPC in comparison with techniques such as pipes or MQs wherein the
           sending process copies data from a buffer in user space into kernel memory and the receiving
           process copies in the reverse direction.
       -  Synchronization premitive can be used to avoid race condition e.g. semaphore and shared
           mutex
       -  Many processes can read from SM
       -  SM need manual freeing, even if unused by any program
       - Functions:
           shm_open() - open an object with specified name and return object
mmap()        - maps shared memory object into process's virtual address space and return the
                                   address of memory which points to the data
            shm_unlink() - remove  Shared Memory Object


6.  POSIX Semaphores:
       - Not used to transfer data
       - Allow processes and threads to synchronize access to shared resouces
       - an integer whose value is not permitted to fall below 0
       - Types of Semaphores:
          a. Named semaphores: By calling sem_open() with the same name, unrelated processes can
               access the same semaphore
            - Functions:
                 sem_open() - function opens or creates a semaphore, initializes the semaphore if it is
                                       created by the call, and returns a handle for later use
sem_post(sem) and sem_wait(sem) - respectively increment and decrement a semaphore’s
                                                                             value
                 sem_getvalue() - retrieves a semaphore’s current value
sem_close() - removes the calling process’s association with a semaphore that it
                                        previously opened sem_unlink() function removes a semaphore name and
                                        marks the semaphore for deletion when all processes have closed it

b. Unnamed semaphores:
                - doesn’t have a name. Instead it resides at a location in memory.
                - It can be shared between processes(Related) or between a group of threads.
                - When shared between processes, the semaphore must reside in a region of shared
                   memory. When shared between threads, the semaphore may reside in an area of memory
                   shared by the threads (such as, on the heap or in a global variable).
            - Functions:
sem_init() - initializes a semaphore and informs the system of whether the semaphore will
                                    be shared between processes(related) or between the threads of a single process
sem_destroy(sem) - destroys a semaphore
                Other function remains same as named semaphores

7.  Sockets:
      - allows communication b/w processes either running on same hosts or different hosts
      - SM is faster than Sockets. Data from sending process is delivered thro' kernel to receving   
         process if sender and and recever are on same machine
      - blocking and non-blocking mode and switching between them
      - No need to free them when tasks are completed


Comments

Popular posts from this blog

STL Questions

Producer Consumer problem using mutex

Interview questions