What are "before" and "after" used in getrusage? Why and how they are used? I also find manuel's explanation about RUSAGE_SELF hard to understand. Thanks in advance.


1 Answer 1


As denoted by the man page for getrusage, it takes 2 arguments, an int and a struct rusage. before and after are of type struct rusage whose definition is

struct rusage {
    struct timeval ru_utime; /* user CPU time used */
    struct timeval ru_stime; /* system CPU 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 (soft page faults) */
    long   ru_majflt;        /* page faults (hard page faults) */
    long   ru_nswap;         /* swaps */
    long   ru_inblock;       /* block input operations */
    long   ru_oublock;       /* block output operations */
    long   ru_msgsnd;        /* IPC messages sent */
    long   ru_msgrcv;        /* IPC messages received */
    long   ru_nsignals;      /* signals received */
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */

getrusage basically takes a second argument of this type and sets some of its fields. Each field stores the information corresponding to the comment in front of it. For example, the ru_utime is the total amount of time spent executing in user mode — the mode in which case the executing code has no direct access to the hardward.

To understand what RUSAGE_SELF is, you need to understand what a process is and what a thread is. As the Wikipedia article suggests

a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.

Back to the first parameter, the value RUSAGE_SELF basically tells getrusage to fill the second argument with information about the resources used by the current process (i.e., the process that represents an instance of your program).

  • Thanks for providing such a detailed explanation. There is some other questions I don't understand. before and after is not specified in the definition of rusage, and I noticed that they are pointers in the spellers program. What do they point to?
    – eifphysics
    Feb 17, 2015 at 20:55
  • @eifphysics as the beginning of the answer denotes, before and after are not pointers. They are two variables of type struct rusage. A pointer to each one of them is passed to getrusage() when called because it accepts an int and a pointer of type struct rusage.
    – kzidane
    Feb 17, 2015 at 21:33
  • @Kareem: although I have not asked this question, I would like to thank you for this helpful explanation!
    – Vitale
    Feb 16, 2016 at 10:59
  • thanks this was helpful too Jan 6, 2017 at 3:45
  • Thank you very much! it's helpful.
    – Damon Liao
    Aug 26, 2018 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .