2009-05-27 12 views

Respuesta

11

Ha sido un tiempo desde que jugué con C, pero se puede usar la función fcntl() para cambiar las banderas de un descriptor de archivo:

#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í, este es el método aceptado. Buena respuesta y buen enfoque para hacer el fcntl con ~ O_NONBLOCK. :) – BobbyShaftoe

7

Yo esperaría que simplemente no se establecía la marca O_NONBLOCK debe revertir la descriptor de archivo en el modo predeterminado, que es el bloqueo:

/* 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; 
} 
Cuestiones relacionadas