Use time() instead of clock() for timeouts.
The clock() function returns the processor time used by the process. This is not a good timeout mechanism since the code is suspended most of the time waiting for the forked process to finish. Replace with the time() function. Bug: 17154069 Change-Id: Ib383329f9db269445d71a100553b08e71fddda3f
This commit is contained in:
parent
7eff26a02a
commit
89d4949f86
@ -296,7 +296,7 @@ int dump_file_from_fd(const char *title, const char *path, int fd) {
|
|||||||
/* forks a command and waits for it to finish */
|
/* forks a command and waits for it to finish */
|
||||||
int run_command(const char *title, int timeout_seconds, const char *command, ...) {
|
int run_command(const char *title, int timeout_seconds, const char *command, ...) {
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
clock_t start = clock();
|
time_t start = time(NULL);
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
/* handle error case */
|
/* handle error case */
|
||||||
@ -340,19 +340,19 @@ int run_command(const char *title, int timeout_seconds, const char *command, ...
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
int status;
|
int status;
|
||||||
pid_t p = waitpid(pid, &status, WNOHANG);
|
pid_t p = waitpid(pid, &status, WNOHANG);
|
||||||
float elapsed = (float) (clock() - start) / CLOCKS_PER_SEC;
|
time_t elapsed = time(NULL) - start;
|
||||||
if (p == pid) {
|
if (p == pid) {
|
||||||
if (WIFSIGNALED(status)) {
|
if (WIFSIGNALED(status)) {
|
||||||
printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
|
printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
|
||||||
} else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
|
} else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
|
||||||
printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
|
printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
|
||||||
}
|
}
|
||||||
if (title) printf("[%s: %.1fs elapsed]\n\n", command, elapsed);
|
if (title) printf("[%s: %ds elapsed]\n\n", command, (int) elapsed);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeout_seconds && elapsed > timeout_seconds) {
|
if (timeout_seconds && elapsed > timeout_seconds) {
|
||||||
printf("*** %s: Timed out after %.1fs (killing pid %d)\n", command, elapsed, pid);
|
printf("*** %s: Timed out after %ds (killing pid %d)\n", command, (int) elapsed, pid);
|
||||||
kill(pid, SIGTERM);
|
kill(pid, SIGTERM);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user