|
reading from named pipes versus urandom
I am trying to write (in C++) something that behaves similarly to /dev/urandom, but using named pipes. After a lot of cursing I came up with a "server" program that opens a named pipe for reading, then opens it for writing, then starts writing bytes in an infinite loop. This allowed me to meet much of the demand I had for the "server":
1. Since the pipe is always open for reading by at least one process, it never fails with SIGPIPE due to others opening and/or closing it for reading.
2. Many processes can open the pipe and read from it simultaneously, as many as the OS will bear.
But I noticed that when I run two simultaneous readers, they seem to read the same bytes. That is, the "server" writes one stream and both readers get to see the entire stream. As I increased the number of readers (to about 5), the readers started to see different streams (I am guessing that some bytes have enough time to get removed from the pipe while a reader is waiting for its turn). I don't know if it matters, but I have 4 cores.
I have a couple of questions.
Is there any way to prevent the pipe data from being read twice?
Does /dev/urandom have the same behavior, either theoretically or practically? That is, can two processes ever read the same byte from /dev/urandom if they do it simultaneously enough?
|