Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is kill function synchronous?

Tags:

Is the kill function in Linux synchronous? Say, I programatically call the kill function to terminate a process, will it return only when the intended process is terminated, or it just sends the signal and return. If that is the case, how can I make it wait for the intended process to be killed?

like image 581
MetallicPriest Avatar asked Jan 06 '12 17:01

MetallicPriest


2 Answers

No, since it doesn't kill anything, it only sends a signal to the process.

By default this signal can even be blocked or ignored.

You can't block kill -9 which represents sending SIGKILL

To wait for the process to die:

while kill -0 PID_OF_THE_PROCESS 2>/dev/null; do sleep 1; done 
like image 130
Šimon Tóth Avatar answered Sep 20 '22 16:09

Šimon Tóth


kill cannot be synchronous as it only sends a signal. The target process might ignore incoming signals (cf. SIG_IGN) so there's no guarantee regarding kill's effect.

It shouldn't be difficult to make an experiment verifying this hypothesis. Start process A and make it handle SIGTERM with a 10 second sleep before dying. Then start process B, which delivers a SIGTERM to A and exits immediately.

like image 35
Jan Avatar answered Sep 20 '22 16:09

Jan