2009-05-27 16 views

答えて

11

私はCと遊んだので、そのはしばらくして、しかし、あなたはファイルディスクリプタのフラグを変更するfcntl()機能を使用することができます。

#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

これは受け入れられる方法です。 〜O_NONBLOCKでfcntlをやっていることに対する良い答えと素敵な、簡潔なアプローチ。 :) – BobbyShaftoe

7

私は単にO_NONBLOCKフラグを戻すべきであるが、設定期待ファイルディスクリプタをブロックしているデフォルトモードに変更します。

/* 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; 
} 
関連する問題