Question

Say i have a fifo (named pipe) which gets open multiple times from multiple processes or threads. All of them will call select() for read concurrently. If a writer comes in and writes, say one byte to this fifo, will all blocked select() calls return simultaneously or will only one thread be chosen and return from select()?

Is there any specification on this or would this be system-dependent?

The reason i m asking is that i want to implement Windows-like manual reset events using fifos. Manual reset event requires that when event is signaled, all waiting threads will be released (not just one as for auto-reset event).

I could use pthread conds for this, but my requirement is for events to be shared by multiple processes.

Thanks.

Was it helpful?

Solution

If a writer comes in and writes, say one byte to this fifo, will all blocked select() calls return simultaneously or will only one thread be chosen and return from select()?

There will be a race condition. System will start to wake threads in some random order until one of them reads the data, emptying the buffer and preventing other threads from awaking.

Is there any specification on this or would this be system-dependent?

It will depend on the system, the app layout, the code structure. It will be one great undefined behavior.

The reason i m asking is that i want to implement Windows-like manual reset events using fifos.

Are you coding on Linux/Posix? Use the tee function instead of read, make the threads write some status code back into the pipe to signal that they have awaken. Then, reset the pipe by reading data back in the control thread.

I.e., do the following:

In the working threads: select (and block). In the control thread: write one byte. Now the select is not blocking. The working threads get awaken. Now do an optional tee (one byte) in the working threads and write one byte back to the pipe in each thread. In the control thread: read and count bytes. If all the working thread wave awaken, do one more byte read to erase the first byte you wrote. The select now blocks again.

Note that you can not enter the select again until you complete the whole procedure. You might need two such pipes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top