Is there a method to keep fwrite from blocking, like say using timeouts?
The scenario is this: I need to make a fwrite call (since I am largely reusing a code , I need to stick to it) on a network. However if I pull off the network cable, the fwrite blocks. Is there a workaround? Any help/suggestions will be greatly appreciated.
Switch an underlying file descriptor to non-blocking mode:
int fd = fdnum(stream);
int f = fcntl(fd, F_GETFL, 0);
f |= O_NONBLOCK;
fcntl(fd, F_SETFL, f);
After this a call to fwrite()
will immediately return and set errno
to EAGAIN if the file descriptor is not ready for being written to. Calling it in a loop and using sleeps you can easily implement whatever timeout behavior you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With