LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2009, 09:11 PM   #1
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
how to find number of threads in the process in C Language?


hello all,

I need to find how many threads are alive with respect to the current process for my further processing. Is there any means to trace this number ?

http://h21007.www2.hp.com/portal/sit...10275d6e10RCRD

I referred the above link. But sys/pstat.h is not in my system. Don't know which library gives this header.

Last edited by chakka.lokesh; 12-23-2009 at 09:14 PM.
 
Old 12-24-2009, 05:45 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
The link in your post refers not to Linux, but to HP-UX, which is the Hewlett-Packard variant of Unix. You can't use that solution in Linux.

The best solution, in my opinion, is to put a wrapper function around pthread_create(). Write this wrapper function so that, in addition to calling pthread_create(), it bumps a thread counter. Of course, your main program must initialize that counter to zero at the beginning. Of course, you need to protect that counter with a mutex.

Make sure that you also create appropriate wrapper functions for decrementing that count.

Once you've done this, you can refer to that count any time you want (with mutex protection, of course). Furthermore, you can place in your pthread_create() wrapper function some additional code to keep an eye on the thread count and report every time that count reaches a multiple of 100, say, or do something appropriate if that count gets too high.

There may be a simpler way to find the thread count, but I'm not aware of it.

Hope this helps.
 
Old 12-24-2009, 06:11 AM   #3
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Use ps with 'H' option
 
Old 12-25-2009, 09:01 PM   #4
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by jf.argentino View Post
Use ps with 'H' option
H option gives the hierarchy. More over I want solution in C language.

In which way your post is going to help me?
 
Old 12-25-2009, 09:17 PM   #5
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by wje_lq View Post
The best solution, in my opinion, is to put a wrapper function around pthread_create().
Dear wje_lq, thanks for your response.

Yes. This is a feasible solution. I can adopt this. If I adopt this, I will be in need of another information. For a given thread id, I should be able to decide whether that thread is still existing or terminated.

This situation in my requirement is like this. For the sake of optimizing the time consumption, I will create a new thread for sub functions that can be executed in parallel. After completing the execution of that sub function, that thread has to die or I should assign another sub function to that. At the same time, I must ensure that there are only a limited (maximum) number of threads existing. The process should not contain number of threads more than this threshold value.
 
Old 12-25-2009, 11:52 PM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Two questions.

First, how do you pass information between your main thread and subthreads (in each direction)?

Second, because this could drive the solution that you build: how many subthreads max? 2? 200? 20,000?
 
Old 12-27-2009, 04:53 AM   #7
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by wje_lq View Post
First, how do you pass information between your main thread and subthreads (in each direction)?
No information will be passed.


Quote:
Originally Posted by wje_lq View Post
Second, because this could drive the solution that you build: how many subthreads max? 2? 200? 20,000?
number of threads depends on the number of cores available in CPU
 
Old 12-28-2009, 03:37 AM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
All it takes, then, is a simple bookkeeping operation. You could do something like the following example.
Quote:
Originally Posted by chakka.lokesh View Post
No information will be passed.
In the proposed example, this would change.
Quote:
Originally Posted by chakka.lokesh View Post
number of threads depends on the number of cores available in CPU
With such a small number, which would remain constant during any given run of your program, this makes things very simple.

In the proposed example, the following steps would be performed once, at program initialization.
  1. Determine how many cores are available in the CPU. Since this will be done only once per run of the program, you can rely on a crude but effective hack. There are many variants. Here's one:
    Code:
    #include <stdlib.h>
    
    /* ... */
    
    if(system("grep ^processor /proc/cpuinfo | wc -l > cpucount.txt"))
    {
      /* insert error handling code here */
    }
    
    /* then read the answer from the file cpucount.txt */
  2. Define a struct like the following:
    Code:
    typedef struct
    {
      /* tt_status can have one of the following values:
       * 0: no thread has been started
       * 1: a thread has been started but not finished
       * 2: a thread has been finished
       */
    
      int       tt_status;
      pthread_t tt_thread_id;
    
    } thread_tracker_t,
     *thread_tracker_p;
  3. malloc() an array of this struct. The number of elements in the array should be equal to the number you got in step I. Let's call this array thread_status. Zero out each tt_status, or all bytes of thread_status if that's your style.
