blob: 380be52a085b7ba642f1553710ef2c6bc9c90595 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <unistd.h>
#include <stdio.h>
#include <time.h>
const useconds_t
minSleepTime = 1,
maxSleepTime = 1000000;
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "supply a program name\n");
return -1;
}
useconds_t sleepTime = minSleepTime;
time_t td = time(NULL);
while (1) {
pid_t pid = fork();
switch (pid) {
case 0: // child
execvp(argv[1], &(argv[1]));
return -1; // exec failed, stop
case -1: // fork failed, assume temporary failure
break;
default: // parent
wait(NULL);
}
fprintf(stderr, "child exit, restarting\n");
// apply current and calc new delay
td = time(NULL) - td;
if (td > 2) {
sleepTime = minSleepTime;
}
usleep(sleepTime);
if (td <= 2) {
sleepTime *= 2;
if (sleepTime > maxSleepTime)
sleepTime = maxSleepTime;
}
td = time(NULL);
}
}
|