tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
* SUMMARY :  C code to have CPU and RAM USAGE (pct) of a given proces s
@ 1998-11-26 16:55 lombardi emanuele
  0 siblings, 0 replies; only message in thread
From: lombardi emanuele @ 1998-11-26 16:55 UTC (permalink / raw)
  To: tru64-unix-managers


Hi guys & dolls!

I had so many answers and I did succeded in making a C routine which,
given a PID , returns a string containing the following info about the
process:

 PID      UID PRI NICE  SIZE   RES STATE   TIME    CPU COMMAND

In case the process does'nt exist the routine returns a NULL string.

As you may notice the above fields are those provided by TOP. Infact I
simply cutted TOP sources to get what I needed.

The name of the routine is showproc and it is located at
	ftp://ftp@mantegna.casaccia.enea.it/lele/showproc.tar.gz 
which is 13000 byte and it contains all the necessary stuff (*.c & *.h)
and an example main program (example.c) wich shows how to use my
routine.

I have to thanks a lot of people who suggested what to do and some of
them worked to give me a good starting point.
There are two main suggestions 

1) access /proc info
2) grab code from TOP


The info in proc(4) can easily be accessed by ioctl(2) and many of
the answers contained some code to get some info from /proc. 
The problem is that the /proc info are not enough to compute %CPU used
by processes. That is why I had to grab TOP source.

Here a sample example on how to access /proc data:

#include <sys/types.h>
#include <sys/procfs.h>
#include <limits.h>
#include <fcntl.h>
#include <stdio.h>
main(int argc, char **argv)
{
   struct prpsinfo p;
   char procfile[PATH_MAX];
   int pid;
   int fd;
   if (argc !=2) {
      fprintf(stderr, "usage: %s pid\n", argv[0]);
      exit(1);
   }
   sprintf(procfile, "/proc/%s", argv[1]);
   if ((fd = open(procfile, O_RDONLY)) < 0) {
      perror(procfile);
      return -1;
   }
   if (ioctl(fd, PIOCPSINFO, &p) < 0){
      perror("prpsinfo");
      return -1;
   }
   printf("cpu: %d.%9.9d resident: %d\n",
      p.pr_time.tv_sec,
      p.pr_time.tv_nsec,
      p.pr_rssize);
}

Grabbing TOP sources has been a good way to improve my C knowledge
and it resulted in a working code. It is made of 4 *.c files and 6 *.h
files. (I changed as little as possible the TOP structure and I left
sorces name as they were. Only top.c has became showproc.c)
I don't attach them here , but you can get them from the above URL.

I really want to thank all of you who helped me:

	Christophe Colle <colle@pandora.be>
	soma_c@decus.fr (Claude SOMA - CNTS)
	alan@nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward Inodes.)
	"Dr. Tom Blinn, 603-884-0646" <tpb@doctor.zk3.dec.com>
	Keith Piepho <kap@uakron.edu>
	"Degerness, Mandell ITSD:EX" <Mandell.Degerness@gems2.gov.bc.ca>
	Oisin McGuinness <oisin@sbcm.com>
	Francesco Zerra <zerra@cmns.mnegri.it>
	"Dr. Tom Blinn, 603-884-0646" <tpb@doctor.zk3.dec.com>
	"Serguei Patchkovskii" <patchkov@ucalgary.ca>
	Debra Alpert <alpert@fas.harvard.edu>
	Kurt Carlson <snkac@java.sois.alaska.edu> 
		who gave some good pointers:
		Both uaklogin and the ua_get_ps.c routine are available in:
		ftp://raven.alaska.edu/pub/sois/README.uakpacct
		 kit:	ftp://raven.alaska.edu/pub/sois/uakpacct-v2.7.tar.Z

		The complete libcci library & man page (terse as it is) are in:
		ftp://raven.alaska.edu/pub/sois/README.uak
  		kit:	ftp://raven.alaska.edu/pub/sois/uak-v1.9.tar.Z

Greetings from ITALY,

Emanuele

-- 
 Emanuele Lombardi
 mail:  AMB-GEM-CLIM ENEA Casaccia
        I-00060 S.M. di Galeria (RM)  ITALY
 mailto:lele@mantegna.casaccia.enea.it
 tel	+39 6 30483366 fax	+39 6 30483591

     This transmission was made possible by 100% recycled electrons.




---- my original question:

Dear system managers,

Dunix 4.0d 

I' m looking for a piece of code which , given a Process Id, can print
the percentage of the CPUs and of the RAM the process is using.
I need the some value given by 
ps aux
but I would like to do it by C not by shell.

Any suggestion ?

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1998-11-26 16:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-11-26 16:55 SUMMARY : C code to have CPU and RAM USAGE (pct) of a given proces s lombardi emanuele

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox