IPC via named pipe: Difference between revisions

→‎{{header|C}}: If WILL_BLOCK_EVERYTHING, then prevent the "in" pipe blocking everything.
(→‎{{header|C}}: add flag regarding whole process block)
(→‎{{header|C}}: If WILL_BLOCK_EVERYTHING, then prevent the "in" pipe blocking everything.)
Line 31:
 
#define WILL_BLOCK_EVERYTHING 0
 
#if WILL_BLOCK_EVERYTHING
#include <poll.h>
#endif
 
size_t tally = 0;
Line 43 ⟶ 47:
fd = open("out", O_WRONLY|O_NONBLOCK);
if (fd < 0) { /* assume it's ENXIO, "no reader" */
sleepusleep(1200000);
continue;
}
#else
/* block open, until areadera reader comes along */
fd = open("out", O_WRONLY);
#endif
Line 65 ⟶ 69:
size_t len;
char buf[PIPE_BUF];
#if WILL_BLOCK_EVERYTHING
struct pollfd pfd;
pfd.events = POLLIN;
#endif
while (1) {
#if WILL_BLOCK_EVERYTHING
fd = pfd.fd = open("in", O_RDONLY|O_NONBLOCK);
fcntl(fd, F_SETFL, 0); /* disable O_NONBLOCK */
poll(&pfd, 1, INFTIM); /* poll to avoid reading EOF */
#else
fd = open("in", O_RDONLY);
#endif
while ((len = read(fd, buf, PIPE_BUF)) > 0) tally += len;
close(fd);
}
Anonymous user