[ale] differentiating processes from threads

hirsch at zapmedia.com hirsch at zapmedia.com
Tue Apr 2 11:28:08 EST 2002


Danny Cox writes:
 > John,
 > 
 > On Tue, 2002-04-02 at 04:32, John Wells wrote:
 > > Hmmm...
 > > 
 > > Ok, so to test this we should be able to:
 > > 
 > > perl -e 'fork();<STDIN>;'
 > > 
 > > Shouldn't this do a true fork?  If so, then look at the output of:
 > > 
 > > ps -eo pid,pgid,flags,user,args | grep perl
 > > 
 > > 30213 30213 000 jbw      perl -e fork();<STDIN>;
 > > 30214 30213 040 jbw      perl -e fork();<STDIN>;
 > > 
 > > The second process still has the FORKNOEXEC flags set?!?
 > 
 > 	Yes, that's correct.  The process has forked, but it's not execed
 > anything.  The '040' flag simply means that no exec(2) system call has
 > been performed, but a fork(2) has.  The above test shows nothing about
 > thread possibilities.  It was just something I noticed with mozilla, and
 > will be true of any threads.  While the vast majority of forks will exec
 > soon thereafter, a thread necessarily will not.
 > 
 > 	Unfortunatly, this isn't really a property of threads, but a
 > consequence.  Other processes, like the one you discovered, can also
 > have the same flag set.  I see no direct way of testing a PID for
 > 'threadness' ('thread-nature' Hmm.  Zen anyone?).  The necessary flags
 > just aren't exposed in /proc/<pid> to tell.  This is most likely why ps
 > can't tell that threads N,M,O are sharing the same memory, and must show
 > it twice.
 > 
 > 	However, this does present a possibility: hack the kernel where it
 > displays the strings for "/proc/<pid>/status".  It would just be one
 > more line, I think.  Of course, you'd have to find which member(s) of
 > the task struct represented 'threadness', but then it's dead easy.

I don't think there are any such members, per se, but it should be
deducible.  The kernel could tell which processes are sibling threads
(i.e. threads of the same process) by comparing their memory segments.
If the only difference between them is the stack and instruction
pointers then they are threads.  If the heap is also different then
they are separate processes.

--Michael






---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list