Mandar
Mandar

Reputation: 733

exit() vs _exit() : Does calling _exit() ensures closing of all open fd and sockets?

I have used exit() to terminate the process. I do not have any exit handlers registered also I do not care about flushing buffers on exit, so thought of using _exit() as more robust method to terminate process.

The only question is, does _exit() handles closing of all open file descriptors and open sockets gracefully?

Upvotes: 2

Views: 1910

Answers (2)

blaze
blaze

Reputation: 4364

Yes, it is. Actually, on most platforms, operating system closes files and sockets for terminated process, so it doesn't matter if it finished with _exit(), exit(), assert(false) or kill -KILL.

Note that stdio FILE * streams are NOT closed correctly by _exit() and any unwritten (still buffered) data will be lost.

Upvotes: 3

cnicutar
cnicutar

Reputation: 182664

The function exit calls _exit. From TLPI:

The following actions are performed by exit():

  • Exit handlers are called
  • The stdio stream buffers are flushed
  • The _exit() system call is invoked

The standard page for _exit says this:

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

Upvotes: 6

Related Questions