C get all open file descriptors

前端 未结 4 1906
南旧
南旧 2021-01-13 07:46

I want to implement behavior in my C program so that if a SIGINT happens, I close all open file descriptors. Is there a simple way to get a list of them?

4条回答
  •  半阙折子戏
    2021-01-13 08:17

    My solution for POSIX systems:

    All opened fd's are the lowest value possible.
    Make a wrapper function upon open(2).
    Your new function open (and return) the requested fd and pass his value to a function called define_if_is_the_higtest_fd_and_store_it().
    You should have a int hightest_fd_saved accessible only for a singleton function (there is only 1 'descriptor table') named save_fd() (initial value is 3 (cuz stderr is 2)).
    Configure SIGINT to your signal function. Inside, you do a loop from [3, return_fd()].

    I think that's it...

提交回复
热议问题