2

I am using int s=pthread_kill(thread_arr[t], 9); to send the SIGKILL signal to the thread that is in the t place of thread_arr, but instead of killing this exact thread my whole program is being killed. Can anyone tell me if I am doing something wrong?

Michaila
  • 31
  • 1
  • 5

1 Answers1

2

I think the answer is in the man page for pthread_kill:

Signal dispositions are process-wide: if a signal handler is installed, the handler will be invoked in the thread, but if the disposition of the signal is "stop", "continue", or "terminate", this action will affect the whole process.

The disposition of SIGKILL is to terminate the process, and the signal can't be caught, blocked or ignored.

I would suggest using a different signal, and having the thread receiving the signal stop itself, instead of using

rahul.deshmukhpatil
  • 977
  • 1
  • 16
  • 31
Richard St-Cyr
  • 970
  • 1
  • 8
  • 14
  • I also used other signals after your answer, but most of them kill the whole process, too. Can you please give me an example of what you are suggesting? I am new with threads and I am having a hard time understanding them. – Michaila Dec 14 '15 at 12:16
  • An example is probably too long to explain here. You should look at tutorials or documentation on signal handling. There is a short one [here](http://www.gnu.org/software/libc/manual/html_node/Basic-Signal-Handling.html). – Richard St-Cyr Dec 14 '15 at 12:40
  • to which thread (is it main parent thread) you are sending signal? what is your method of sending singal? programmatically or using control+C on terminal? – rahul.deshmukhpatil Dec 15 '15 at 06:03
  • Where to send the signal depends on what you want to achieve and your context. If, for example, you want to send a signal to your application to for it to reconfigure, this signal can be directed to the application. If you want a specific thread to stop, you should send the signal to the thread, unless the application has a way of identifying the thread. In these cases, I usually use the [kill](http://linuxcommand.org/lc3_man_pages/kill1.html) command. – Richard St-Cyr Dec 15 '15 at 12:53
  • Finally I opened a new process with posix_spawn running in a thread and when it was necessary I killed it with the process id using `int kill(pid,9)`. Thank you all for your answers. – Michaila Dec 15 '15 at 22:02