2012-01-09 12 views
1

wait4forkのプロセスを実行しようとしていますが、rusageの戻り値はstimeutimeの両方で一貫してゼロです。私はプロセスがforkであることを確認しました。execが適切に編集され、クロックサイクルが数秒間しかテストされないテストプロセスの場合、rusageの値は正常です(>0)。C fork()、wait4()issue

私は、使用しているクロックの解像度が短いプロセスを適切に実行するのに不十分だと思っていますが、a)ALL clockid_tタイプの応答が1nsであり、b)指定方法がわかりませんwait4()にはどのクロックを使用する必要があります。ここで

は私のコードです:

double spawnprocess(char *arg1, char *arg2){ 
    struct rusage usage; 
    struct timeval time; 
    double t1 = 0.0; 
    int nogo; 
    pid_t pid; 
    char* args[] = { "/path/to/process", arg1, arg2, (char *) 0 }; 

    do { 
    pid = fork(); 
    if (pid == -1) { printf("Error forking... trying again.\n"); sleep(1); } 
    } while (pid == -1); 

    if (pid == 0) { 
    if ((nogo=execv(args[0], args)) == -1) printf("Error starting new process.\n"); 
    } else { 
    printf("Waiting for child %d...\n", pid); 
    int status; 
    int options = 0; 
    pid_t cpid = wait4(pid, &status, options, &usage); 
    if (cpid != pid) { 
     printf("Error: %d\n", cpid); 
     int tmp; 
     if (WIFEXITED(status)) printf("%s", "WIFEXITED"); 
     if ((tmp = WEXITSTATUS(status))) printf("WEXITSTATUS: %d", tmp); 
     if (WIFSIGNALED(status)) printf("%s", "WIFSIGNALED"); 
     if ((tmp = WTERMSIG(status))) printf("WTERMSIG: %d", tmp); 
     if (WCOREDUMP(status)) printf("%s", "Core dumped."); 
     if (WIFSTOPPED(status)) printf("%s", "WIFSTOPPED"); 
     if (WSTOPSIG(status)) printf("%s", "WSTOPSIG"); 
     if (WIFCONTINUED(status)) printf("%s", "WIFCONTINUED"); 
    } else { 
     t1 = (usage.ru_utime.tv_sec + (usage.ru_utime.tv_usec/1000000.0)); 
    } 
    } 
printf("Sent value is: %e\n", t1); 
return t1; 
} 

答えて

1

システムタイマー解像度をナノ秒しているかもしれませんが、あなたはそれを見ることができることを十分にあなたの方法から抜け出すためにリアルタイム動作のために設計されたシステムが必要になりますタイミング解決の一種。私はここにある非Linuxシステムであなたのコードを試しました。そして、Pentium IIIで動作する古いFreeBSDシステムは、時々0と4e-03を別々に印刷します。最近のPowerPCとIntel Macは、4e-05〜2e-04の範囲の数値を印刷します。