Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are posix pipes lightweight?

Tags:

c

linux

posix

pipe

In a linux application I'm using pipes to pass information between threads.

The idea behind using pipes is that I can wait for multiple pipes at once using poll(2). That works well in practice, and my threads are sleeping most of the time. They only wake up if there is something to do.

In user-space the pipes look just like two file-handles. Now I wonder how much resources such a pipes use on the OS side.

Btw: In my application I only send single bytes every now and then. Think about my pipes as simple message queues that allow me to wake-up receiving threads, tell them to send some status-data or to terminate.

like image 437
Nils Pipenbrinck Avatar asked Mar 28 '10 14:03

Nils Pipenbrinck


1 Answers

No, I would not consider pipes "lightweight", but that doesn't necessarily mean they're the wrong answer for your application either.

Sending a byte over a pipe is going to require a minimum of 3 system calls (write,poll,read). Using an in-memory queue and pthread operations (mutex_lock, cond_signal) involves much less overhead. Open file descriptors definitely do consume kernel resources; that's why processes are typically limited to 256 open files by default (not that the limit can't be expanded where appropriate).

Still, the pipe/poll solution for inter-thread communication does have advantages too: particularly if you need to wait for input from a combination of sources (network + other threads).

like image 141
David Gelhar Avatar answered Oct 03 '22 11:10

David Gelhar