Is there a way to kill or suspend a thread/task in a multithreaded process ?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there a way to kill or suspend a thread/task in a multithreaded process ?
Is there a way to kill or suspend a thread in a multithreaded process ? for(eg) in java which is a multithreaded process , if i do a kill -STOP <tid> it stops the entire process neither the kill -9 <tid> kills the entire process instead of a particular thread . Is there any trick to just suspend or kill the thread ?
You can't kill or stop just one thread from another process.
You can send a signal to a particular thread, but the stop/abort action that is taken by the signal affects the whole process.
In the earlier implementation of Linux threads, it was possible to stop a single thread with SIGSTOP, but this behaviour has now been fixed to conform to the Posix standard (so it stops all threads in the process).
You can use pthread_cancel to stop a thread. But this is done from within the same process space.
You can't kill or stop just one thread from another process.
You can send a signal to a particular thread, but the stop/abort action that is taken by the signal affects the whole process.
In the earlier implementation of Linux threads, it was possible to stop a single thread with SIGSTOP, but this behaviour has now been fixed to conform to the Posix standard (so it stops all threads in the process).
You can use pthread_cancel to stop a thread. But this is done from within the same process space.
How about using any debugger like gdb or dbx , is it possible to stop after attaching to the process, maybe probably calling the pthread_cancel() method or something. or set a breakpoint on most commonly used methods by a process to break the thread and stop , any thoughts ?
Why and under what circumstances are you trying to do this?
If a thread goes into somekind kind of loop causing the CPU to spike , I would like to suspend or kill the thread instead of killing the entire process.
Well, theoretically, if you have a the main ctrl thread to respond to (ie catch) say
kill USR1
which is an unassigned signal for use by programmers, you could get the ctrl thr to read a cfg file where you've set the thread id to be stopped (pthread_cancel), but I can't see how you'd id that thread. Possibly you can tell externally eg separate logs for each thread?
IOW, first you need some way for you or the ctrl thr itself to notice you've got a runaway and second you need to be able to id which thread it is.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.