当前位置:编程学习 > C/C++ >>

使用getrusage获取进程使用系统资源信息

print?#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <sys/resource.h>  
#include <time.h>  
#include <unistd.h>  
 
int main(void) 

    while(1) 
    { 
        struct rusage ru; 
        memset(&ru, 0, sizeof(struct rusage)); 
        getrusage(RUSAGE_SELF, &ru); 
        printf("ou/inblock:%ld,%ld,snd/rcvmsg:%ld,%ld,max/ix/idrss:%ld,%ld,%ld,min/majflt:%ld,%ld,signal:%ld,nv/nivcsw:%ld,%ld,swap:%ld,utime:%lu,%lu,stime:%lu,%lu\r\n", 
                ru.ru_oublock, ru.ru_inblock, 
                ru.ru_msgsnd, ru.ru_msgrcv, 
                ru.ru_maxrss, ru.ru_ixrss, ru.ru_idrss, 
                ru.ru_minflt, ru.ru_majflt, 
                ru.ru_nsignals, 
                ru.ru_nvcsw, ru.ru_nivcsw, 
                ru.ru_nswap, 
                ru.ru_utime.tv_usec, ru.ru_utime.tv_sec, 
                ru.ru_stime.tv_usec, ru.ru_stime.tv_sec); 
/*
    ru_oublock: 块输出操作
    ru_inblock: 块输入操作
    ru_msgsnd: 发送的message
    ru_msgrcv: 收到的message
    ru_maxrss: 最大驻留集大小
    ru_ixrss: 全部共享内存大小
    ru_idrss:全部非共享内存大小
    ru_minflt: 页回收
    ru_majflt: 页失效
    ru_nsignals: 收到的信号
    ru_nvcsw: 主动上下文切换
    ru_nivcsw: 被动上下文切换
    ru_nswap: 交换区
    ru_utime.tv_usec: 用户态时间 (microseconds)
    ru_utime.tv_sec: 用户态时间(seconds)
    ru_stime.tv_usec: 系统内核时间 (microseconds)
    ru_stime.tv_sec: 系统内核时间(seconds)
*/ 
        //do something  
 
        sleep(1); 
    } 
    return 0; 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <time.h>
#include <unistd.h>

int main(void)
{
    while(1)
    {
        struct rusage ru;
        memset(&ru, 0, sizeof(struct rusage));
        getrusage(RUSAGE_SELF, &ru);
        printf("ou/inblock:%ld,%ld,snd/rcvmsg:%ld,%ld,max/ix/idrss:%ld,%ld,%ld,min/majflt:%ld,%ld,signal:%ld,nv/nivcsw:%ld,%ld,swap:%ld,utime:%lu,%lu,stime:%lu,%lu\r\n",
                ru.ru_oublock, ru.ru_inblock,
                ru.ru_msgsnd, ru.ru_msgrcv,
                ru.ru_maxrss, ru.ru_ixrss, ru.ru_idrss,
                ru.ru_minflt, ru.ru_majflt,
                ru.ru_nsignals,
                ru.ru_nvcsw, ru.ru_nivcsw,
                ru.ru_nswap,
                ru.ru_utime.tv_usec, ru.ru_utime.tv_sec,
                ru.ru_stime.tv_usec, ru.ru_stime.tv_sec);
/*
    ru_oublock: 块输出操作
    ru_inblock: 块输入操作
    ru_msgsnd: 发送的message
    ru_msgrcv: 收到的message
    ru_maxrss: 最大驻留集大小
    ru_ixrss: 全部共享内存大小
    ru_idrss:全部非共享内存大小
    ru_minflt: 页回收
    ru_majflt: 页失效
    ru_nsignals: 收到的信号
    ru_nvcsw: 主动上下文切换
    ru_nivcsw: 被动上下文切换
    ru_nswap: 交换区
    ru_utime.tv_usec: 用户态时间 (microseconds)
    ru_utime.tv_sec: 用户态时间(seconds)
    ru_stime.tv_usec: 系统内核时间 (microseconds)
    ru_stime.tv_sec: 系统内核时间(seconds)
*/
        //do something

        sleep(1);
    }
    return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,