//------------------------------------------------------------------- // myrusage.cpp // // This program demonstrates use of the 'getrusage()' function // as it is currently implemented under Linux (kernel 2.6.26). // // to compile: $ g++ myrusage.cpp -o myrusage // to execute: $ ./myrusage // // programmer: ALLAN CRUSE // written on: 09 MAR 2009 //------------------------------------------------------------------- #include // for printf(), perror() #include // for fork() #include // for exit() #include // for wait() #include // for getrusage() int main( int argc, char **argv ) { if ( fork() == 0 ) { printf( " Child-process will sleep\n" ); sleep(1); printf( " Child-process is exiting\n" ); exit(0); } wait( NULL ); struct rusage myrusage = { 0 }; if ( getrusage( RUSAGE_SELF, &myrusage ) < 0 ) { perror( "rusage" ); exit(1); } printf( "\n Current resource usages: \n" ); printf( "\n\t user time consumed: " ); printf( "%lu seconds ", myrusage.ru_utime.tv_sec ); printf( "%lu microseconds ", myrusage.ru_utime.tv_usec ); printf( "\n\tsystem time consumed: " ); printf( "%lu seconds ", myrusage.ru_stime.tv_sec ); printf( "%lu microseconds \n", myrusage.ru_stime.tv_usec ); printf( "\n\tpage reclaims: %lu ", myrusage.ru_minflt ); printf( "\tvoluntary context switches: %lu ", myrusage.ru_nvcsw ); printf( "\n\tpage faults: %lu \t", myrusage.ru_majflt ); printf( "\tinvoluntary context switches: %lu\n", myrusage.ru_nivcsw ); int len = sizeof( myrusage ); printf( "\n sizeof( myrusage ) = %d bytes ", len ); int n = sizeof( myrusage ) / sizeof( long ); unsigned long *lp = (unsigned long*)&myrusage; for (int i = 0; i < n; i++) { if ( (i % 4) == 0 ) printf( "\n " ); printf( "%016X ", lp[i] ); } printf( "\n\n" ); }