From fb6042c5cd8a411cfcfcf6c1a1d466eb42a4ff33 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sat, 14 Jul 2012 23:13:38 -0700 Subject: working toward a process-group signalling system --- tools/hi-q.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'tools/hi-q.c') diff --git a/tools/hi-q.c b/tools/hi-q.c index a065ad0..14ca5ec 100644 --- a/tools/hi-q.c +++ b/tools/hi-q.c @@ -197,17 +197,16 @@ int main(int argc, char** argv, char const * const * env) { } if (job.size()) filter.push_back(job); } - if (verbose) for (vector::const_iterator job = filter.begin(); - job != filter.end(); job++) { - cerr << "Filter: "; - for (VS::const_iterator token = job->begin(); - token != job->end(); token++){ + unsigned int nkids = filter.size(); + if (verbose) for (unsigned int ii = 0; ii < nkids; ii++) { + cerr << "hi-q filter[" << ii << "] :; "; + for (VS::const_iterator token = filter[ii].begin(); + token != filter[ii].end(); token++){ cerr << *token << " "; } cerr << endl; } - int nkids = filter.size(); vector kidpid(nkids); // indexed by kid number // At this point, there are some loop invariants; @@ -218,7 +217,7 @@ int main(int argc, char** argv, char const * const * env) { // doesn't choose it. That allows N-1 of the kids // to close it and dup() something useful onto it. - for (int ii=0; ii < nkids; ii++){ /* loop over all kids */ + for (unsigned int ii=0; ii < nkids; ii++){ /* loop over all kids */ int datapipe[2]; int kid_end; @@ -256,6 +255,16 @@ int main(int argc, char** argv, char const * const * env) { kidpid[ii] = fork(); if (!kidpid[ii]) { /*** child code ***/ + pid_t kidgroup(0); // process group for all kids is + // equal to pid of kid#0 + if (ii) kidgroup = kidpid[0]; + if (setpgid(0, kidgroup) != 0) { + cerr << "setpgid failed! " << errno << " ... "; + perror(0); + } + cerr << "kid [" << ii << "] starts: " << getpid() + << " kidpid[0] " << kidpid[0] + << endl; // Now that we are through creating pipes, we don't // need to continue blocking fd1: close(1); @@ -295,10 +304,14 @@ int main(int argc, char** argv, char const * const * env) { panic(ex_syserr); } close(kid_end); -#ifdef more_testing - fprintf(stderr, "forked kid #%d (%d) piping %d\n", - ii, kidpid[ii], kid_end); -// sleep(1); /* let kid run a while */ +// it is important to let kid#0 run a while; +// otherwise it won't fully exist when the other kids start, +// and depending race conditions, the setpgid could fail + if (1) usleep(0); +#if 1 + cerr << "forked kid #" << ii + << " (" << kidpid[ii] << ") " + << endl; #endif } @@ -307,7 +320,7 @@ int main(int argc, char** argv, char const * const * env) { close(0); // the reading end of stdin was // delegated to the first child - for (int ii=0; ii