Can I kill a process from itself?

34,854

Solution 1

Yes, you can. There's even a specific function for it — raise(sig) — although kill(getpid(), sig) will work too.

Solution 2

you can call your own process using kill through:

kill(getpid(),SIGINT);

For more information take a look at this

This would have a similar effect to exit() command.

Solution 3

Try exit - a lot simpler - why make things complicated?

Solution 4

I suspect there's a larger problem with your design choices.

If you want to execute some code when your process terminates, register the code with atexit.

That said, yes, you can send your own process a signal with kill(getpid(), sig).

Solution 5

You can use kill(getpid(), SIGSPEC) to do this properly to execute the code that's actually installed as a signal handler for any particular signal specified by SIGSPEC.

You cannot of course capture SIGKILL or SIGSTOP those cannot have handlers. All other signals can have handlers installed using the signal code.

If the handler code is not a signal handler but an atexit handler then it will only be called via exit() call. Note that _exit() call bypasses all atexit handlers.

Also i see a few comments here that seem to suggest that kill(getpid(), SIGSPEC) is the same as _exit() or exit() IT IS NOT! They are different things.

My suggestion would be to read exit(3) _exit(2) signal(7) signal(2) raise(3) sigaction(3) man pages for a complete understanding.

Share:
34,854

Related videos on Youtube

Francesco Belladonna
Author by

Francesco Belladonna

A passionate software developer with great interest in software architecture and dedicated to deliver highly maintainable, durable and scalable software. Full-stack experience on the web platform, with a preference toward the back-end. Has expertise planning and managing work for multiple teams.

Updated on September 15, 2020

Comments

  • Francesco Belladonna
    Francesco Belladonna over 3 years

    I have some code that is executed when the process is killed, can I actually call kill(getpid()) to force the execution of this code (and close the process obviously)?

    • Jack Leow
      Jack Leow over 12 years
      Wouldn't it be easier to just exit()?
    • Brandon Buck
      Brandon Buck over 12 years
      Couldn't you just add a function in that calls the code you want to perform before it exits and then call exit()? Since you need to process that code first.
    • Bill Lynch
      Bill Lynch over 12 years
      An alternative approach to this could involve using on_exit()
  • Francesco Belladonna
    Francesco Belladonna over 12 years
    As I wrote in my question it's because I'm handling some code through signals and I want make the exit code standard, any problem in killing process through kill so?
  • Ed Heal
    Ed Heal over 12 years
    You can give an exit code. Just use the signal handling to tidy up then exit with a status code.
  • Francesco Belladonna
    Francesco Belladonna over 12 years
    It's not a design, it's a workaround for fixing a design change
  • tdenniston
    tdenniston over 12 years
    Well, it is a design choice. If you have to create a workaround like this, you should re-examine your original design choices.
  • Francesco Belladonna
    Francesco Belladonna over 12 years
    It was something that weren't requested, that's why I thought about this workaround, however they changed their mind about this another time and I had to change a lot of things, I'm going mad
  • Ahmed Masud
    Ahmed Masud over 12 years
    I would like to point out that kill(getpid(), SIGINT) DOES NOT have the same effect as exit().
  • Neowizard
    Neowizard over 11 years
    raising SIGINT, unlike exit(), will transfer the process control to any registered handler, and also it might result in a core-dump file - to name a couple of the differences between exit() and raising a SIGINT.
  • Daniel
    Daniel almost 11 years
    @AhmedMasud That's why the text in the link said similar and not same ("Same" means they are Identical with no differences, "similar" means they are almost identical but have some minor differences) ;)
  • Andre Holzner
    Andre Holzner over 10 years
    one use case of sending a SIGINT to yourself is e.g. in an assert macro when you run the application in a debugger. When an assertion fails, the debugger will become active and you can inspect the call stack, values of variables etc.
  • Francesco Belladonna
    Francesco Belladonna over 10 years
    I noticed this new answer only today but I think it's better.
  • Erich Kitzmueller
    Erich Kitzmueller almost 7 years
    There is at least one case when using exit() is not desirable: after a fork where the child tries to exec another program, but fails (for whatever reason). Calling exit in the child causes routines installed by on_exit to be executed, which causes unwanted side effects since only the parent process should do that stuff (e.g. closing a database connection).
  • Kratz
    Kratz about 3 years
    Just a word of caution: if you use this in a multi threaded environment and you want to send a signal to your signal handling thread raise(sig) will not work because. The man page states: The raise() function sends a signal to the calling process or thread. In a single-threaded program it is equivalent to kill(getpid(), sig); In a multithreaded program it is equivalent to pthread_kill(pthread_self(), sig); If the signal causes a handler to be called, raise() will return only after the signal handler has returned.
  • red0ct
    red0ct almost 3 years
    @FrancescoBelladonna Doesn't kill(0, some_sig) also work?
  • Janne Paalijarvi
    Janne Paalijarvi over 2 years
    Discovered today that I indeed need to use kill(getpid(), sig) instead of raise(sig) from my thread. Thanks @Kratz