PSI sics-cvs-psi_pre-ansto

This commit is contained in:
2003-06-13 00:00:00 +00:00
committed by Douglas Clowes
parent 2e3ddfb6c6
commit 3ffd0d8af4
1099 changed files with 318432 additions and 0 deletions

49
tecs/keep_running.c Normal file
View File

@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
#define MAX_CNT 10
#define MIN_PERIOD 3600
int main(int argc, char *argv[]) {
int cnt, dif;
time_t tim1, tim2;
pid_t pid;
int status, i;
if (argc<2) return 0;
cnt = MAX_CNT;
while (cnt > 0) {
time(&tim1);
pid=vfork();
if (pid == 0) {
execvp(argv[1], argv+1);
perror("execvp");
exit(1);
}
waitpid(pid, &status, 0);
if (status==512) { /* kill */
return 0;
}
if (status==256) { /* restart */
cnt = MAX_CNT;
}
time(&tim2);
dif = (int) difftime(tim2, tim1);
cnt += dif / MIN_PERIOD;
if (cnt > MAX_CNT) cnt = MAX_CNT;
cnt--;
if (dif < MIN_PERIOD) {
printf("Status=%d. Died after %d sec", status, dif);
} else {
printf("Status=%d. Died after %f hours", status, dif/3600.);
}
if (cnt > 0) {
printf(" - %d tries left - retry ...\n", cnt);
}
}
printf(" - too many restarts\n");
return -1;
}