C get all open file descriptors

前端 未结 4 1862
南旧
南旧 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:03

    Keep track of all of your open file descriptors and close them individually.

    In the general case, a library you're using might have an open file, and closing it will cause that library to misbehave.

    In fact, the same problem could exist in your own code, because if you close file descriptors indiscriminately but another part of your program still remembers the file descriptor and tries to use it, it will get an unexpected error or (if other files have been opened since) operate on the wrong file. It is much better for the component responsible for opening a file to also be responsible for closing it.

提交回复
热议问题