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)?
The kill command will kill a single process at a time with the given process ID. It will send a SIGTERM signal indicating to a process to stop. It waits for the program to run its shutdown routine. The -signal command can be used to specify a signal that isn't SIGTERM.
A process can use kill() to send a signal to itself. If the signal is not blocked or ignored, at least one pending unblocked signal is delivered to the sender before kill() returns. If there are no other pending unblocked signals, the delivered signal is sig.
Whenever a user kills a process, it actually sends a signal to the target process, considered a kill signal or termination message. However, there are several types of termination messages and some of them are as follows: SIGKILL - It is generally considered as the ultimate way of killing a process.
Yes, you can. There's even a specific function for it — raise(sig)
— although kill(getpid(), sig)
will work too.
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.
Try exit
- a lot simpler - why make things complicated?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With