ORIGINAL QUESTION:
I'm running Tru64 5.1.
>From a C program, I need to find out how many processes are currently in
the
system.
Is there a cfg_subsys_query( ) call I can make that will return the value
I'm looking for?
ANSWERS FROM:
alan_at_nabeth.cxo.cpqcorp.net
Oisin McGuinness
Paul A Sand
Hannes Visagie
Zelenák Gábor
MANY THANKS!
(Now I need to decide which is best for my application!!!)
++++++
The number of processes running or the number of processes
allowed? The number running isn't a configuration parameter
and probably won't have a handle. The other may be, but it
could also be dynamic.
For the number running, count the number of files in /proc.
+++++++
This code works on 4.0D
/* Attempt to get cpu info, and print it meaningfully. */
int cpuinfo(void) {
int cpu ;
struct cpu_info my_cpu ;
int rc ;
char name[1024] ;
name[0] = '\0' ;
rc = getsysinfo(GSI_CPU, &cpu, (sizeof cpu), 0, NULL, NULL) ;
if(rc < 0) return rc ;
rc = getsysinfo(GSI_CPU_INFO, &my_cpu, (sizeof my_cpu), 0, NULL,
NULL) ;
if(rc < 0) return rc ;
rc = getsysinfo(GSI_PLATFORM_NAME, name, 1024, 0, NULL, NULL) ;
if(rc < 0) return rc ;
printf("Cpuinfo: platform %s, cpu number %d of %d cpus with speed
%d, type %d (%d)\
n",
name,
my_cpu.current_cpu, my_cpu.cpus_in_box, my_cpu.mhz,
my_cpu.cpu_type, cpu) ;
return 0 ;
}
where man getsysinfo shows:
#include <sys/sysinfo.h>
#include <machine/hal_sysinfo.h>
getsysinfo(op, buffer, nbytes, start, arg, flag)
unsigned long op;
caddr_t buffer;
unsigned long nbytes;
int *start;
void *arg;
unsigned long *flag;
The program this is part of runs fine on 5.1.
(I'm not familiar with cfg_subsys_query...)
+++++++
There could well be an easier way to do it, but this seems to give a good
number. You need to be root.
#include <sys/table.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
main(int argc, char **argv)
{
int i, n, count;
struct tbl_procinfo *pit;
n = table(TBL_PROCINFO, 0, NULL, INT_MAX, 0);
pit = (struct tbl_procinfo *) malloc(n * sizeof(struct tbl_procinfo));
table(TBL_PROCINFO, 0, (void *)pit, n, sizeof(struct tbl_procinfo));
count = 0;
for (i = 0; i < n; i++) {
if (pit[i].pi_pid) {
count++;
}
}
printf("%d\n", count);
exit(0);
}
++++++
Think you will have to use the table function.
Here with 2 simple programs that I use to log Disk IO, CPU, and mem usage
every 2 second with hardly any extra overhead to the system. Please note
that
mem_free used is actually not 100% correct. It should not include ubc pages
{Unix FS buffers}, and inactive pages.
I also suggest that you get the source of "TOP". Peek around, it might give
you better Ideas.
#include <sys/table.h>
int pmeminfo(void)
{
struct tbl_vmstats vmstats;
unsigned long total_managed, total_free;
if (table(TBL_VMSTATS,0,&vmstats,1,sizeof(struct tbl_vmstats))<0) {
perror("TBL_VMSTATS");
return(0);
}
total_managed = vmstats.free_count + vmstats.active_count + \
vmstats.inactive_count + vmstats.wire_count + vmstats.ubc_pages;
total_free=vmstats.free_count + vmstats.ubc_pages +
vmstats.inactive_count;
/* Same as on X, but we want used */
// return(vmstats.active_count * 100 / total_manages);
return(100 - (total_free * 100 / total_managed));
}
int swapinfo(void)
{
struct tbl_swapinfo swapinfo;
unsigned long size, free;
long i;
/* First swap device */
if (table(TBL_SWAPINFO, 0,&swapinfo,1,sizeof(struct tbl_swapinfo))<0) {
perror("TBL_SWAPINFO");
return(0);
}
size = swapinfo.size;
free = swapinfo.free;
for (i=1; i > 0; i++)
{
/* Next swap devices */
if (table(TBL_SWAPINFO,i,&swapinfo,1,sizeof(struct tbl_swapinfo))<0)
break;
size += swapinfo.size;
free += swapinfo.free;
}
return(100 - (free * 100 / size));
}
int reserved_swapinfo(void)
{
struct tbl_swapinfo swapinfo;
unsigned long size, free;
/* Reserved swap info */
if (table(TBL_SWAPINFO,-1,&swapinfo,1,sizeof(struct tbl_swapinfo))<0) {
perror("TBL_SWAPINFO");
return(0);
}
size = swapinfo.size;
free = swapinfo.free;
return(100 - (free * 100 / size));
}
void sysinfo(void)
{
static struct tbl_sysinfo last_sibuf = {0};
struct tbl_sysinfo sibuf;
unsigned long sys, wait, user, idle, total;
char buf[1024];
/* array of cpu state counters */
if (table(TBL_SYSINFO,0,&sibuf,1,sizeof(struct tbl_sysinfo))<0) {
perror("TBL_SYSINFO");
return;
}
sys = sibuf.si_sys - last_sibuf.si_sys;
wait = sibuf.wait - last_sibuf.wait;
user = sibuf.si_user - last_sibuf.si_user;
user += sibuf.si_nice - last_sibuf.si_nice;
idle = sibuf.si_idle - last_sibuf.si_idle;
last_sibuf = sibuf;
total = sys + wait + user + idle;
sys = sys * 100 / total;
wait = wait * 100 / total;
user = user * 100 / total;
idle = idle * 100 / total;
sprintf(buf, "/usr/local/bin/rrdtool update /var/log/cpuload.rrd
N:%d:%d:%d:%d:%d:%d:%d", \
sys,wait,user,idle, pmeminfo(), swapinfo(),
reserved_swapinfo());
// puts(buf); return;
if (system(buf)) {
printf("sys %d wait %d user %d idle %d pmem %d swap %d rswap %d\n",
\
sys, wait, user, idle, pmeminfo(), swapinfo(), reserved_swapinfo());
exit(1);
}
}
main()
{
while (1)
{
sleep(2);
sysinfo();
}
}
++++++
if you're rady to write a C source to get the
process informations on your system... so,
write a code using PIOCTL commands to examine
/proc fil system.
there you can find all the actual processes - and their
statuses as well - existing in your system.
btw... have you heard about "top" utility?
I kindly advice you that use top to monitor
- in realtime as well - the CPU resources.
Received on Thu Mar 14 2002 - 14:07:44 NZDT