From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96078728412219.tru64-unix-managers.marc@archive.invalid> From: lombardi emanuele Date: Thu, 26 Nov 1998 16:55:34 +0000 To: tru64-unix-managers Subject: SUMMARY : C code to have CPU and RAM USAGE (pct) of a given proces s X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96078728412219 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 #include #include #include #include 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 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" Keith Piepho "Degerness, Mandell ITSD:EX" Oisin McGuinness Francesco Zerra "Dr. Tom Blinn, 603-884-0646" "Serguei Patchkovskii" Debra Alpert Kurt Carlson 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 ?