Christian Wagner
Christian Wagner

Reputation: 261

Inter-process communication using pipes

I think maybe this is an obvious question, but I just want to be sure by asking you guys.

I'm working with parent-child process communication using the pipe system call to create a unnamed pipe.

My child process needs to gather some information and send it to its parent. My questions are:

  1. Can I only send and receive strings using the write and read functions, right? I have to forget about sending structures.
  2. If the answer to my previous question is "yes", the right way to transfer all the information to the parent process is to call the functions write and read several times?

Thank you very much!

Upvotes: 0

Views: 1487

Answers (1)

geekosaur
geekosaur

Reputation: 61369

You can write() and read() structs just fine; use a pointer to the struct as the buf parameter. It's when you want to do this between processes not running on the same machine that you run into problems and need to do marshaling/unmarshaling to portable representations to insure the values are understood the same way everywhere. This includes recognizing the start and end of data "packets", since a pipe doesn't really have the concept of packets: if all you're doing is writing a series of identical structs, then you can just write() them and the reader can rely on read() returning 0 to indicate the end of the series; but if you need to send other information as well then you'll need a framing protocol to say "what follows is such-and-such struct", "what follows is a string", etc.

Upvotes: 2

Related Questions