Inter-process communication under Linux 🐧

Inter-process communication under Linux 🐧

There are roughly the following types of inter-process communication under Linux:

  • socket
  • Pipes (including anonymous pipes for parent and child processes, named pipes)
  • Semaphore
  • Shared memory
  • Message queue

socket

Socket can be used for local inter-process communication or remote inter-process, and is the most commonly used.

Pipeline – Anonymous Pipeline

It is generally created with pipe and communicates between father and son. Close the read/write pipeline in the parent process, and close the write/read pipeline in the child process.

Pipes-named pipes

Generally use mkfifo to create. If it is not stated that the pipe is non-blocking, then a pipe opened for reading will block until a process opens the FIFO for writing. Similarly, if a pipe opened for writing will block until a process opens this FIFO for reading.

Semaphore

A process can use kill to send a signal to another process.

Shared memory

Use functions:

shmget, shmat, shmctl, shmdt

Use shmget function to get a piece of memory, and use shmat to set it to allow this process to use this shared memory. Use shmdt to delete the memory block after use.

Message queue

Use functions:

msgget, msgsnd, msgrcv, msgctl

The use steps are the same as above.

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.