In the proposed example, the following steps would be taken each time you want to fire off a thread.
  1. Loop through thread_status. For each element where tt_status is 2, set tt_status to 0 and call pthread_join; its first parameter should be the corresponding value of tt_thread_id.
  2. Loop through thread_status again. If no member contains tt_status being 0, you know that you shouldn't fire off a new thread at this point. Otherwise, set xxx to the index in the array of one of the elements containing tt_status equal to 0, and continue with the following step.
  3. Set thread_status[xxx].tt_status to 1. Then fire off a new thread with pt_create(). Let the pointer passed as the first parameter to pthread_create() be &(thread_status[xxx].tt_thread_id). Let the fourth parameter to pt_create() be thread_status+xxx.
Each thread, when started, gets a pointer to the relevant member of thread_status. When that thread is done, it should set the tt_status field to 2 just before calling pthread_exit().

Hope this helps.
 
Old 12-29-2009, 03:22 AM   #9
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by wje_lq View Post
Hope this helps.
ya. thanks.
 
Old 12-29-2009, 04:07 AM   #10
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 3
Quote:
Originally Posted by wje_lq View Post
[*]Determine how many cores are available in the CPU. Since this will be done only once per run of the program, you can rely on a crude but effective hack. There are many variants. Here's one:
Code:
#include <stdlib.h>

/* ... */

if(system("grep ^processor /proc/cpuinfo | wc -l > cpucount.txt"))
{
  /* insert error handling code here */
}

/* then read the answer from the file cpucount.txt */
Somewhat offtopic, but here's another way to determine the number of cpus/cores:
Code:
#include <unistd.h>

long numberOfCpus = sysconf(_SC_NPROCESSORS_ONLN);
if(numberOfCpus < 1) {
  /* error */
}
 
1 members found this post helpful.
Old 12-29-2009, 09:28 AM   #11
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by haikan View Post
Somewhat offtopic, but here's another way
Not off topic at all. Thank you!
 
Old 12-29-2009, 11:22 PM   #12
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Code:
#include<unistd.h>
#include<stdio.h>

main()
{
	if(sysconf(_SC_NPROCESSORS_ONLN)<1)
		printf("error");
	else
		printf("number of CPUs: %ld\n",sysconf(_SC_NPROCESSORS_ONLN));
}
It is printing number fo CPUs: 2.


I referred the man unistd.h.
In that, _SC_NPROCESSORS_ONLN is not there...!!!

Last edited by chakka.lokesh; 12-29-2009 at 11:27 PM.
 
Old 12-30-2009, 12:28 AM   #13
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by chakka.lokesh View Post
I referred the man unistd.h.
In that, _SC_NPROCESSORS_ONLN is not there...!!!
One wouldn't expect _SC_NPROCESSORS_ONLN to be defined in unistd.h. unistd.h includes other files. In modern Linux systems, you'll see _SC_NPROCESSORS_ONLN defined in bits/confname.h
 
Old 12-30-2009, 04:00 PM   #14
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 3
Quote:
Originally Posted by chakka.lokesh View Post
It is printing number fo CPUs: 2.

I referred the man unistd.h.
In that, _SC_NPROCESSORS_ONLN is not there...!!!
man sysconf

Portable programs should if possible use the posix standardized sysconf instead of parsing text from /proc files.
 
Old 12-31-2009, 12:02 AM   #15
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by wje_lq View Post
One wouldn't expect _SC_NPROCESSORS_ONLN to be defined in unistd.h
dear wje_lq,

Cooooooool. I am not speaking about definition. I am speaking about manual page. I am speaking about "man unistd.h"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to increase number of threads per process? Vaish Linux - Distributions 8 02-25-2010 01:21 AM
some threads are become unnoticed because of large number of continious threads deepak_cucek LQ Suggestions & Feedback 9 08-20-2009 11:21 PM
Number of threads per process avijitp Solaris / OpenSolaris 2 06-22-2009 08:18 AM
Can i find all threads by a process? msbinu Linux - Newbie 1 09-19-2008 12:32 AM
how find pid of process in C language (POSIX) shifter Programming 6 04-20-2007 02:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration