Introduction to Interprocess Communication

OS Laboratory Team
Fall 2018
  • Taxonomy of IPC Facilities
  • Comminucation

Taxonomy of IPC Facilities

  • Comminucation
  • Synchronization
  • Signals
Communication
These facilities are concerned with exchaning data between processes
Synchronization
These facilities are concerned with synchronizing the actions of processes or threads
Signals
Although signals are intended primarily for other purposes, they can be used as a synchronization technique in certain circumestances
IPC

Comminucation

  • Pipes
  • FIFOs
  • Shared Memory

The various comminucation facilities allow processes to exchange data with one another

Data-transfer facilities
The key factor distinguishing these facilities is the notion of writing and reading. In order to communicate, one process writes data to the IPC facility, and another process reads the data
Shared memory
Shared memory allows processes to exchange information by placing it in a region of memory that is shared between the processes. Shared memory can provide very fast comminucation because it doesn't require system calls or data transfer between user and kernel memory.

DPDK is a set of libraries and drivers for fast packet processing

DPDK

We can further break data-transfer facilities into the following subcategories

  • Byte Stream
    • pipes
    • FIFOs
    • stream sockets (TCP)
  • Message
    • System V message queue
    • POSIX message queues
    • datagram sockets (UDP)
  • Pseudoterminals

Let's code :)

Pipes as a Byte Stream Data Transfer Facility


          #include <unistd.h>

          int pipe(int filedes[2]);
          /*
           * Returns 0 on success, or -1 on error
           */
          

          #include <stdio.h>

          FILE *popen(const char *command, const char *mode);
          /*
           * Returns file stream or NULL on error
           * mode is w: calling process write to command
           * mode is r: calling process read from command
           */

          int pclose(FILE *stream);
          /*
           * Returns termination status of child process, or -1 on error
           */
          

Separating messages in a byte stream:

  • delimiter character
  • header with length field
  • fixed-length messages

Synchronization between the reader and writer processes is automatic

Let's code :)

FIFOs as a Byte Stream Data Transfer Facility

FIFOs == Named pipes !


          #include <sys/stat.h>

          int mkfifo(const char *pathname, mode_t, mode);
          /*
           * Returns 0 on success, or -1 on error
           */
          

Although shared memory provides fast communication, this speed advantage is offset by the need of synchronize operations on the shared memory.

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

Redis

Let's code :)

Shared Memory :|

shm API description
shmget allocate shared memory segment
shmat attach to the shared memory with the given shared memory identifier
shmctl perform control operations on the shared memory segment
shmdt detaches from the shared memory

To use the above API we must include <sys/ipc.h> and <sys/shm.h> header files.


          int shmget(key_t key, size_t size, int shmflg);
          

shmget returns the shared memory ID on success.

  • The first argument key must be unique. This key can be generated using the ftok() call.
  • The size argument is the size of the shared memory segment (it is rounded to the multiples of PAGE_SIZE. Usually PAGE_SIZE is 4k).
  • The shmflg is usually set with the IPC_CREAT flag.
  • If the key already exist, the errno is set to EEXIST and returns -1.

ICPS !?

ipcs - show information on IPC facilities

-m, --shmems
Write information about active shared memory segments.
-q, --queues
Write information about active message queues.
-s, --semaphores
Write information about active semaphore sets.
-a, --all
Write information about all three resources (default).

          void *shmat(int shmid, const void *shmaddr, int shmflg);
          

The API shmat performs the attachement to the shared memory segment.

  • the first argument shmid is the id returned from shmget.
  • the second argument is the attach address, and is usually kept to NULL.
  • the shmflg is also kept to 0 when doing read and write operations on the shared memory.

References

Linux Systems Programming with C Sample
Linux Programming Interface, by Michael Kerrisk, 2010