Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
$ ps -e -T | grep myp | grep -v grep
  797   797 ?        00:00:00 myp
  797   798 ?        00:00:00 myp
  797   799 ?        00:00:00 myp
  797   800 ?        00:00:00 myp

This shows the process myp with PID = 797 and four threads with different SPIDs.

How can I kill a particular thread of the process without killing the whole process. I understand that it might not be possible at all in some cases when there are fatal dependencies on that particular thread. But, is it possible in any case? Is yes, how?

I tried kill 799 and the process itself was terminated. Now I am not sure this was because there were dependencies that made myp fail without the process 800 or because kill is simple not able to kill individual processes.

share|improve this question
add comment

4 Answers

up vote 13 down vote accepted

Threads are an integral part of the process and cannot be killed outside it. There is the pthread_kill function but it only applies in the context of the thread itself. From the docs at the link:

Note that pthread_kill() only causes the signal to be handled in the context of the given thread; the signal action (termination or stopping) affects the process as a whole.

share|improve this answer
    
+1. just to add here. within the process you can use pthread_kill() to send signals to individual thread. may be you can add some signal handler which do it. –  Hemant Aug 23 '10 at 13:59
    
@hemant: Suppose MS Word uses a seperate thread for spell check. Killing that thread should not bring the whole thing down, unless it is designed that way. Why cannot the process exist without that thread in situations like these? –  Lazer Aug 24 '10 at 4:27
    
Well, I suppose you could design a threading model to be independent of a parent process, but allowing outside processes to kill intra-process threads but that opens up a can of worms with respect to security, process management and system integrity that I don't think any system designer would willingly submit themselves to. Threading is hard enough without those kind of headaches. Killing a thread from the inside isn't a problem because the work is done by the parent process which is held responsible if any issues arise and can be killed--automatically or otherwise. –  gvkv Aug 24 '10 at 9:16
    
"unless it is designed that way": The main reason for using threads is exactly so that you can share resources. It's rather like knocking down half a house. –  pjc50 Mar 2 '11 at 14:55
    
@Lazer, one thread can not check spelling at the same time as another thread is updating the text because you are typing. Because of this, to have a background spell checking thread, it must do something like grab a lock to prevent the other thread from changing the text, make a copy of some of the words, release the lock, then check the copied words in the background. If you happened to kill it while it held the lock, you would hang the other thread as soon as you tried to type. Multithreaded applications are full of inter dependencies like this. –  psusi May 3 '12 at 19:20
add comment

The canonical answer to this question is: With the cooperation of the process, by whatever mechanism it provides. Without the cooperation of the process, it is impossible. That the process consists of threads is an internal detail of the process that is, by intentional design, not exposed outside of the process.

share|improve this answer
add comment

On top of @gkv's answer you can take a look at the function pthread_cancel(3), part of <pthread.h>. From the man page:

The pthread_cancel() function sends a cancellation request to the thread thread. Whether and when the target thread reacts to the cancellation request depends on two attributes that are under the control of that thread: its cancelability state and type.

share|improve this answer
add comment

You may find tgkill() useful. It is Linux specific as the man page mentions.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.