-1

I know similar questions have been asked, but I think my situation is little bit different. I need to check if child thread is alive, and if it's not print error message. Child thread is supposed to run all the time. So basically I just need non-block pthread_join and in my case there are no race conditions. Child thread can be killed so I can't set some kind of shared variable from child thread when it completes because it will not be set in this case.

Killing of child thread can be done like this:

kill -9 child_pid

EDIT: alright, this example is wrong but still I'm sure there exists way to kill a specific thread in some way.

EDIT: my motivation for this is to implement another layer of security in my application which requires this check. Even though this check can be bypassed but that is another story.

EDIT: lets say my application is intended as a demo for reverse engineering students. And their task is to hack my application. But I placed some anti-hacking/anti-debugging obstacles in child thread. And I wanted to be sure that this child thread is kept alive. As mentioned in some comments - it's probably not that easy to kill child without messing parent so maybe this check is not necessary. Security checks are present in main thread also but this time I needed to add them in another thread to make main thread responsive.

jozols
  • 560
  • 7
  • 22
  • I would consider a different design. You could have threads which end by e.g. changing (under some mutex) a global flag. – Basile Starynkevitch Oct 31 '17 at 10:34
  • But I don't need to end any threads. I just want to be sure that my slave thread is still alive and do some error handling (without using slave thread) after it dies. – jozols Oct 31 '17 at 10:36
  • Explain how the thread would be killed. Show some [MCVE] in your question. You need to improve your question a lot, so please **edit your question** – Basile Starynkevitch Oct 31 '17 at 10:36
  • Child thread, or child process? – Andrew Henle Oct 31 '17 at 10:39
  • 2
    A thread cannot be killed with `kill -9`. You must be thinking about processes. – n. m. could be an AI Oct 31 '17 at 10:41
  • "still I'm sure there exists way to kill a specific thread in some way." You would be wrong then. – n. m. could be an AI Oct 31 '17 at 10:43
  • @AndrewHenle Sorry about confusion - it's really child thread (not process) created using pthread_create. – jozols Oct 31 '17 at 10:43
  • You need to **edit your question** by giving some [MCVE] and explaining your motivation and use case in several additional paragraphs. You look confused. – Basile Starynkevitch Oct 31 '17 at 10:52
  • Your question looks as some [XY problem](http://xyproblem.info) and without motivation and context smells really bad. You need to improve your design. – Basile Starynkevitch Oct 31 '17 at 11:01
  • The recent edit is even more confusing. Why security is relevant? You are decreasing it! And what kind of application are you coding? – Basile Starynkevitch Oct 31 '17 at 11:06
  • Maybe I should start a doctoral dissertation about this problem and post you a link here? Question is simple (how to check if thread is alive?), solution might not be that simple. – jozols Oct 31 '17 at 11:20
  • Question show that you are confused. Thread programming is difficult, we cannot change that. You need to read more stuff. – Basile Starynkevitch Oct 31 '17 at 11:22
  • I don't understand your real issue (because you did not motivate your question as you should). I guess that you'll need to redesign part of your application. Yes, design bugs are very costly in development time. You might need to spend months of work in refactoring (but that won't give you a PhD). – Basile Starynkevitch Oct 31 '17 at 11:30
  • Possible duplicate of [How do you query a pthread to see if it is still running?](https://stackoverflow.com/q/2156353/608639), [How do I determine if a pthread is alive?](https://stackoverflow.com/q/1693180/608639), [pthread thread state](https://stackoverflow.com/q/1365711/608639), etc. – jww Oct 10 '18 at 07:08

3 Answers3

4

killed by what and why that thing can't indicate the thread is dead? but even then this sounds fishy

it's almost universally a design error if you need to check if a thread/process is alive - the logic in the code should implicitly handle this.

In your edit it seems you want to do something about a possibility of a thread getting killed by something completely external.

Well, good news. There is no way to do that without bringing the whole process down. All ways of non-voluntary death of a thread kill all threads in the process, apart from cancellation but that can only be triggered by something else in the same process.

  • 1
    This is more a long comment than an answer. – Basile Starynkevitch Oct 31 '17 at 10:35
  • I believe this is not design problem as I'm implementing additional security check. – jozols Oct 31 '17 at 10:38
  • @jozols *I believe this is not design problem as I'm implementing additional security check.* What security do you think such checking could add? Anyone with permissions to kill the child thread or process can manipulate your environment to return a false result. – Andrew Henle Oct 31 '17 at 10:43
  • Yes, but Im not building bullet proof solution as I know it can't be done. Just adding another layer. – jozols Oct 31 '17 at 10:46
  • @employee of the month - if it's possible please add source to your last sentence. – jozols Oct 31 '17 at 10:52
2

The kill(1) command does not send signals to some thread, but to a entire process. Read carefully signal(7) and pthreads(7).

Signals and threads don't mix well together. As a rule of thumb, you don't want to use both.

BTW, using kill -KILL or kill -9 is a mistake. The receiving process don't have the opportunity to handle the SIGKILL signal. You should use SIGTERM ...

If you want to handle SIGTERM in a multi-threaded application, read signal-safety(7) and consider setting some pipe(7) to self (and use poll(2) in some event loop) which the signal handler would write(2). That well-known trick is well explained in Qt documentation. You could also consider the signalfd(2) Linux specific syscall.

If you think of using pthread_kill(3), you probably should not in your case (however, using it with a 0 signal is a valid but crude way to check that the thread exists). Read some Pthread tutorial. Don't forget to pthread_join(3) or pthread_detach(3).

Child thread is supposed to run all the time.

This is the wrong approach. You should know when and how a child thread terminates because you are coding the function passed to pthread_create(3) and you should handle all error cases there and add relevant cleanup code (and perhaps synchronization). So the child thread should run as long as you want it to run and should do appropriate cleanup actions when ending.

Consider also some other inter-process communication mechanism (like socket(7), fifo(7) ...); they are generally more suitable than signals, notably for multi-threaded applications. For example you might design your application as some specialized web or HTTP server (using libonion or some other HTTP server library). You'll then use your web browser, or some HTTP client command (like curl) or HTTP client library like libcurl to drive your multi-threaded application. Or add some RPC ability into your application, perhaps using JSONRPC.

(your putative usage of signals smells very bad and is likely to be some XY problem; consider strongly using something better)

my motivation for this is to implement another layer of security in my application

I don't understand that at all. How can signal and threads add security? I'm guessing you are decreasing the security of your software.

I wanted to be sure that this child thread is kept alive.

You can't be sure, other than by coding well and avoiding bugs (but be aware of Rice's theorem and the Halting Problem: there cannot be any reliable and sound static source code program analysis to check that). If something else (e.g. some other thread, or even bad code in your own one) is e.g. arbitrarily modifying the call stack of your thread, you've got undefined behavior and you can just be very scared.

In practice tools like the gdb debugger, address and thread sanitizers, other compiler instrumentation options, valgrind, can help to find most such bugs, but there is No Silver Bullet.

Maybe you want to take advantage of process isolation, but then you should give up your multi-threading approach, and consider some multi-processing approach. By definition, threads share a lot of resources (notably their virtual address space) with other threads of the same process. So the security checks mentioned in your question don't make much sense. I guess that they are adding more code, but just decrease security (since you'll have more bugs).

Reading a textbook like Operating Systems: Three Easy Pieces should be worthwhile.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Yes, you are right. But the point is thread can be kill in some way, even brutally. – jozols Oct 31 '17 at 10:42
  • 1
    If a thread is brutally killed, your program is likely to exhibit some [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) in such case. – Basile Starynkevitch Oct 31 '17 at 10:47
  • Thanks, I will try to find evidence that thread can't be killed without avoiding parent to behave abnormally (even if parent is not interacting with child thread). – jozols Oct 31 '17 at 10:49
  • "How can signal and threads add security?" It's simple. Thread is performing some security checks in background from time to time. I need to be sure that this thread is still running, if not then there's a breach. – jozols Oct 31 '17 at 11:11
  • @jozols: please don't comment here. Take time to **edit your question**, it needs to be significantly improved to make some sense. Currently, it is confusing. There is an `edit` hyperlink near your question, please use it a lot more. And "security checks from time to time" is a profound and dangerous illusion. You need to tell a lot more about your application and project! – Basile Starynkevitch Oct 31 '17 at 11:12
0

You can use pthread_kill() to check if a thread exists.

SYNOPSIS

#include <signal.h>

int pthread_kill(pthread_t thread, int sig);

DESCRIPTION

The pthread_kill() function shall request that a signal be delivered to the specified thread.

As in kill(), if sig is zero, error checking shall be performed but no signal shall actually be sent.

Something like

int rc = pthread_kill( thread_id, 0 );
if ( rc != 0 )
{
    // thread no longer exists...
}

It's not very useful, though, as stated by others elsewhere, and it's really weak as any type of security measure. Anything with permissions to kill a thread will be able to stop it from running without killing it, or make it run arbitrary code so that it doesn't do what you want.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • I've read about pthread_kill and it causes undefined behaviour if thread is already dead. – jozols Oct 31 '17 at 10:51
  • @jozols What undefined behavior? There are issues with "zombie" threads (no longer running but not joined) that can be addressed by using detached threads, but if you're tracking your thread IDs and have a unique one for your process, I don't see anything in the POSIX standard relating to undefined behavior. – Andrew Henle Oct 31 '17 at 10:55
  • 1
    @jozols POSIX states *If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an [ESRCH] error.* I haven't looked through the entire POSIX standard, but there's no mention of undefined behavior in the `pthread_kill()` documentation at all, and it specifically calls out the recommended result for a no-longer-existing thread. – Andrew Henle Oct 31 '17 at 11:05