Get the no. of threads spawned in a C program


 
Thread Tools Search this Thread
Top Forums Programming Get the no. of threads spawned in a C program
# 1  
Old 09-24-2008
Get the no. of threads spawned in a C program

Hello
Is there a way to get the no. of threads spawned by a program in C?
There is no function in pthread.h that does this. Smilie
# 2  
Old 09-25-2008
Hi,
This is totally OS dependent. In think WIndows allow 1024 threads.
# 3  
Old 09-25-2008
Question Unclear

Do you want to know how many threads a process is allowed to create?
If that's the case then on AIX sysconf(_SC_THREAD_THREADS_MAX) will tell you the max number of threads allowed within a process.
# 4  
Old 09-25-2008
I think the OP wants to now how many threads an application has created, not the maximiun possible.

It is generally possible to determine the number of threads from within a C program. However, how it is done is very OS-specific. If you tell us your platform and OS version, somebody here on this forum should be able to help you.
# 5  
Old 09-25-2008
Quote:
Originally Posted by shamrock
Do you want to know how many threads a process is allowed to create?
If that's the case then on AIX sysconf(_SC_THREAD_THREADS_MAX) will tell you the max number of threads allowed within a process.
Thats not what I am looking for. I have written a C program that spawns posix threads using pthread_create. I want to programmatically get the no. of threads spawned by the program at some instance of time in the program. There is no function in pthread.h that does this. So I was wondering if there is a way to do it.


Quote:
Originally Posted by fpmurphy
If you tell us your platform and OS version, somebody here on this forum should be able to help you.
I am running my program on a Linux box that runs OpenSUSE 11.
# 6  
Old 09-25-2008
In that case it only depends on how you go about creating threads within your C program especially if threads create other threads using nested function calls.
# 7  
Old 09-25-2008
If you call pthread_create in a master thread or main program it is easy. This sit eh total numbers of threads created, not currently active threads
Code:
int thread_count=0;
...................
if(pthread_create(.....) == 0) thread_count++;

If "everybody" creates a thread, then you need to create a static mutex then a static integer variable in shared memory, initialize it to zero. Everybody calling pthread_create does this:
Code:
wait on mutex
get mutex & add one to the global counter;
release the mutex

pthreads have these basic calls (plus a lot of others)
Code:
create & destroy a mutex
      pthread_mutex_init(),
      pthread_mutex_destroy()
      PTHREAD_MUTEX_INITIALIZER is a macro to create a static mutex - probably what you want.

Lock/unlock a mutex.      

      pthread_mutex_lock(),
      pthread_mutex_trylock(),
      pthread_mutex_unlock()

that lets you play with mutexes. You will have to pass the mutex address to all of your threads or they cannot use it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh behavior in scripts spawned w/nohup

I have a need to run any number of identical scripts simultaneously, so I've created a driver script which reads a template script, edits these appropriately and then submits them via nohup. The spawned scripts all check to see at some point how many of their number are running and once the count... (7 Replies)
Discussion started by: safedba
7 Replies

2. Shell Programming and Scripting

Storing received value from send, spawned telnet session

Hi Guys, I'm completely new to bash and trying to write a script to spawn a telnet session to retrieve the RSSI value of my device and log the average value of the RSSI over 20 samples. I know that my command does return the RSSI value successfully but my bash scripting is letting me down. I'm... (5 Replies)
Discussion started by: bwkm
5 Replies

3. Shell Programming and Scripting

Process behavior different when spawned from single terminal

So this one just plain confuses me. I have a bunch of somewhat CPU intensive processes that all communicate using a shared memory region. Some of these programs are threaded and some also change the scheduling to FIFO or round robin. The good news is that everything works as long as I spawn... (3 Replies)
Discussion started by: talkingfennel
3 Replies

4. Shell Programming and Scripting

Preparing a list of spawned processes

Hi All I'm currently trying to develop a script which will find the child processes of a process ID already passed to the script. I then need the script to look for spawned processes of these child processes and so on until it can't find any more. For example At the moment, I have to... (6 Replies)
Discussion started by: huskie69
6 Replies

5. Shell Programming and Scripting

Suppressing output of a spawned telnet process

Hi, I'm trying to spawn a telnet process and trying to do some actions in the remote host using expect script. I would like to know how to suppress all the output in order the user using the script should not be able to see any actions done on the remote host. I tried using the "log_user 0"... (8 Replies)
Discussion started by: arun_maffy
8 Replies

6. UNIX for Dummies Questions & Answers

Finding apache process id that is spawned by a wget

Hi, I have a situation where I am writing a programme that runs a series of long running PHP scripts that can take anything from 20 minutes to 10 hours to execute. I have a solution half implemented where I use via php exec(wget <location to command>) and get the process id back. This... (1 Reply)
Discussion started by: mrploddy
1 Replies

7. Programming

In unix how we can test or check race condition in a c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (1 Reply)
Discussion started by: afroze
1 Replies

8. Programming

In unix how we can test or check race condition in c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (5 Replies)
Discussion started by: afroze
5 Replies

9. UNIX for Dummies Questions & Answers

In unix how we can test or check race condition in a c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (1 Reply)
Discussion started by: afroze
1 Replies

10. Linux

In unix how we can test or check race condition in c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (1 Reply)
Discussion started by: afroze
1 Replies
Login or Register to Ask a Question