Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I kill a process from itself?

Tags:

c

unix

process

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)?

like image 593
Francesco Belladonna Avatar asked Oct 21 '11 14:10

Francesco Belladonna


People also ask

Does kill actually kill the process?

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.

Can a process signal itself?

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.

What happens when I kill a process?

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.


3 Answers

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

like image 192
Ilmari Karonen Avatar answered Oct 14 '22 08:10

Ilmari Karonen


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.

like image 39
Daniel Avatar answered Oct 14 '22 08:10

Daniel


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

like image 42
Ed Heal Avatar answered Oct 14 '22 09:10

Ed Heal