10

In my application I open a file, using the open() call.

My questions are:

  • Is the file automatically closed (as using the close() call on the returned file descriptor) if I kill the process?
  • What happens if the application crashes (e.g. segmentation fault)?
  • Is this documented somewhere?

2 Answers 2

15

Yes, the file will be automatically closed when the process terminates, regardless of the reason for the process termination.

This is documented in POSIX. In “Consequences of Process Termination”, among other consequences:

  • All of the file descriptors, directory streams, conversion descriptors, and message catalog descriptors open in the calling process shall be closed.

And in “Terminating a Process”:

It is important that the consequences of process termination as described occur regardless of whether the process called _exit() (perhaps indirectly through exit()) or instead was terminated due to a signal or for some other reason.

2

You could read about Linux Kernel Process Management, e.g. here

Process Termination

When a process terminates, the kernel releases the resources owned by the process and notifies the child's parent of its demise.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .