2009-05-27 11 views

risposta

11

Il suo stato un po 'che ho giocato con la C, ma è possibile utilizzare la funzione di fcntl() di cambiare le bandiere di un descrittore di file:

#include <unistd.h> 
#include <fcntl.h> 

// Save the existing flags 

saved_flags = fcntl(fd, F_GETFL); 

// Set the new flags with O_NONBLOCK masked out 

fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK); 
+0

Sì, questo è il metodo accettato. Una buona risposta e un approccio gradevole al fare il fcntl con il ~ O_NONBLOCK. :) – BobbyShaftoe

7

mi aspetterei semplicemente non impostando il flag O_NONBLOCK dovrebbe ritornare il descrittore di file alla modalità predefinita, che blocca:

/* 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; 
} 
Problemi correlati