From 25e136abd74f4b8d5eb5a69ec641eb5d757c91ce Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 15 Jul 2012 03:14:56 -0700 Subject: working, cleaner now --- tools/hi-q.c | 66 ++++++++++++++++++++++++++++----------------------------- tools/hi-test.c | 2 +- 2 files changed, 34 insertions(+), 34 deletions(-) (limited to 'tools') diff --git a/tools/hi-q.c b/tools/hi-q.c index f2da44d..6d73b9b 100644 --- a/tools/hi-q.c +++ b/tools/hi-q.c @@ -217,7 +217,7 @@ int main(int argc, char** argv, char const * const * env) { if (pipe(resync) != 0) cerr << "resync pipe failed" << endl; // At this point, there are some loop invariants; -// (a) fd0 is open (standard input) and has the email msg, +// (a) fd0 is open (standard input) and has the email msg, // ready for the next child to read, and // (b) fd1 is open (nonstandard input) and has envelope information. // We need it to be open, so that pipe() @@ -267,35 +267,40 @@ int main(int argc, char** argv, char const * const * env) { // equal to pid of kid#0 if (ii) kidgroup = kidpid[0]; if (setpgid(0, kidgroup) != 0) { - cerr << "*** kid " << ii + cerr << "*** kid " << ii << " setpgid failed! " << errno << " ... "; perror(0); } else { - cerr << "*** kid " << ii << " setpgid OK" << endl; + // cerr << "*** kid " << ii << " setpgid OK" << endl; } -// everybody else has to wait for us to get this far +// ... everybody else has to wait for us to get this far ... +// ... so that the new process group will be valid ... +// Write-a-byte synchronization is released when the *first* guy writes. if (ii == 0) { int junk(1); write(sync[wEnd], &junk, 1); - cerr << "sync sent" << endl; + //cerr << "sync sent" << endl; } -#if 1 - cerr << "kid [" << ii << "] " << getpid() +#if 0 + cerr << "kid [" << ii << "] " << getpid() << " kidpid[0]: " << kidpid[0] << " pgid: " << getpgid(0) << " starts" << endl; #endif + close(resync[wEnd]); -// Now we wait for everybody else: +// ... now we must wait for everybody else, because ... +// ... if we do the exec(), the new process group becomes invalid ... +// Close synchronization is released when the *last* guy closes. if (ii==0) { int junk; - cerr << "about to read resync" << endl; + //cerr << "about to read resync" << endl; ssize_t rslt = read(resync[rEnd], &junk, 1); - if (rslt != 1) { - cerr << "bad sync ... 1 != " << rslt << endl; + if (rslt < 0 ) { + cerr << "bad sync ... " << rslt << endl; } else { - cerr << "back from read resync, good: " << rslt << endl; + // cerr << "back from read resync, good: " << rslt << endl; } } @@ -318,7 +323,7 @@ int main(int argc, char** argv, char const * const * env) { } close(kid_end); // use fd1 instead now - // OK, at this point this kid is set up to read fd0 and write fd1 + // OK, at this point this kid is set up to read fd0 and write fd1 // (except last kid reads fd1 as well as fd0). //// probe_fd(); @@ -343,25 +348,19 @@ int main(int argc, char** argv, char const * const * env) { close(kid_end); // Let kid #0 run a little ways: - if (ii==0) { - int junk; - cerr << "about to read sync" << endl; - ssize_t rslt = read(sync[rEnd], &junk, 1); - if (rslt < 0) { - cerr << "bad sync ... 1 != " << rslt << endl; - } else { - cerr << "back from read sync, good: " << rslt << endl; - } + if (ii==0) { + int junk; + //cerr << "about to read sync" << endl; + ssize_t rslt = read(sync[rEnd], &junk, 1); + if (rslt != 1) { + cerr << "bad sync ... 1 != " << rslt << endl; + } else { + //cerr << "back from read sync, good: " << rslt << endl; } - -// 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 (ii == 0) usleep(1); + } #if 0 - cerr << "apparent kid #" << ii + cerr << "apparent kid #" << ii << " (" << kidpid[ii] << ") " << endl; #endif @@ -369,7 +368,8 @@ int main(int argc, char** argv, char const * const * env) { // here with the whole pipeline of kids launched - close(resync[wEnd]); + close(resync[wEnd]); + close(resync[rEnd]); close(0); // the reading end of stdin was // delegated to the first child @@ -384,13 +384,13 @@ int main(int argc, char** argv, char const * const * env) { if (WIFEXITED(kidstatus)) { int sts = WEXITSTATUS(kidstatus); if (sts == 1) { - cerr << "hi-q says: kid " << ii + cerr << "hi-q says: kid " << ii << " i.e. '" << filter[ii][0] << "' reports spam." << endl; panic(ex_spam); } if (sts != 0) { - cerr << "hi-q says: kid " << ii + cerr << "hi-q says: kid " << ii << " i.e. '" << filter[ii][0] << "'" << " suffered an error: " << sts << endl; @@ -433,6 +433,6 @@ fprintf(stderr, "About to wait for kid #%d (%d)\n", sleep(1); #endif // the last kid did not exit but was killed: - return ex_syserr; + return ex_syserr; } diff --git a/tools/hi-test.c b/tools/hi-test.c index 01e475b..3086535 100644 --- a/tools/hi-test.c +++ b/tools/hi-test.c @@ -3,5 +3,5 @@ using namespace std; int main(){ - cerr << "++++ " << getpid() << " : " << getpgid(0) << endl; + cerr << "++++ hi-test pid: " << getpid() << " group: " << getpgid(0) << endl; } -- cgit v1.2.3