Unix select system call in Rust

Viewed 336

I have a list of file descriptors and I want to find what all are ready to read. So far I was doing it python using below function select.select(read_fds, [], [], 1) and it returns list of ready to read streams. The same thing I am trying to do in Rust. I am using nix crate but there the function select only returns the number of file descriptors ready to read. Is there a way to get the list of file descriptors also?

1 Answers

select is a C function which modifies the arguments you pass to it (the file descriptor sets) in-place, rather than allocating and returning an entirely new structure. nix::select is just a thin wrapper for that C function. Python is really the outlier here, as it deviates from the C API by allocating entirely separate lists containing the results.

When using nix::select, you should check the FD sets you pass in - they will have been modified to contain only the ready file descriptors.

let mut readfds = FdSet::new();
readfds.insert(0);
readfds.insert(3);
let _ = select(&mut readfds, None, None)
for fd in readfds.fds(None) {
    println!("{:?}", fd);
}

FdSet::fds returns an iterator over the file descriptors in the set.

Related