c - 测量父进程和子进程的时间

标签 c linux

我编写了一个循环 fork 的程序。子进程唯一要做的就是增加计数器并退出,而父进程等待它们中的每一个。

我的目标是分别测量父进程及其所有子进程的用户和系统时间。 我已经使用 times() 函数和 struct tms 成功完成了父进程。令人惊讶的是,对子进程的相同方法不起作用。我在做什么错误?如何衡量这些时间?

我也试过 getrusage() 但失败了。

我的代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/times.h>
#include <time.h>

#ifndef COUNT
#define COUNT 100000
#endif



int counter;


int main(){

struct tms time1,time2;
times(&time1);

int count = COUNT;
pid_t pid;
while(count--){
    if((pid=fork())<0){
        printf("fork error\n");
    } else if(pid==0){ /* child */
        counter++;
        _exit(0);
    } else {
        waitpid(pid,NULL,0); /*wait()*/
    }
}
printf("COUNTER: %d\n",counter);



times(&time2);

long double clktck=sysconf(_SC_CLK_TCK);
double user=(time2.tms_utime-time1.tms_utime)/(double)clktck;
double system=(time2.tms_stime-time1.tms_stime)/(double)clktck;
double cuser=(time2.tms_cutime-time1.tms_cutime)/(double)clktck;
double csystem=(time2.tms_cstime-time1.tms_cstime)/(double)clktck;

printf("USER:%lf\nSYSTEM:%lf\n",user,system);
printf("CUSER:%lf\nCSYSTEM:%lf\n",cuser,csystem);



return 0;
}

最佳答案

我认为问题在于您的 child 执行太快;他们没有花足够的时间来执行,所以他们的时间总和是很多零。为了检验这个理论,我稍微修改了你的程序:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/times.h>
#include <time.h>

#ifndef COUNT
#define COUNT 100
#endif



int counter;


int main(){

struct tms time1,time2;
times(&time1);

int count = COUNT;
pid_t pid;
while(count--){
    if((pid=fork())<0){
        printf("fork error\n");
    } else if(pid==0){ /* child */
        int i;
        for (i=0; i<10000; i++) {
            printf("in child %i\n", getpid());
        }
        exit(0);
    } else {
        waitpid(pid,NULL,0); /*wait()*/
    }
}
printf("COUNTER: %d\n",counter);



times(&time2);

printf("%lu %lu %lu %lu\n", time2.tms_utime, time2.tms_stime, time2.tms_cutime, time2.tms_cstime);

long double clktck=sysconf(_SC_CLK_TCK);
double user=(time2.tms_utime-time1.tms_utime)/(double)clktck;
double system=(time2.tms_stime-time1.tms_stime)/(double)clktck;
double cuser=(time2.tms_cutime-time1.tms_cutime)/(double)clktck;
double csystem=(time2.tms_cstime-time1.tms_cstime)/(double)clktck;

printf("USER:%lf\nSYSTEM:%lf\n",user,system);
printf("CUSER:%lf\nCSYSTEM:%lf\n",cuser,csystem);



return 0;
}

你会看到我大大减少了 child 的数量,让 children 做一些真正的工作; 10_000 printf(... getpid()) 操作。 现在时代已经成为某种东西:

$ time ./times
...
in child 16181
COUNTER: 0
1 0 24 95
USER:0.010000
SYSTEM:0.000000
CUSER:0.240000
CSYSTEM:0.950000

real    0m2.234s
user    0m0.250s
sys 0m0.950s

恐怕您的 child 只是没有足够的工作来成就任何事情。 (奇怪,听起来像是育儿建议。)

关于c - 测量父进程和子进程的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5608984/

相关文章:

linux - K8s 没有杀死我的 airflow 网络服务器 pod

Linux文本文件处理——连接断线

android - 如何通过本地网络中的主机名查找IP地址?

php - 获取文件的 mimetype 作为字符串 (imap_fetchbody)

c - PTrace 无法识别子进程

c - 如何区分管道和交互式标准输入

c++ - 在 linux 上编译 libfacebookcpp,未声明 'StringBuilder'

c++ - 是否可以使用宏来创建 typedef?

C:尺寸大于 1000 的二维数组相乘会导致段错误

在 C 中将 32 位和 64 位数字转换为 IEEE 754 二进制