Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Sending signal from process A to a specific Process B's thread: (10 Items)
   
Sending signal from process A to a specific Process B's thread  
Hello,

I'm currently writing a portable framework for RT-related programs, which should support RTAI, VxWorks, RTEMS and of 
course QNX Neutrino.

The problem that I'm currently trying to solve and that I'm having some difficulties to find a proper solution for QNX 
is this: Process A need to signal a specific thread of Process B. I know about sigaction/kill for processes, and I know 
of pthread_kill for signaling between threads of same the same process. I've tried under Linux to use pthread_kill 
between threads of different processes but all I got was a segfault on the 'killer' side.

I've however found a solution for Linux which is the following: process B saves in a shared memory are its PID and all 
its threads do the same for their respectiveTID (obtained using gettid() - which is NOT the same number as 
pthread_self() ).
Now process A can simply read the shared memory and signal a specific thread of process B using: tgkill(pid, tid, signo)
 which is a syscall that sends signal 'signo' to a thread 'tid' that must belong to process 'pid'.

Using the same procedure i.e. saving the pthread_self() results in the shared memory and then using 
pthread_kill(pthread_id) on process A side yielded a segfault.

Anyone has any tips or even a solution?

Thanks for all the help

Enrico
Re: Sending signal from process A to a specific Process B's thread  
Hi Enrico,

I suspect that the (Neutrino specific) call you're looking for is
SignalKill(), which can take both a pid and a tid.

http://www.qnx.com/developers/docs/6.4.1/neutrino/lib_ref/s/signalkill.html

Regards,
Neil

On Fri, 2009-07-03 at 14:27 -0400, Enrico Simetti wrote:
> Hello,
> 
> I'm currently writing a portable framework for RT-related programs, which should support RTAI, VxWorks, RTEMS and of 
course QNX Neutrino.
> 
> The problem that I'm currently trying to solve and that I'm having some difficulties to find a proper solution for QNX
 is this: Process A need to signal a specific thread of Process B. I know about sigaction/kill for processes, and I know
 of pthread_kill for signaling between threads of same the same process. I've tried under Linux to use pthread_kill 
between threads of different processes but all I got was a segfault on the 'killer' side.
> 
> I've however found a solution for Linux which is the following: process B saves in a shared memory are its PID and all
 its threads do the same for their respectiveTID (obtained using gettid() - which is NOT the same number as 
pthread_self() ).
> Now process A can simply read the shared memory and signal a specific thread of process B using: tgkill(pid, tid, 
signo) which is a syscall that sends signal 'signo' to a thread 'tid' that must belong to process 'pid'.
> 
> Using the same procedure i.e. saving the pthread_self() results in the shared memory and then using 
pthread_kill(pthread_id) on process A side yielded a segfault.
> 
> Anyone has any tips or even a solution?
> 
> Thanks for all the help
> 
> Enrico
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post33092
> 
Re: Sending signal from process A to a specific Process B's thread  
Thanks Neil, yes I've seen that kernel call. To use that I'd still need a method to get the tid. 

I've seen that there exists a getpid(), I've also seen that there is a getpgid() (process group) that I honestly don't 
exactly know what it means.

By looking on google for "gettid qnx" I was able to find a piece of code like this:

#include <sys/neutrino.h>
#include "cpucfg.h"

pid_t gettid(void) 
{
	return LIBC_TLS()->__tid;
}

which seems to be what I'm looking for but I have no idea, since its not documented.

Anyone can clarify this to me?
Re: Sending signal from process A to a specific Process B's thread  
Congratulations, you just implemented pthread_self() :-)

Enrico Simetti wrote:
> Thanks Neil, yes I've seen that kernel call. To use that I'd still need a method to get the tid. 
> 
> I've seen that there exists a getpid(), I've also seen that there is a getpgid() (process group) that I honestly don't
 exactly know what it means.
> 
> By looking on google for "gettid qnx" I was able to find a piece of code like this:
> 
> #include <sys/neutrino.h>
> #include "cpucfg.h"
> 
> pid_t gettid(void) 
> {
> 	return LIBC_TLS()->__tid;
> }
> 
> which seems to be what I'm looking for but I have no idea, since its not documented.
> 
> Anyone can clarify this to me?
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post33096
> 

-- 
cburgess@qnx.com
Re: Sending signal from process A to a specific Process B's thread  
Does that means that I can use the result of the pthread_self() as a tid in the SignalKill call, and that in QNX 
pthread_t == int? And that is consistent between different processes?

Cool, saves me more troubles than the Linux version then

man page of pthread_self():
Thread IDs are only guaranteed to be unique within a process.  A thread ID maybe reused after a terminated thread has 
been joined, or a detached thread has terminated.
----

Thanks again!! :D


Re: Sending signal from process A to a specific Process B's thread  
SignalKill() takes a nd, pid, tid combo which uniquely identifies a thread in a process on a node.  But the caveat from 
the linux manpage stands - a tid may be reused, and tids are
process local identifiers - ie you normally have a tid 1 in every process.

