pthonda@bgsuvax.UUCP (Killer B) (04/08/91)
Hi I am trying to read the resource usage of each process in the system . The structure for each process has a pointer to a resource structure.After getting process information, when I try to print its resource usage, I am getting some garbage. Infact the contents of the pointer to resource structure , is 0, or in other words this pointer is pointing to location 0. Could you suggest some method, and also if I want to print for all the processes ,should I check for EOF of file 'kmem'. Thanx in advance Thanx in advance Pratap. Here is the source code for your reference: /*-------------------------------------------------------------------*/ #include <stdio.h> #include <types.h> #include <nlist.h> #include <time.h> #include <sys/resource.h> #include <sys/proc.h> #include <sys/fcntl.h> #include <sys/file.h> #define MAX 10 #define ZERO 0 #define ERROR -1 #define NOERROR 0 #define VMUNIX "/vmunix" #define KMEM "/dev/kmem" #define PTR_KMEM "_proc" int fileDes ; main(argc,argv) int argc ; char *argv[] ; { int ret_cd ; /* Position in Kmem file */ if((ret_cd = posKmem()) == ERROR) { printf("Error in handling Kmem file \n") ; exit(0) ; } /* process Kmem file */ if((ret_cd = proKmem()) == ERROR) { printf("Error in processing Kmem file \n") ; exit(0) ; } /* Close all open files */ close(fileDes) ; } int posKmem() { int ret_cd ; struct nlist nl[2] ; unsigned long proOff ; /* Get _proc address from 'vmunix file in read mode' */ nl[0].n_name = PTR_KMEM ; nl[0].n_type = N_DATA ; nl[1].n_name = (char *) NULL ; if((ret_cd = nlist(VMUNIX,nl)) < ZERO) { perror("Nlist failed") ; return(ERROR) ; } /* Open file 'kmem' in read mode */ if((fileDes = open(KMEM,O_RDONLY)) < ZERO) { perror("Kmem open failed") ; return(ERROR) ; } /* Seek to the offset returned by nlist */ if((ret_cd = lseek(fileDes,nl[0].n_value,L_SET)) < nl[0].n_value) { perror("Lseek failed") ; return(ERROR) ; } if((ret_cd = read(fileDes,(char *) &proOff,sizeof(long))) < ZERO) { perror("Read failed") ; return(ERROR) ; } /* Seek to the offset returned by read */ if((ret_cd = lseek(fileDes,proOff,L_SET)) < proOff) { perror("Lseek failed") ; return(ERROR) ; } return(NOERROR) ; } int proKmem() { char *malloc() ; int ret_cd, ix ; struct proc *ptrProc ; /* Get storage for proc structure */ if((ptrProc = (struct proc *) malloc(sizeof(struct proc))) == (struct proc *) NULL) { perror("Malloc failed") ; return(ERROR) ; } /* Read process structures */ for (ix = 0 ; ix < MAX ; ix++ ) { if((ret_cd = read(fileDes,(char *) ptrProc, sizeof(struct proc))) < sizeof(struct proc)) { perror("Read failed") ; return(ERROR) ; } disPinfo(ptrProc) ; } return(NOERROR) ; } int disPinfo(ps) struct proc *ps ; { struct rusage *ru ; ru = ps->p_ru ; /* ps->p_ru is pointing to location 0 , and I dont know why */ /* Print process info and then the resource usage */ printf("Process Information of process: %d\n" ,ps->p_pid) ; printf("----------------------------------------------------------\n") ; printf("PPID\tP_UID\tP_CPU\tP_GRP\n") ; printf("%hd\t\t%d\t\t%c\t\t%hd\n",ps->p_ppid,ps->p_uid,ps->p_cpu, ps->p_pgrp) ; printf("----------------------------------------------------------\n") ; printf("Resource Information of process: %d\n" ,ps->p_pid) ; printf("----------------------------------------------------------\n") ; printf("Page reclaims\tPage faults\tMessages received\tSignals\n") ; printf("%ld\t\t%ld\t\t%ld\t\t%ld\n\n\n ", ru->ru_minflt, ru->ru_majflt, ru->ru_msgrcv,ru->ru_nsignals); return(NOERROR) ; } /*--------------------E N D O F D A Y ' S P L A Y--------------------------*/