summaryrefslogtreecommitdiff
path: root/tools/hi-q.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/hi-q.c')
-rw-r--r--tools/hi-q.c39
1 files changed, 31 insertions, 8 deletions
diff --git a/tools/hi-q.c b/tools/hi-q.c
index 1e71ef4..3de9f1c 100644
--- a/tools/hi-q.c
+++ b/tools/hi-q.c
@@ -209,6 +209,11 @@ int main(int argc, char** argv, char const * const * env) {
vector<pid_t> kidpid(nkids); // indexed by kid number
+ const int rEnd(0); // end of a pipe for reading
+ const int wEnd(1); // end of a pipe for writing
+ int sync[2];
+ if (pipe(sync) != 0) cerr << "sync pipe failed" << endl;
+
// At this point, there are some loop invariants;
// (a) fd0 is open (standard input) and has the email msg,
// ready for the next child to read, and
@@ -247,10 +252,11 @@ int main(int argc, char** argv, char const * const * env) {
// It will be written by this kid and read by the next kid.
// For the last kid, the loose end connects to hi-q.
// It will be written by hi-q and read by the last kid.
+
int lastkid = (ii == nkids-1);
#define flip(a,b) (lastkid ? b : a)
- loose_end = datapipe[flip(0,1)];
- kid_end = datapipe[flip(1,0)];
+ loose_end = datapipe[flip(rEnd, wEnd)];
+ kid_end = datapipe[flip(wEnd, rEnd)];
kidpid[ii] = fork();
if (!kidpid[ii]) { /*** child code ***/
@@ -262,9 +268,17 @@ int main(int argc, char** argv, char const * const * env) {
cerr << "setpgid failed! " << errno << " ... ";
perror(0);
}
- cerr << "kid [" << ii << "] starts: " << getpid()
+ if (ii == 0) {
+ int junk(1);
+ write(sync[wEnd], &junk, 1);
+ //cerr << "sync sent" << endl;
+ }
+
+#if 1
+ cerr << "kid [" << ii << "] " << getpid()
<< " kidpid[0] " << kidpid[0]
- << endl;
+ << " starts" << endl;
+#endif
// Now that we are through creating pipes, we don't
// need to continue blocking fd1:
close(1);
@@ -308,15 +322,24 @@ int main(int argc, char** argv, char const * const * env) {
// 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 1
- cerr << "forked kid #" << ii
+///////??? if (ii == 0) usleep(1);
+
+ if (ii==0) {
+ int junk;
+ //cerr << "about to read sync" << endl;
+ ssize_t rslt = read(sync[rEnd], &junk, 1);
+ //cerr << "back from read sync: " << rslt << endl;
+ if (rslt) {}
+ }
+
+#if 0
+ cerr << "apparent kid #" << ii
<< " (" << kidpid[ii] << ") "
<< endl;
#endif
} /* end loop starting all kids */
-// here with the whole pipeline of kids running
+// here with the whole pipeline of kids launched
close(0); // the reading end of stdin was
// delegated to the first child