2

I have written a signal handler to handle a SIG, and I want to kill the process if I get too many of it. So, which of the following code is better, or should I use them both?

  1. exit(-1); // or some other exit code
  2. kill(getpid(), SIGKILL);
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
maidamai
  • 712
  • 9
  • 26
  • 3
    The `man` pages don't explain the difference well enough? – Ken White Dec 20 '17 at 02:04
  • My personal preference is to `return` my way back out or throw a suitable exception if the exit is because of badness. Both are easier to deal with should you later find that you want to trap what used to be an exit condition, handle it, and continue. A forgotten `exit` in the code can be a nuisance. – user4581301 Dec 20 '17 at 02:30
  • For Linux, read [signal(7)](http://man7.org/linux/man-pages/man7/signal.7.html) and [signal-safety(7)](http://man7.org/linux/man-pages/man7/signal-safety.7.html) – Basile Starynkevitch Sep 03 '18 at 17:29

3 Answers3

5

You probably don't want either one, but what you do want is much closer to exit than to kill.

kill is something else coming in from the outside, and forcibly destroying a process. exit is the process itself deciding to quit executing. The latter is generally preferable.

As to why exit isn't the right answer either: most C++ code depends on destructors to clean up objects as you exit from a scope. If you call exit, that won't generally happen--you call exit, it exits to the OS, and no destructors get called in between (except things registered with onexit).

Instead, you generally want to throw an exception that's typically only caught in main, and exits gracefully when it is caught:

int main() { 
    try {
        do_stuff();
    }
    catch(time_to_die const &) {
    }
}

The advantage in this case is that when you do a throw time_to_die;, it automatically unwinds the stack, executing the destructors for all local objects as it goes. When it gets back to main, you get a normal exit, with all destructors having executed, so (assuming proper use of RAII) all your files, network connections, database connections, etc., have been closed as expected, any caches flushed, and so on, so you get a nice, graceful exit.

Short summary: as a rule of thumb, C++ code should never call exit. If your code is completely stuck in a crack, and you want to exit immediately, you want to call abort. If you want a semi-normal exit, do it by throwing an exception that will get you back to main so you can clean things and up and exit gracefully.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
3

Difference between exit and kill in C++

One difference is that kill function is not specified in the C++ standard library. It is merely specified in POSIX. exit is standard C++.

Another difference is that kill(getpid(), SIGKILL) will cause the operating system terminates the process forcefully. exit instead performs cleanup (by calling a atexit callback, and flushing streams etc.) and terminates the execution voluntarily.

So, which of the following code is better, or should I use them both?

Depends on the use case, but usually exit is more sensible, since one usually wants the cleanup that it provides.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • "Another difference is that kill function will send a signal that can be handled by the process before the operating system terminates the process forcefully" not `kill()`. This would happen when the system was shutting down, maybe in other process supervision scenarios, but not with the kill function as I know it. http://man7.org/linux/man-pages/man2/kill.2.html - kill just sends a signal ( not necessarily `SIGKILL` ) – erik258 Dec 20 '17 at 02:54
  • @DanFarrell surely `kill(getpid(), SIGKILL);` sends a `SIGKILL`? Or am I mistaken? – eerorika Dec 20 '17 at 09:13
  • It seems the specified signal, in that case, yes, KILL. However, KILL can't be handled and `kill` doesn't do any monitoring or subsequent killing – erik258 Dec 20 '17 at 15:24
  • @DanFarrell I don't know what you mean with *monitoring*, but it seems you're right that it won't call handlers. – eerorika Dec 20 '17 at 15:43
1

I would recommend exit(1).

Typically, an app would want to terminate gracefully if at all possible. SIGKILL is an instant death for your process - your exit handlers won't be called, for example. But in your case, you also have to call the getpid as well as the kill itself. exit instantly initiates the graceful exit process. It's the right choice for your needs.

There's rarely a good architectural reason to use SIGKILL in general. there's so many signals (http://man7.org/linux/man-pages/man7/signal.7.html in POSIX, you have SIGINT, SIGTERM, ... ) and to reiterate, there's no reason not to die gracefully if you can.

erik258
  • 14,701
  • 2
  • 25
  • 31