Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Pipes vs When to use Shared Memory

I am reading about various IPC mechanism. I am trying to figure out the scenarios, where we use Shared Memory and where we use named Pipes(FIFO).

Pipes: Multiple process can Write, however only one process can read. Write operation is atomic.

Shared Memory: Multiple process can read and write. And also user needs to provide mutual exclusion for read & write.

Is this the only difference of application of shared memory and pipe ?

like image 831
vamsi Avatar asked Mar 14 '12 12:03

vamsi


People also ask

Is shared memory faster than pipes?

IPC messages mimic the reading and writing of files. They are easier to use than pipes when more than two processes must communicate by using a single medium. The IPC shared semaphore facility provides process synchronization. Shared memory is the fastest form of interprocess communication.

What is the difference between pipe and shared memory?

Once Shared Memory is setup by the kernel there is no further need of kernel for the communication b/w process whereas in Pipe, data is buffered in the kernel space and requires system call for each access. Here, Shared Memory is faster than Pipe.

When should you use shared memory?

Shared memory provides an efficient way for multiple processes to share data (for example, control information that all processes require access to). Commonly, the processes use semaphores to take turns getting access to the shared memory.

Do pipes use shared memory?

A pipe is a section of shared memory that processes use for communication. The named pipe, which could be one-way or duplex (two-way) can offer both reading and writing services for the processes.


1 Answers

Essentially, pipes - whether named or anonymous - are used like message passing. Someone sends a piece of information to the recipient and the recipient can receive it. Shared memory is more like publishing data - someone puts data in shared memory and the readers (potentially many) must use synchronization e.g. via semaphores to learn about the fact that there is new data and must know how to read the memory region to find the information.

With pipes the synchronization is simple and built into the pipe mechanism itself - your reads and writes will freeze and unfreeze the app when something interesting happens. With shared memory, it is easier to work asynchronously and check for new data only once in a while - but at the cost of much more complex code. Plus you can get many-to-many communication but it requires more work again. Also, due to the above, debugging of pipe-based communication is easier than debugging shared memory.

A minor difference is that fifos are visible directly in the filesystem while shared memory regions need special tools like ipcs for their management in case you e.g. create a shared memory segment but your app dies and doesn't clean up after itself (same goes for semaphores and many other synchronization mechanisms which you might need to use together with shared memory).

Shared memory also gives you more control over bufferring 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.

Summary of most important points: pipes for one-to-one communication, less coding and letting the OS handle things, shared memory for many-to-many, more manual control over things but at the cost of more work and harder debugging.

like image 79
Michał Kosmulski Avatar answered Oct 09 '22 12:10

Michał Kosmulski