Anonymous user
IPC via named pipe: Difference between revisions
→{{header|C}}: add flag regarding whole process block
(→{{header|C}}: Edit comment: OpenBSD blocks the whole process.) |
(→{{header|C}}: add flag regarding whole process block) |
||
Line 18:
#include <limits.h>
#include <pthread.h>
/* Flag for systems where a blocking open on a pipe will block
entire process instead of just current thread. Ideally this
kind of flags should be automatically probed, but not before
we are sure about how each OS behaves. It can be set to 1
even if not needed to, but that would force polling, which I'd
rather not do.
Linux: won't block all threads (0)
OpenBSD: will block all (1)
Other OSes: ?
*/
#define WILL_BLOCK_EVERYTHING 0
size_t tally = 0;
Line 26 ⟶ 39:
char buf[32];
while (1) {
#if WILL_BLOCK_EVERYTHING
/* try to open non-block. sleep and retry if no reader */
fd = open("out", O_WRONLY|O_NONBLOCK);
if (fd <
sleep(1);
continue;
}
#else
/* block open, until areader comes along */
fd = open("out", O_WRONLY);
#endif
write(fd, buf, snprintf(buf, 32, "%d\n", tally));
close(fd);
|