Operating System - HP-UX
1833323 Members
2833 Online
110051 Solutions
New Discussion

Getting CPU number from C program

 
SOLVED
Go to solution
Maurizio_12
Occasional Contributor

Getting CPU number from C program

How can I get number of CPU installed on the system by means of a system call (via C program)?
I didn't find any example program or ioctl usage example.
Thank in advance to anyone who knows an answer.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Getting CPU number from C program

Hi:

Have a look at 'pstat_getprocessor()'. Do 'man 2 pstat' for details.

Regards!

...JRF...
Caesar_3
Esteemed Contributor

Re: Getting CPU number from C program

Hello!

You have the pstat_getprocesor but it's not
much suported on every unix like OS.

Caesar
Umapathy S
Honored Contributor

Re: Getting CPU number from C program

Maurizio,
JRF got the answer. Check the examples section in man pages of pstat_getprocessor().

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Mike Stroyan
Honored Contributor

Re: Getting CPU number from C program

You can use pstat_getprocessor. If you are using pthreads, you can also use pthread_num_processors_np.

#include
#include
#include
#include

#include

int main(int argc, char *argv[])
{
struct pst_processor st;
int id;
for (id=0; pstat_getprocessor(&st, sizeof(st), (size_t)1, id)==1; id++) ;
printf("%d processors by pstat_getprocessor\n", id);


printf("%d processors by pthread_num_processors_np\n",
pthread_num_processors_np());
return 0;
}
Caesar_3
Esteemed Contributor

Re: Getting CPU number from C program

Hello!

Als ocheck this msg:
http://mail.nongnu.org/archive/html/gomp-discuss/2003-02/msg00263.html

There you will find another way to get this
info.

Caesar
twang
Honored Contributor

Re: Getting CPU number from C program

Hi,
You may get clock rate with this with "pstat_getprocessor()" function.
twang