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
/* This will block current thread. On Linux it won't block
/* try to open non-block. sleep and retry if no reader */
the other thread (the reader). On OpenBSD it blocks the
fd = open("out", O_WRONLY|O_NONBLOCK);
whole process (causing problems), because of a bug in the
if (fd < thread0) library.{ /* Whatassume happensit's onENXIO, other"no OS?reader" */
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);
Anonymous user