Enrico Simetti wrote:
> Does that means that I can use the result of the pthread_self() as a tid in the SignalKill call, and that in QNX 
pthread_t == int? And that is consistent between different processes?
> 
> Cool, saves me more troubles than the Linux version then
> 
> man page of pthread_self():
> Thread IDs are only guaranteed to be unique within a process.  A thread ID maybe reused after a terminated thread has 
been joined, or a detached thread has terminated.
> ----
> 
> Thanks again!! :D
> 
> 
> 
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post33100
> 

-- 
cburgess@qnx.com
Re: Sending signal from process A to a specific Process B's thread  
The fact that tid are reused doesn't bother me as I'm going to track the life of every threads.

Now that I think about it, it doesn't bother me either that the tid aren't unique between processes. As long as the (pid
,tid) couple (plus the node in QNX) uniquely identifies the specified thread I'm all happy.

As far as I understood this holds using getpid and pthread_self respectively.

Thanks alot for the very fast answers guys. I'm sure I'll have to come back to these forums in the next few weeks with 
more portability questions :)
Re: Sending signal from process A to a specific Process B's thread  
Hi Enrico,

On Neutrino, you just use the actual POSIX tid (e.g. the result of
pthread_self()).  Just as Colin says, the code fragment you found is,
indeed, exactly how pthread_self() is actually implemented on Neutrino.

It's perhaps worth mentioning in passing that directed signals are often
not an ideal message passing mechanism, owing to their very specific
POSIX semantics.  If you have a choice, you might be better off with
some other sort of IPC.

If you are unfamiliar with POSIX signal semantics, process groups, and
so on, I can recommend Steven's Advanced Programming in the UNIX
Environment.  There are also any number of good references on the
Internet (including the Neutrino documentation and the SUSv3/POSIX
specifications themselves).

Regards,
Neil

On Fri, 2009-07-03 at 15:00 -0400, Enrico Simetti wrote:
> Thanks Neil, yes I've seen that kernel call. To use that I'd still need a method to get the tid. 
> 
> I've seen that there exists a getpid(), I've also seen that there is a getpgid() (process group) that I honestly don't
 exactly know what it means.
> 
> By looking on google for "gettid qnx" I was able to find a piece of code like this:
> 
> #include <sys/neutrino.h>
> #include "cpucfg.h"
> 
> pid_t gettid(void) 
> {
> 	return LIBC_TLS()->__tid;
> }
> 
> which seems to be what I'm looking for but I have no idea, since its not documented.
> 
> Anyone can clarify this to me?
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post33096
> 
Re: Sending signal from process A to a specific Process B's thread  
I admit that I never messed that much with signals. We're mostly going to use them to implement our shutdown procedure 
in order to nicely quit any pending tasks (especially those blocked on some resource), and leaving some free signals for
 the final user. We already intend to offer other major forms of IPC such as shared memory/queues/semaphores/rpc.

However the major headache comes from the fact that we're constantly looking at four different RTOS APIs and things are 
hardly always the same. I'd never be able to use a getpid and pthread_self results together in unix for example.

Even if there is actually TONS of documentation for QNX (its like being in heaven compared to RTAI) though I'm glad that
 this forum exists because sometime you could just read references for hours and never come up with a definite answer.

Cheers!
Re: Sending signal from process A to a specific Process B's thread  
Hi Enrico,

Yes, portability can be a real headache.  For your situation, you might
want to consider dedicating a thread in each process for shutdown
handling.  If necessary, it could be the only thread in the process
with, for example, SIGTERM unblocked (or use sigwait() or whatever).
Then you don't need directed signals and you're back into POSIX-land.
Shutdown within the process could be accomplished using any number of
mechanisms, possibly including thread cancellation (but preferably
something more structured).   However, the dedicated shutdown thread
could just as easily be reading a pipe, or socket, or whatever instead
-- which might be even more portable.  (There may, of course, be latency
or other design considerations which make my suggestions a bad idea for
your particular application.)

Regards,
Neil

On Fri, 2009-07-03 at 15:40 -0400, Enrico Simetti wrote:
> I admit that I never messed that much with signals. We're mostly going to use them to implement our shutdown procedure
 in order to nicely quit any pending tasks (especially those blocked on some resource), and leaving some free signals 
for the final user. We already intend to offer other major forms of IPC such as shared memory/queues/semaphores/rpc.
> 
> However the major headache comes from the fact that we're constantly looking at four different RTOS APIs and things 
are hardly always the same. I'd never be able to use a getpid and pthread_self results together in unix for example.
> 
> Even if there is actually TONS of documentation for QNX (its like being in heaven compared to RTAI) though I'm glad 
that this forum exists because sometime you could just read references for hours and never come up with a definite 
answer.
> 
> Cheers!
> 
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post33106
>