3

I'm trying to do some inter program data exchange and I decided to play a bit with named pipes. I have a very basic reader and a very basic writer, taken from the example answer here.

  • My first problem is that I cannot use two time write(...) unless I let the program sleep. Is there a way around that? Having it call sleep(1) after every entry will explode the process time.

  • I would also like to have more than one process write into the same fifo. Is that possible? My attempts returned quite a few issues...

  • Finally is there a way to have a fifo carry an object instead of a string?

Community
  • 1
  • 1
Ant
  • 1,083
  • 1
  • 15
  • 35
  • _'C++ named pipes'_ there's no such thing?!? – πάντα ῥεῖ Apr 16 '14 at 19:04
  • If I add an "&" between C++ and named pipes will it be better? – Ant Apr 16 '14 at 19:06
  • 1
    What do you mean by "I cannot use two time write()"? You should be able to write repeatedly - it's just that once the pipe buffer fills up, `write()` will block until something `read()`s from the pipe to make some space... Make sure to check the return value of `write()` and also check `errno` when appropriate. – twalberg Apr 16 '14 at 19:31

1 Answers1

2

1) Writing - check the result of the write. If the other end of the pipe is not open, the number of bytes written will be 0.

2) Not possible but you can have one server fifo for receiving requests. It then generates a pipe name for each client process to connect to and sends it to the client. The client reads the server pipe, closes the server pipe and opens a pipe with the new name given by the server. The server will have to poll all the pipes in separate threads.

3) It is just a write - it can take anything. Just dump the whole object in binary. Note that there should not be any pointers in the object as the pointers will not be pointing to the same address space.

cup
  • 7,589
  • 4
  • 19
  • 42
  • I don't get your first point. You mean have it try to write until I don't get an exception? About 2 and 3, thanks. You answered my questions – Ant Apr 16 '14 at 19:19
  • write returns the number of bytes written. If the other end of the pipe is not open, it will return 0. You can use usleep from unistd.h if you don't wish to wait one whole second before you try again. It won't throw an exception: this is C: not C++. – cup Apr 16 '14 at 19:26