#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#define RUSAGE_SELF 0
#define RUSAGE_CHILDREN -1
int getrusage(int who, struct rusage *rusage);
getrusage函数有两个参数。第一个参数可以设置为RUSAGE_SELF或者RUSAGE_CHILDREN。如果设置成 RUSAGE_SELF,那么将会以当前进程的相关信息来填充rusage(数据)结构。反之,如果设置成RUSAGE_CHILDREN,那么 rusage结构中的数据都将是当前进程的子进程的信息。( RUSAGE_CHILDREN
Return resource usage statistics for all children of the calling process that have terminated and been waited for. These statistics will
include the resources used by grandchildren, and further removed descendants, if all of the intervening descendants waited on their termi‐
nated children.)
linux 增加了第三个参数 RUSAGE_THREAD (since Linux 2.6.26)
Return resource usage statistics for the calling thread.
rusage(数据)结构定义在/usr/include/sys/resource.h中。它含有以下成员变量:
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* max resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data */
long ru_isrss; /* integral unshared stack */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary " */
};
示例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
int
main(int argc, char **argv)
{
struct rusage buf;
if(argc == 2) {
system(argv[1]);
}else {
fprintf(stderr,"./getrusage /"ls -l >/dev/null/"/n");
exit(0);
}
int err = getrusage(RUSAGE_CHILDREN, &buf);
//int err = getrusage(RUSAGE_SELF, &buf);
printf("ERR=%d/n", err);
printf("%20s:%ld/%ld/t%s/n", "ru_utime",
buf.ru_utime.tv_sec, buf.ru_utime.tv_usec,
"user time used (secs/usecs)");
printf("%20s:%ld/%ld/t%s/n", "ru_stime",
buf.ru_stime.tv_sec,
buf.ru_stime.tv_usec,
"system time used (secs/usecs)");
printf("%20s:%-10ld/t%s/n",
"ru_maxrss",
buf.ru_maxrss,
"maximum resident set size");
printf("%20s:%-10ld/t%s/n",
"ru_ixrss",
buf.ru_ixrss,
"integral shared memory size");
printf("%20s:%-10ld/t%s/n",
"ru_idrss",
buf.ru_idrss,
"integral unshared data size");
printf("%20s:%-10ld/t%s/n",
"ru_isrss",
buf.ru_isrss,
"integral unshared data stack size");
printf("%20s:%-10ld/t%s/n",
"ru_minflt",
buf.ru_minflt,
"page reclaims");
printf("%20s:%-10ld/t%s/n",
"ru_majflt",
buf.ru_majflt,
"page faults");
printf("%20s:%-10ld/t%s/n",
"ru_nswap",
buf.ru_nswap,
"swaps");
printf("%20s:%-10ld/t%s/n",
"ru_inblock",
buf.ru_inblock,
"block input operations");
printf("%20s:%-10ld/t%s/n",
"ru_oublock",
buf.ru_oublock,
"block output operations");
printf("%20s:%-10ld/t%s/n",
"ru_msgsnd",
buf.ru_msgsnd,
"messages sent");
printf("%20s:%-10ld/t%s/n",
"ru_msgrcv",
buf.ru_msgrcv,
"messages received");
printf("%20s:%-10ld/t%s/n",
"ru_nsignals",
buf.ru_nsignals,
"signals received");
printf("%20s:%-10ld/t%s/n",
"ru_nvcsw",
buf.ru_nvcsw,
"voluntary context switches");
printf("%20s:%-10ld/t%s/n",
"ru_nivcsw",
buf.ru_nivcsw,
"involuntary context switches");
exit(0);
}