summaryrefslogtreecommitdiffstats
path: root/piper.c
diff options
context:
space:
mode:
Diffstat (limited to 'piper.c')
-rw-r--r--piper.c36
1 files changed, 31 insertions, 5 deletions
diff --git a/piper.c b/piper.c
index be3d677..62f1b9c 100644
--- a/piper.c
+++ b/piper.c
@@ -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;