From: "Eiler, James A." <James.Eiler () alcoa ! com>
To: tru64-unix-managers
Subject: SUMMARY: Finding current number of processes
Date: Thu, 14 Mar 2002 14:07:23 +0000 [thread overview]
Message-ID: <102653731622723.tru64-unix-managers.marc@archive.invalid> (raw)
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@nabeth.cxo.cpqcorp.net
Oisin McGuinness
Paul A Sand
Hannes Visagie
Zelen=E1k G=E1bor
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] =3D '\0' ;
rc =3D getsysinfo(GSI_CPU, &cpu, (sizeof cpu), 0, NULL, NULL) ;
if(rc < 0) return rc ;
rc =3D getsysinfo(GSI_CPU_INFO, &my_cpu, (sizeof my_cpu), 0, =
NULL,
NULL) ;
if(rc < 0) return rc ;
rc =3D 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 =3D table(TBL_PROCINFO, 0, NULL, INT_MAX, 0);
pit =3D (struct tbl_procinfo *) malloc(n * sizeof(struct =
tbl_procinfo));
table(TBL_PROCINFO, 0, (void *)pit, n, sizeof(struct =
tbl_procinfo));
count =3D 0;
for (i =3D 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 =3D vmstats.free_count + vmstats.active_count + \
vmstats.inactive_count + vmstats.wire_count + vmstats.ubc_pages;
total_free=3Dvmstats.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 =3D swapinfo.size;
free =3D swapinfo.free;
for (i=3D1; i > 0; i++)
{
/* Next swap devices */
if (table(TBL_SWAPINFO,i,&swapinfo,1,sizeof(struct =
tbl_swapinfo))<0)
break;
size +=3D swapinfo.size;
free +=3D 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 =3D swapinfo.size;
free =3D swapinfo.free;
return(100 - (free * 100 / size));
}
void sysinfo(void)
{
static struct tbl_sysinfo last_sibuf =3D {0};
struct tbl_sysinfo sibuf;
unsigned long sys, wait, user, idle, total;
char buf[1024];
=20
/* array of cpu state counters */
if (table(TBL_SYSINFO,0,&sibuf,1,sizeof(struct tbl_sysinfo))<0) {
perror("TBL_SYSINFO");
return;
}
sys =3D sibuf.si_sys - last_sibuf.si_sys;
wait =3D sibuf.wait - last_sibuf.wait;
user =3D sibuf.si_user - last_sibuf.si_user;
user +=3D sibuf.si_nice - last_sibuf.si_nice;
idle =3D sibuf.si_idle - last_sibuf.si_idle;
last_sibuf =3D sibuf;
total =3D sys + wait + user + idle;
=20
sys =3D sys * 100 / total;
wait =3D wait * 100 / total;
user =3D user * 100 / total;
idle =3D 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);
} =20
}
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.=20
btw... have you heard about "top" utility?
I kindly advice you that use top to monitor=20
- in realtime as well - the CPU resources.
next reply other threads:[~2002-03-14 14:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-03-14 14:07 Eiler, James A. [this message]
-- strict thread matches above, loose matches on Subject: below --
2002-03-14 14:00 SUMMARY: Finding current number of processes Eiler, James A.
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox