I have two processes communicating via a pty, nonblocking. Problem is that the fread()
on the master fails when there is no data available to process.
How can I ignore the "no reader/data present" case when reading from the unconnected file descriptor on the master side? I suspect there is some flag for open()
or fcntl()
which I skipped during reading?
// initialization:
int pty_fd = posix_openpt(O_RDWR | O_NOCTTY);
int rc = grantpt(pty_fd);
rc = unlockpt(pty_fd);
fcntl(pty_fd, F_SETFL, O_NONBLOCK);
fd_read = fdopen(pty_fd, "r");
// now, trying to read will fail if no data is present:
char buf[100];
int count = sizeof(buf);
size_t bytesRead = fread(buf, sizeof(char), count, fd_read);
if ((bytesRead == 0) && (ferror(fd_read)) {
printf("fail...\n");
}
Sure, I can ignore the return value of ferror()
, but I suppose this is not the right way to use this function.
Ah, one thing: I found the POLLHUP trick on stackoverflowz. It works but is racy and hence not suitable for my case...
Greetings