diff options
author | Marius Halden <marius.h@lden.org> | 2015-10-29 03:09:54 +0100 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2015-10-29 03:09:54 +0100 |
commit | 4058c3af39ea0bc93b2f98d5ef37a89a85fb96ee (patch) | |
tree | f3c063a0a81699640066ef10e684c9ee22d028f7 | |
parent | 2a5ecde87eca3d25d945608e3f37c5a47a6bd523 (diff) | |
download | piper-4058c3af39ea0bc93b2f98d5ef37a89a85fb96ee.tar.gz piper-4058c3af39ea0bc93b2f98d5ef37a89a85fb96ee.tar.bz2 piper-4058c3af39ea0bc93b2f98d5ef37a89a85fb96ee.tar.xz |
Add some basic backoff when restarting programs
-rw-r--r-- | piper.c | 36 |
1 files changed, 31 insertions, 5 deletions
@@ -5,12 +5,12 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/time.h> #include <sys/types.h> #include <sys/wait.h> +#include <time.h> #include <unistd.h> -//#define NUM_PROCS 2 - struct proc { pid_t pid; char *cmd; @@ -18,6 +18,9 @@ struct proc { int _stdin; int _stdout; int _stderr; + time_t last_started; + time_t next_start; + int num_starts; } *procs; int num_procs = 0; @@ -126,6 +129,13 @@ run_cmd(struct proc *proc) { pid_t cpid; + // XXX: This REALLY needs to be done better + time_t t = time(NULL); + if (t < procs->next_start) + sleep(procs->next_start - t); + + procs->last_started = time(NULL); + cpid = fork(); if (cpid == 0) { if (proc->_stdin != -1) { @@ -260,6 +270,12 @@ print_argv() } int +have_nonstarted() +{ + return 0; +} + +int main(int argc, char **argv) { int i; @@ -283,6 +299,9 @@ main(int argc, char **argv) procs[i]._stdin = -1; procs[i]._stdout = -1; procs[i]._stderr = -1; + procs[i].last_started = 0; + procs[i].next_start = 0; + procs[i].num_starts = 0; gen_argv(&procs[i]); } @@ -308,11 +327,11 @@ main(int argc, char **argv) if (sendsignal != 0) signal_procs(); - pid = wait(NULL); + pid = waitpid(0, NULL, 0); if (pid == -1) { - if (errno == EINTR) + if (errno == EINTR) { continue; - else + } else fail(1, "wait"); } @@ -320,6 +339,13 @@ main(int argc, char **argv) if (p == NULL) continue; // XXX: Log something here? p->pid = -1; + + time_t t = time(NULL); + if (p->last_started >= t - (p->num_starts + 1)) { + p->next_start = t + p->num_starts; + p->num_starts++; + } else + p->num_starts = 0; } return 0; |