[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming.threads

How to get number of own threads programatically on Linux?

rs

8/4/2004 6:28:00 AM

I have a multithreaded linux application. I want this application
to display, how much threads this application has created. How can
I retrieve this value (I tried with /proc/<pid> but couldn't find
the number of threads)?
Thanks in advance,

Roland Scholz
11 Answers

Ronald Landheer-Cieslak

8/4/2004 4:08:00 PM

0

Roland Scholz wrote:
> I have a multithreaded linux application. I want this application
> to display, how much threads this application has created. How can
> I retrieve this value (I tried with /proc/<pid> but couldn't find
> the number of threads)?
> Thanks in advance,
Let me get this straight: you want the application *itself* to display
how many threads it has created, presumably with pthread_create(3)?

Hint:
#define pthread_create rpl_pthread_create

Keep a (global) counter in rpl_pthread_create and have
rpl_pthread_create call the real thing.

Hint 2: don't forget
#undef pthread_create

HTH

rlc

Randy

8/4/2004 4:32:00 PM

0

Roland Scholz wrote:
> I have a multithreaded linux application. I want this application
> to display, how much threads this application has created. How can
> I retrieve this value (I tried with /proc/<pid> but couldn't find
> the number of threads)?

Impossible to know how many threads were spawned unless you monitor the
invocation of pthread_create(). (Some threads may already have been reaped, and
so, would be absent from /proc.)

But if you want to know how many child threads are running, you can parse the
output from 'ps -ef' to look for all threads whose PPID equals your process's
PID. This is also stored in /proc/<PID>/status under 'PPid'.

Of course, you'll also want to look for all other processes whose PPIDs equal
the child processes, in case a child process has spawned threads of its own.

Randy

--
Randy Crawford http://www.ruf.rice... rand AT rice DOT edu

ptjm

8/5/2004 4:49:00 AM

0

In article <cer32m$7ea$1@joe.rice.edu>, Randy <joe@burgershack.com> wrote:

% But if you want to know how many child threads are running, you can parse the
% output from 'ps -ef' to look for all threads whose PPID equals your process's
% PID. This is also stored in /proc/<PID>/status under 'PPid'.

Keeping in mind that this is totally non-portable. It won't work with anything
but Linux, and it won't work with some present or most future versions of
Linux.

--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com

rs

8/5/2004 8:43:00 AM

0

Thanks for all answers so far!

Randy <joe@burgershack.com> wrote in message news:<cer32m$7ea$1@joe.rice.edu>...

> But if you want to know how many child threads are running,
This is actually what I want to know. Sorry if I didn't make that
clear with my original message.

> you can parse the
> output from 'ps -ef' to look for all threads whose PPID equals your process's
> PID. This is also stored in /proc/<PID>/status under 'PPid'.
>
> Of course, you'll also want to look for all other processes whose PPIDs equal
> the child processes, in case a child process has spawned threads of its own.

Is there no API for such things, no function I can call?
Parsing all status files in /proc/<PID> don't sound like an easy
solution to me.

Ronald Landheer-Cieslak

8/5/2004 2:28:00 PM

0

Roland Scholz wrote:

> Thanks for all answers so far!
>
> Randy <joe@burgershack.com> wrote in message news:<cer32m$7ea$1@joe.rice.edu>...
>
>
>>But if you want to know how many child threads are running,
>
> This is actually what I want to know. Sorry if I didn't make that
> clear with my original message.
>
>
>>you can parse the
>>output from 'ps -ef' to look for all threads whose PPID equals your process's
>>PID. This is also stored in /proc/<PID>/status under 'PPid'.
>>
>>Of course, you'll also want to look for all other processes whose PPIDs equal
>>the child processes, in case a child process has spawned threads of its own.
> Is there no API for such things, no function I can call?
> Parsing all status files in /proc/<PID> don't sound like an easy
> solution to me.
Really, if you want an even mildly portable solution to your problem,
wrap the pthreads interface and count the creations and deaths of
threads. Parsing /proc/<PID> files nails you not only to a specific
kernel (Linux) but also to a specific range of versions of that kernel
(i.e. the ones of which you know how to parse the files).

AFAIK, there is no API for such things.

rlc

amar

8/5/2004 2:30:00 PM

0

on the 2.6 kernel (i use suse 9.1), you could look at /proc/<pid>/status file
and that gives you the no. of threads currently running "Threads: <n>" I am not
sure how it works for 2.4/older ...

Amar.

rs@RolandScholz.de (Roland Scholz) wrote in message news:<511b2f03.0408050042.f62d26c@posting.google.com>...
> Thanks for all answers so far!
>
> Randy <joe@burgershack.com> wrote in message news:<cer32m$7ea$1@joe.rice.edu>...
>
> > But if you want to know how many child threads are running,
> This is actually what I want to know. Sorry if I didn't make that
> clear with my original message.
>
> > you can parse the
> > output from 'ps -ef' to look for all threads whose PPID equals your process's
> > PID. This is also stored in /proc/<PID>/status under 'PPid'.
> >
> > Of course, you'll also want to look for all other processes whose PPIDs equal
> > the child processes, in case a child process has spawned threads of its own.
>
> Is there no API for such things, no function I can call?
> Parsing all status files in /proc/<PID> don't sound like an easy
> solution to me.

Eric Sosman

8/5/2004 3:20:00 PM

0

Roland Scholz wrote:
> Thanks for all answers so far!
>
> Randy <joe@burgershack.com> wrote in message news:<cer32m$7ea$1@joe.rice.edu>...
>
>
>>But if you want to know how many child threads are running,
>
> This is actually what I want to know. Sorry if I didn't make that
> clear with my original message.
>
>
>>you can parse the
>>output from 'ps -ef' to look for all threads whose PPID equals your process's
>>PID. This is also stored in /proc/<PID>/status under 'PPid'.
>>
>>Of course, you'll also want to look for all other processes whose PPIDs equal
>>the child processes, in case a child process has spawned threads of its own.
>
>
> Is there no API for such things, no function I can call?
> Parsing all status files in /proc/<PID> don't sound like an easy
> solution to me.

As far as I know there's no API to answer this question.
That should be something of a hint, really: If the people
who designed the threading APIs had thought the question
had a useful answer, they'd presumably have invented an
API to answer it. The experts didn't invent such an API,
so it's likely they didn't see the question as useful.

So: Why do you want to know the answer to this question
that didn't appear to interest the experts? What do you
intend to do with the answer once you have it? What is
the larger problem you're trying to solve? There may be a
better solution than learning that "Seven threads are
running -- or, rather, *were* running a microsecond ago."

--
Eric.Sosman@sun.com

rs

8/6/2004 7:08:00 AM

0

Eric Sosman <Eric.Sosman@sun.com> wrote in message news:<41125005.2070201@sun.com>...
>
> As far as I know there's no API to answer this question.
> That should be something of a hint, really: If the people
> who designed the threading APIs had thought the question
> had a useful answer, they'd presumably have invented an
> API to answer it. The experts didn't invent such an API,
> so it's likely they didn't see the question as useful.

If one person answer that no API exist, there is no reason to
believe he is right. In this thread, many people said so, so
I believe.


>
> So: Why do you want to know the answer to this question
> that didn't appear to interest the experts?

Even experts might not be thinking of everything, which is
useful.

> What do you
> intend to do with the answer once you have it? What is
> the larger problem you're trying to solve? There may be a
> better solution than learning that "Seven threads are
> running -- or, rather, *were* running a microsecond ago."

My application should have a system status page, where yuo get
some information about the system, i.e. free memory, free
disc space, average load, number of currently running processes
and number of own threads. If there is a problem with the
application, customer can look to this page. If you have enough
memory (both on disc and RAM), not much running processes,
but a high load and a large number of threads for this application
(for example one thousand or so, it is a heavily multithreaded
application), there might be a problem with the application.
This application is running on Solaris, Linux and Windows and
there is no problem to get the number of own threads on
Solaris and Windows. So I am surprised it is a problem on
Linux. The class which retrieves the information should be
usefull for other applications as well, so I don't want to
wrap the thread class to count the threads just to get this
information for Linux. The "solution" might be simply
not to show the number of threads on Linux.

Markus.Elfring

8/6/2004 9:05:00 AM

0

> AFAIK, there is no API for such things.

I suggest to look at the discussion "Introspection functions", too.
http://groups.google.de/groups?threadm=3ffc3410%40usenet01....

Randy Howard

8/6/2004 10:54:00 PM

0

In article <511b2f03.0408050042.f62d26c@posting.google.com>, rs@RolandScholz.de
says...
> Thanks for all answers so far!
>
> Randy <joe@burgershack.com> wrote in message news:<cer32m$7ea$1@joe.rice.edu>...
>
> > But if you want to know how many child threads are running,
> This is actually what I want to know. Sorry if I didn't make that
> clear with my original message.
>
> > you can parse the
> > output from 'ps -ef' to look for all threads whose PPID equals your process's
> > PID. This is also stored in /proc/<PID>/status under 'PPid'.
> >
> > Of course, you'll also want to look for all other processes whose PPIDs equal
> > the child processes, in case a child process has spawned threads of its own.
>
> Is there no API for such things, no function I can call?
> Parsing all status files in /proc/<PID> don't sound like an easy
> solution to me.

Perhaps I'm missing something, but why not just track it yourself? Every time
your start a thread, bump a counter "n_threads_running" or something (behind a
mutex or something of course) and every time a thread calls pthread_exit(),
reverse it. Seems too simple, what are you really tring to do?


--
Randy Howard
To reply, remove FOOBAR.