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.c37
1 files changed, 25 insertions, 12 deletions
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<VS>::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<pid_t> 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<nkids-1; ii++){ /* loop over N-1 kids */
+ for (unsigned int ii=0; ii<nkids-1; ii++){ /* loop over N-1 kids */
/* not last kid */
#ifdef testing
blurb(ii, kidpid);