tags:

views:

60

answers:

2

As part of my Uni course we've been shown and asked to use pipes to communicate between processes (using pipe() and fork()) for a few small exercises. No problems getting it to work or with the concept, but outside of these requirements I'm wondering how efficient it is to write and read with a pipe of this type?

If I've some value that I'm packing in to a block of 4 bytes, is it better to pack and write 100 values (so 400 bytes) all at once?

Or is the performance comparable if I do 100 writes each of 4 bytes?

Does piping 400 bytes in one go prevent the reciever doing anything until the write is complete - assuming the reciever is only trying to read the first 4 bytes, will it be able to do so after they're written, but before all 400 is finished?

+3  A: 

Pipes are handled similarly to files (of course, they're not on the disk). They are buffered in some same ways, and the handle to them is treated similarly. Don't try to manually buffer for a pipe; it does that already. If you're really concerned about it, you could try both ways and collect some run times. I'd suggest just writing blocks of 4 bytes as you have them. Don't try to optimize unless you know there's a problem.

JoshD
It was more of a curiosity than a concern, and this is pretty much the answer I was hoping to get. Thanks!
DMA57361
+1  A: 

I can tell you an anecdote. I once worked on a web system for an embedded Linux platform. We tried pipes for internal communication, because they were much faster than UNIX socket or TCP sockets. In the end we used sockets anyway, because they were simpler to use in our application. (We needed two-way communication and pipes are only one way.)

Amigable Clark Kant