SUMMARY : C code to have CPU and RAM USAGE (pct) of a given proces s

From: lombardi emanuele <lele_at_mantegna.casaccia.enea.it>
Date: Thu, 26 Nov 1998 17:55:34 +0100 (MET)

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_at_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_at_pandora.be>
        soma_c_at_decus.fr (Claude SOMA - CNTS)
        alan_at_nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward Inodes.)
        "Dr. Tom Blinn, 603-884-0646" <tpb_at_doctor.zk3.dec.com>
        Keith Piepho <kap_at_uakron.edu>
        "Degerness, Mandell ITSD:EX" <Mandell.Degerness_at_gems2.gov.bc.ca>
        Oisin McGuinness <oisin_at_sbcm.com>
        Francesco Zerra <zerra_at_cmns.mnegri.it>
        "Dr. Tom Blinn, 603-884-0646" <tpb_at_doctor.zk3.dec.com>
        "Serguei Patchkovskii" <patchkov_at_ucalgary.ca>
        Debra Alpert <alpert_at_fas.harvard.edu>
        Kurt Carlson <snkac_at_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_at_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 ?
Received on Thu Nov 26 1998 - 16:58:23 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:38 NZDT