7

I am new to C programming. I used to think using exit() was the cleanest way of process termination (as it is capable of removing temporary files, closing open files, normal process termination...), but when I tried man exit command on the terminal (Ubuntu 16.04.5, gcc 5.4.0) I saw the following line:

The exit() function uses a global variable that is not protected, so it is not thread-safe.

After that I tried to make some research about better replacement for exit() (to change my programming behavior from the beginning). While doing that I faced with this question in which side effects of exit() is mentioned and it is suggested to use atexit() properly to solve the problem (at least partially).

There were some cases in which using abort() was preferred over exit(). On top of that, this question suggests that atexit() might also be harmful.

So here are my questions:

  • Is there any general and better way of process terminating (which is guaranteed to clean like exit() and is not harmful for the system at any case)?
  • If the answer to the first question is NO!, what is the best possible way of process terminating (including the cases in which they are most useful)?
alk
  • 69,737
  • 10
  • 105
  • 255
Adhamzhon Shukurov
  • 681
  • 1
  • 8
  • 21
  • 3
    On modern protected systems like Linux, macOS and Windows, most resources allocated by a process is deallocated not by `exit` but by the operating system itself. And the best way to not risk any resource leaks is still to clean up after yourself: Any resource you allocate or create, you need to deallocate or free or destroy before returning from the `main` function. – Some programmer dude Mar 08 '19 at 12:41
  • 3
    When you start writing multi-threaded programs is a good time to start worrying about the thread (non-)safety of `exit()`. Even more, be skeptical of the value of exit-handler functions and `atexit()`. Who really cares if `exit()` is not thread-safe when its effect is to terminate the program? Only under special circumstances -- prabably involving `atexit()` -- does it make sense to worry about that. – John Bollinger Mar 08 '19 at 13:13
  • Also, if there were a general need for something better than `exit()`, such functionality would exist. If there were general problems with `exit()`, they would be fixed. – Andrew Henle Mar 08 '19 at 13:27
  • 1
    calling free() to return virtual memory to a heap in an address space that is going to cease to exist is a waste of time, both CPU and elapsed. –  Mar 09 '19 at 02:50

2 Answers2

5

what is the best possible way of process terminating

  1. If going single threaded just use exit(), as your code is not going multi-threaded.
  2. Else make sure all but one thread have ended before the last thread and then safely call exit() because of 1. above.
alk
  • 69,737
  • 10
  • 105
  • 255
2

Given that power/hardware fails can happen at any time, the imposs.. extreme difficulty of reliably terminating threads with user code and the chaotic nature of the use of memory pools etc. in many non-trivial multithreaded apps, it is better to design apps and systems that can clean temp files etc. on start-up, rather than trying to micro-manage shutdown.

'Clean up all the resources you allocate before you exit' sounds like good advice in a classroom or lecture, but quickly becomes a whole chain of albatross round your neck when faced with a dozen threads, queues and pools in a continually changing dynamic system.

If you can, if you are running under a non trivial OS, let it do its job and clean up for you. It's much better at it than your user code will ever be.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • "*design apps and systems that can clean temp files etc. on start-up*" good point! Still, a nice and clean shutdown should be part of the app's design as well. If this is not possible something is broken. – alk Mar 09 '19 at 14:59