Subj : Re: process vs. thread ids on linux To : comp.programming.threads From : Loic Domaigne Date : Fri Jun 03 2005 12:38 am Salut, > However, a ps -am gives me this: > > 19103 pts/2 00:00:00 ttest > 19104 pts/2 00:00:00 ttest > ... > 19113 pts/2 00:00:00 ttest As pointed out by some other posters, the "PID column" is in fact the KSE id. David (Schwartz) is right: this is an issue of 'ps'. Newer 'ps' get this right. On my Fedora Core 3: [loic@neumann ~]$ ps -am PID TTY TIME CMD 5455 pts/2 00:00:00 ttest - - 00:00:00 - - - 00:00:00 - - - 00:00:00 - - - 00:00:00 - - - 00:00:00 - 5480 pts/3 00:00:00 ps - - 00:00:00 - Do you note the '-' ? We can even go a step further and print the KSE id using the '-L' flag: [loic@neumann ~]$ ps -aL PID LWP TTY TIME CMD 4463 4463 pts/2 00:00:05 xemacs 5455 5455 pts/2 00:00:00 ttest 5455 5456 pts/2 00:00:00 ttest 5455 5457 pts/2 00:00:00 ttest 5455 5458 pts/2 00:00:00 ttest 5455 5459 pts/2 00:00:00 ttest 5483 5483 pts/3 00:00:00 ps The column "PID" is the same for all KSEs. The Column "LWP" corresponds to the KSE id (with your version of 'ps', that column is wrongly called "PID"). > So, what is going on here? If the kernel is indeed assigning a new > pid to each thread, how can I get to that id (since getpid() doesn't > work). Why doesn't getpid() work, anyway? Oh, getpid() does work on NPTL. As pointed out by David (Butenhof), getpid() returns in fact the "thread group id", which happens to be the KSE id of the main thread. As a result getpid() has the same value in each thread. That conforms to POSIX. In order to get the KSE id, you must use the Linux specific syscall gettid(). > And can I then use these pids to send signals directly to a > particular thread (which I was under the impression was not possible)? Actually, yes you can. If you know the KSE id, you can directly send a signal to a particular thread. ( And as a matter of fact, that's exactly the mechanism used by NPTL to send a signal to a specific thread with pthread_kill() : it sends the signal to the corresponding KSE id. ) Now if you have understood the whole picture, you should be able to send a signal to a particular thread from the shell... Needless to say, but all those "features" are highly Linux-specific! But I shouldn't have tell you all of this ;-) Cheers, Loic. .