Call the getrusage function in Linux.

Source: Internet
Author: User

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;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.