Can I kill a process from itself?
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.
Related videos on Youtube
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, 2020Comments
-
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 over 12 yearsWouldn't it be easier to just
exit()
? -
Brandon Buck over 12 yearsCouldn'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 over 12 yearsAn alternative approach to this could involve using
on_exit()
-
-
Francesco Belladonna over 12 yearsAs 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 over 12 yearsYou can give an exit code. Just use the signal handling to tidy up then exit with a status code.
-
Francesco Belladonna over 12 yearsIt's not a design, it's a workaround for fixing a design change
-
tdenniston over 12 yearsWell, it is a design choice. If you have to create a workaround like this, you should re-examine your original design choices.
-
Francesco Belladonna over 12 yearsIt 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 over 12 yearsI would like to point out that kill(getpid(), SIGINT) DOES NOT have the same effect as exit().
-
Neowizard over 11 yearsraising 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 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 over 10 yearsone 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 over 10 yearsI noticed this new answer only today but I think it's better.
-
Erich Kitzmueller almost 7 yearsThere is at least one case when using
exit()
is not desirable: after afork
where the child tries toexec
another program, but fails (for whatever reason). Callingexit
in the child causes routines installed byon_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 about 3 yearsJust 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 tokill(getpid(), sig);
In a multithreaded program it is equivalent topthread_kill(pthread_self(), sig);
If the signal causes a handler to be called, raise() will return only after the signal handler has returned. -
red0ct almost 3 years@FrancescoBelladonna Doesn't
kill(0, some_sig)
also work? -
Janne Paalijarvi over 2 yearsDiscovered today that I indeed need to use kill(getpid(), sig) instead of raise(sig) from my thread. Thanks @Kratz