Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active September 14, 2017 08:35
Show Gist options
  • Save kjunichi/a25a1667c8f681af6bb2bbbc854a4a74 to your computer and use it in GitHub Desktop.
Save kjunichi/a25a1667c8f681af6bb2bbbc854a4a74 to your computer and use it in GitHub Desktop.
2017/9/14

open済みのFDをノンブロッキングにするには

fcntl(fd, F_SETFL, O_NONBLOCK) 
extern crate libc;
use std::{thread, time};
fn main() {
        println!("Hello, world!");
        unsafe {
                libc::fcntl(0, libc::F_SETFL, libc::O_NONBLOCK);
        }
        let mut buf:[libc::c_char;1] = [0;1];
        let mut ptr = &mut buf;

        loop {
                unsafe {
                        let r = libc::read(0, ptr.as_ptr() as *mut libc::c_void, 1);
                        if r > 0 {
                                println!("input !{:?}",*ptr);
                                break;

                        }
                        thread::sleep(time::Duration::from_millis(100));

                }
        }
}

関連

アクセス解析タグ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment