Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between shared memory and pipe in unix?

what is main difference between shared memory and pipe in unix programming?

like image 238
bunty Avatar asked Sep 09 '10 11:09

bunty


People also ask

Is pipe a shared memory?

A pipe is a section of shared memory that processes use for communication. The process that creates a pipe is the pipe server. A process that connects to a pipe is a pipe client. One process writes information to the pipe, then the other process reads the information from the pipe.

Is shared memory faster than pipe?

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.

Is pipe message passing or shared memory?

It's message passing. You specify a buffer to write into the socket buffer, and you find out how much space it has available beforehand using getTxAvailable() or whatever. It's not really shared memory as it does a buffer blit operation to help encapsulation of the socket.

What is the difference between shared memory and message queue?

Message queue has inherent synchronization overhead, guarantee of safety at cost of performance. Shared memory has no safeguards - if two threads access it simultaneously, they will possibly conflict (write inconsistent data) unless you assure thread safety yourself.


2 Answers

Taken from this article

(unnamed) Pipes

  • Can switch between blocking and non-blocking mode
  • Don't have to free them when done
  • Are automatically inherited by children
  • Must read and write in a linear fashion

Shared Memory

  • Can store structures
  • Won't ever block - positive
  • Can have as many programs read or write to it as you need
  • Won't ever block - negative: must use semaphores or your own spin-locks
  • It's possible for it to not be freed even when all programs exit
like image 198
Sachin Shanbhag Avatar answered Sep 20 '22 13:09

Sachin Shanbhag


Shared Memory Vs Pipe-

Kernel-

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. It is a major drawback of the pipe becuase IPC is important for the computational speed up.

Communication

Shared Memory- Bidirectional whereas Pipe(unnamed Pipe)- Unidirectional.

Reliable

Shared Memory- Less Reliable(data mix up) whereas Pipe is more Reliable as data is buffered and is under control of the kernel.

Hope you like it.

like image 20
Yash Avatar answered Sep 21 '22 13:09

Yash