Call the getrusage function in Linux.
Call the getrusage function in Linux
Function Description:Obtain process-related resource information. For example, user overhead time, system overhead Time, and received semaphore;
Usage:# 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 * usage); if the call is successful, 0 is returned; otherwise,-1 is returned;
Parameters:
Who: You may select RUSAGE_SELF to obtain the resource usage information of the current process. Fill in the rusage (data) structure with the relevant information of the current process RUSAGE_CHILDREN: Get the resource usage information of the sub-process. The data in the rusage structure will be the information of the sub-process of the current process.
Usage: Structure pointer pointing to storing resource usage information. The rusage structure is defined in/usr/include/sys/resource. in h, the following is the structure of rusage: struct rusage {struct timeval ru_utime; // user time used struct timeval ru_stime; // system time used long ru_maxrss; // maximum resident set size long ru_ixrss; // integral shared memory size long ru_idrss; // integral unshared data size long ru_isrss; // integral unshared stack size 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 response; // signals received long ru_nvcsw; // voluntary context switches long response; // involuntary context switches}; example: int who = RUSAGE_SELF; struct rusage usage; int ret; ret = getrusage (who, & usage); then you can use usage to get what you want. Similarly, wait3 () and wait4 () functions can also obtain process resource information. obtain the usage of CPU usage time: struct rusage rup; getrusage (RUSAGE_SELF, & rup); long sec = rup.ru _ utime. TV _sec + rup.ru _ stime. TV _sec; long usec = rup.ru _ utime. TV _usec + rup.ru _ stime. TV _usec; sec + = usec/1000000; usec %= 1000000;