Linux系統調用--getrusage函數詳解功能描述:獲得進程的相關資源信息。如:用戶開銷時間,系統開銷時間,接收的信號量等等;用法:#include #include #include #define RUSAGE_SELF 0#define RUSAGE_CHILDREN -1int get...
Linux系統調用--getrusage函數詳解
功能描述: 獲得進程的相關資源信息。如:用戶開銷時間,系統開銷時間,接收的信號量等等; 用法: #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); 當調用成功後,返回0,否則-1; 參數: who:可能選擇有 RUSAGE_SELF:獲取當前進程的資源使用信息。以當前進程的相關信息來填充rusage(數據)結構 RUSAGE_CHILDREN:獲取子進程的資源使用信息。rusage結構中的數據都將是當前進程的子進程的信息 usage:指向存放資源使用信息的結構指針。 rusage(數據)結構定義在/usr/include/sys/resource.h中,下麵是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 ru_nsignals; // signals received long ru_nvcsw; // voluntary context switches long ru_nivcsw; // involuntary context switches }; 舉個例子: int who = RUSAGE_SELF; struct rusage usage; int ret; ret = getrusage(who, &usage); 這樣你就可以用usage獲取你想要的東西了; 同樣wait3()和wait4()兩個函數也可以得到進程資源信息. 得到CPU使用時間的用法: 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;