If a file is being opened by a process and it is not being closed by any of the processes, will the OS automatically close that open file?
Asked
Active
Viewed 1,755 times
1 Answers
10
The operating system doesn't mess around with the open/closed state of file descriptors while an executable is running. However, once a process terminates (whether gracefully or ungracefully), if there are remaining open file descriptors, they will be destroyed along with the process image.
As a general rule, though, you should always explicitly close them. A major advantage is that this makes it obvious that the files can be closed, so when you refactor/change the code, you have an understanding of where they can be closed instead of leaving it to the operating system to do it after termination.

Chris Down
- 125,559
- 25
- 270
- 266