iFantasticLife


> ping my.next.stop
Destination unreachable...

Inter-process communication - Part 3

28 Apr 2021 - Programming

In my previous blogs, we have already discussed four IPC methods. Today I will continue this topic by talking about the other three IPC methods: message queue, shared memory, and sockets.

2. Different ways for IPC (Cont’d)

There are several ways for IPC, including pipe, FIFO, message queue, signal, and socket etc.

2.4. Message Queue

A message queue is a linked list of messages. It is stored in the kernel and identified by a queue ID. To create a new message queue or open an existing queue, we can use msgget(). To add a new message to the end of a queue or to fetch a message from the queue, we can use msgsnd() and msgrcv().

Unlike pipe and FIFO, in which messages are always read in sequence, a message can be fetched from a queue based on a message type, which is specified when calling msgsnd(). As such, they don’t have to follow the first-in first-out order, which makes it more flexible.

A message queue stays within the kernel. Once created, it will stay there until the kernel is restarted or the queue is explicitly removed by calling msgctl() with its queue ID and a command parameter IPC_RMID.

Reference

2.5. Shared Memory

Shared memory allows multiple processes to share a given region of memory. One process writes data into the region and others read data from the region. One of the advantages of using shared memory is that it is very efficient, because the data does not need to be copied between the client and the server. They can read or write the data directly just like reading or writing a block of memory. For other IPC forms such as pipe and message queue, the data must be copied between user spaces and kernel at both the server side and client side.

When using shared memory, all processes must be synchronized to access the region. This can be achieved by using semaphores or a lock.

There are two different ways to use shared memory:

Reference

2.6. Sockets

Unlike other IPC methods we discussed earlier, which only support communication between processes running on the same host, socket is a type of network IPC that allows processes running on different hosts to communicate with each other. This is also the most widely used IPC method across different platforms.

I bet you can find lots of online resources that talk about sockets, so here I will save my space and not jump into it.

Reference:

3. Summary

This concludes the series of the IPC topic. We have seen different IPC forms, including pipes, FIFO, signals, semaphores, message queues, shared memory, and sockets. There are still lots of details to talk about for each method. However, I hope that this series can be a good start for you.


«Prev More About Next»
Natas19 - Improper Session Management Part 2 Programming Natas20 - Session Management Part 3

Please leave your comments below.