From: Gary Wright Date: 2009-05-09T00:31:28+09:00 Subject: Re: how to find and close opened file descriptor On May 7, 2009, at 11:54 AM, Oliver Peng wrote: > During my testing, I found that ruby doesn't create IO object for each > opened file descriptor which are inherited from parent process. So we > have to close all these opened file descriptor except stdin, stdout > and > stderr by following way: > > 3.upto(1023) do |fd| > begin > if io = IO::new(fd) > io.close > end > rescue > end > end > > But I really don't like this way. Is there better way that I can find > all opened file descriptors. I believe this is the standard idiom on *nix type systems to close all file descriptors when you have no particular knowledge of which ones are open. In other words there is no standard way to ask the OS for a list of open descriptors via a simple system call. A possible alternative is to parse the output of utilities like 'lsof' to determine what file descriptors are open or to dig into the various platform specific ways that those utilities query the kernel for this information. Gary Wright