簡體   English   中英

測量父母和子女過程的時間

[英]Measuring time of parent and children processes

我寫了一個循環分叉的程序。 子進程執行的唯一操作是增加計數器並退出,而父進程等待它們中的每一個。

我的目標是分別測量父進程和所有子進程的用戶和系統時間。 我使用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;
}

我認為問題是你的孩子執行得太快 ; 他們沒有足夠的時間來執行,所以他們的時間總和就是零。 為了測試這個理論,我略微改變了你的程序:

#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;
}

你會看到我徹底減少了孩子的數量,讓孩子們做了一些真正的工作; 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

我擔心你的孩子沒有足夠的工作去做任何事情。 (奇怪,聽起來像育兒的建議。)

每個孩子都有自己的地址空間。 代碼將無法工作,因為它將增加它自己的counter本地副本並退出,使父進程/所有其他子進程中的版本保持不變。

此外,你很可能會遇到很多孩子的錯誤。

對不起,我只能幫助一半的程序:(。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM