How to make a file descriptor blocking? How to make a file descriptor blocking? unix unix

How to make a file descriptor blocking?


Its been a while since I played with C, but you could use the fcntl() function to change the flags of a file descriptor:

#include <unistd.h>#include <fcntl.h>// Save the existing flagssaved_flags = fcntl(fd, F_GETFL);// Set the new flags with O_NONBLOCK masked outfcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK);


I would expect simply non-setting the O_NONBLOCK flag should revert the file descriptor to the default mode, which is blocking:

/* Makes the given file descriptor non-blocking. * Returns 1 on success, 0 on failure.*/int make_blocking(int fd){  int flags;  flags = fcntl(fd, F_GETFL, 0);  if(flags == -1) /* Failed? */   return 0;  /* Clear the blocking flag. */  flags &= ~O_NONBLOCK;  return fcntl(fd, F_SETFL, flags) != -1;}