OS Internals: Interprocess Communication ( IPC)

ipc

Interprocess Communication ( IPC )

  • Interprocess communication allows a process to communicate with another process.

  • Communications can be one of two types

    • Between related process ( parent and child )
    • Between unrelated processes ( one or more different processes)
  • IPC can use

    • Pipes
    • First In First Out ( FIFO) - Queue
    • Message Queues
    • Shared Memory
    • Semaphores
    • Signals

Pipes

  • Pipes are half-duplex, they can only read or write a process but only in one direction,

  •  

First In, First Out (Named Pipes)

  • FIFO are also called as named pipes, and are half duplex as pipes.

  • when you create a pipe, to identify it, execute ls -l command to the pipe, you can see if its a pipe or not

In the above output prw-r--r-- - p denotes that its a pipe.

Shared Memory

  • shared memory is full duplex, either process can read and/or write.
  • most efficient type of IPC, it does not require any kernel intervention once the shared memory has been allocated/deallocated.
  • Required a program
  • Any number of processes can read and write to the same shared memory segment.
  • You can query shared memory with the ipcs command
  • Processes must manage shared memory
  • Process must protect shared memory being wirtten or race conditions will occur( if two process are trying to access same data at same time)

Message Queues

  • created by syscall
  • managed by kernel
  • kernel will delete the message from queue once it is read
  • each read and write creates a syscall to the kernel
  • the message queue helps eliminate the occurrences of race conditions but comes at the expense of performance due to the syscall interrupt(trap)
  • data will remain in the queue until it is read and once read its gone.

 

Semaphores

  • Semaphores are used to protect critical/common regions of memory shared between multiple processes.

  • So other process cant use this shared memory till the process that owned the memory releases it.

  • Semaphores are the atomic structures of operating systems

  • Two Types of semaphores:

    • Binary: Only two states 0 and 1, locked/unlocked, or available, unavailable etc
    • Counting Semaphores : Allow arbitrary resource counters
  • When a process 'allocates' a semaphore, it blocks the other processes from access until the first process issues a 'release' indicating it has completed its operation.

  • The kernel then makes the semaphore available again for allocation.

Signals

  • A signal is a notification of an event occurrence.

  • A signal is also know as a trap, or software interrupt.

  • if you perform 'kill -l' you can see list of killl siganls implemented in your kernel

    • for example in a linux machine

     

    • And in Mac,
  • Signals can be generated by a user, process or kernel
  • if its a process, it is supposed to be written to handle them
  • Certain signals numeric 9-15 cannot be handled by the process they will immediately cause termination of the process and cannot be blocked, these are called 'process crash-outs' ExL CTRL+C, which cant be blocked by the process and terminates the process.

 

POSIX standards now govern the use and definition of IPC constructs and added support to modern features like threads etc.

 

Comments

Popular posts from this blog

grep: unknown device method

Uploading files to FTP/SFTP using CURL

How to find outgoing IP in Linux ?