Getting the highest allocated file descriptor

后端 未结 5 611
傲寒
傲寒 2020-11-29 01:25

Is there a portable way (POSIX) to get the highest allocated file descriptor number for the current process?

I know that there\'s a nice way to get the number on AIX

5条回答
  •  一生所求
    2020-11-29 02:00

    The POSIX way is:

    int maxfd=sysconf(_SC_OPEN_MAX);
    for(int fd=3; fd

    (note that's closing from 3 up, to keep stdin/stdout/stderr open)

    close() harmlessly returns EBADF if the file descriptor is not open. There's no need to waste another system call checking.

    Some Unixes support a closefrom(). This avoids the excessive number of calls to close() depending on the maximum possible file descriptor number. While the best solution I'm aware of, it's completely nonportable.

提交回复
热议问题