2009-07-02 26 views
2

我使用times()函數來測量值,但我不確定我的方法是否正確。請看看和建議測量時間由一個exec() - 在Linux上的編輯過程

struct tms tms_start, tms_end; 
if (!(pid=fork())) 
{ 
    //some necessary operations here 
    times(&tms_start); 
    execl(...); 
} 
else if (pid) 
{ 
    //in parent 
    int status; 
    wait(&status); 
    times(&tms_end); 
    if (WIFEXITED(status)) 
    { 
     if(WEXITSTATUS(status)==0) 
     { 
      clock_t real = tms_end.tms_cstime - tms_start.tms_stime 
      float running_time = real/(double)sysconf(_SC_CLK_TK); 
     } 
    } 
} 

回答

5

你需要調用fork()之前調用times(&tms_start)。在上面的代碼中,tms_start變量在父項中未初始化,因爲父母從不調用times(&tms_start)

struct tms tms_start, tms_end; 
times(&tms_start);    // <-- here 
if (!(pid=fork())) 
{ 
    //some necessary operations here 
    execl(...); 
} 
else if (pid) 
{ 
    ... 
+0

啊我明白了。所以這個差異計算是正確的,我猜? clock_t real = tms_end.tms_cstime - tms_start.tms_stime – user108127 2009-07-02 20:09:04