2012-01-09 2 views
1

wait4으로 fork 처리를 시도하고 있지만및 utime에 대해 rusage 반환 값이 일관되게 0입니다. 나는 프로세스가 fork이고 제대로 작동하는지, exec인지 제대로 확인했으며, 내가 exec 수십 초 동안 시계 사이클 만 먹는 테스트 프로세스 일 때 rusage 값은 sane (>0)입니다.C fork(), wait4() 문제

사용중인 시계의 해상도가 짧은 프로세스를 제대로 처리하는 데 충분하지 않다고 생각합니다. 그러나 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

시스템 타이머 : 여기

내 코드입니다 일종의 타이밍 해상도. 나는 여기에있는 리눅스가 아닌 시스템에서 당신의 코드를 시도했다. 그리고 Pentium III에서 실행되는 오래된 FreeBSD 시스템은 때때로 0과 4e-03을 인쇄 할 것입니다. 최근의 PowerPC 및 Intel Mac은 4e-05 ~ 2e-04 범위의 숫자를 인쇄합니다.