2009-05-27 6 views

Répondre

11

Son été un moment que je jouais avec C, mais vous pouvez utiliser la fonction fcntl() pour changer les drapeaux d'un descripteur de fichier:

#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

Oui, c'est la méthode acceptée. Bonne réponse et bonne approche pour faire le fcntl avec le ~ O_NONBLOCK. :) – BobbyShaftoe

7

J'attendre tout simplement pas mettre le drapeau O_NONBLOCK devrait revenir la le descripteur de fichier au mode par défaut, qui bloque:

/* 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; 
} 
Questions connexes