| 5 |
|
* General component matrix combiner, operating on a row at a time. |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
+ |
#include <signal.h> |
| 9 |
|
#include <math.h> |
| 10 |
|
#include "platform.h" |
| 11 |
|
#include "rtprocess.h" |
| 57 |
|
SUBPROC *cproc = NULL; /* child process array */ |
| 58 |
|
int nchildren = 0; /* # of child processes */ |
| 59 |
|
int inchild = -1; /* our child ID (-1: parent) */ |
| 60 |
+ |
int gpid = -1; /* group process ID (parent) */ |
| 61 |
+ |
int nr_out = 0; /* # of rows output by kids */ |
| 62 |
|
|
| 63 |
|
static int checksymbolic(ROPMAT *rop); |
| 64 |
|
|
| 65 |
+ |
static void |
| 66 |
+ |
on_sigio(int dummy) |
| 67 |
+ |
{ |
| 68 |
+ |
nr_out++; /* happens when child outputs row */ |
| 69 |
+ |
signal(SIGIO, on_sigio); /* reset to maximize portability */ |
| 70 |
+ |
} |
| 71 |
+ |
|
| 72 |
|
static int |
| 73 |
|
split_input(ROPMAT *rop) |
| 74 |
|
{ |
| 596 |
|
#endif |
| 597 |
|
return(0); |
| 598 |
|
} |
| 599 |
+ |
gpid = setpgrp(); /* set group process ID */ |
| 600 |
+ |
signal(SIGIO, on_sigio); |
| 601 |
|
fflush(stdout); /* flush header & spawn children */ |
| 602 |
|
cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np); |
| 603 |
|
if (!cproc) |
| 617 |
|
perror("fork"); |
| 618 |
|
exit(1); |
| 619 |
|
} |
| 620 |
< |
inchild = i; /* child index */ |
| 620 |
> |
inchild = i; /* our child index */ |
| 621 |
> |
while (i-- > 0) /* don't share siblings' pipes */ |
| 622 |
> |
close(cproc[i].w); |
| 623 |
|
fpurge(stdin); /* discard previous matrix input */ |
| 624 |
|
#ifdef getc_unlocked |
| 625 |
|
flockfile(stdin); |
| 626 |
|
#endif |
| 627 |
|
for (i = 0; i < nmats; i++) { |
| 628 |
|
if (mop[i].infp != stdin) |
| 629 |
< |
fclose(mop[i].infp); |
| 629 |
> |
fclose(mop[i].infp); /* ! pclose() */ |
| 630 |
|
mop[i].infp = stdin; |
| 631 |
|
mop[i].imx.dtype = DTdouble; |
| 632 |
|
} |
| 656 |
|
#endif |
| 657 |
|
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
| 658 |
|
FILE *ofp = outfp[cur_row % nchildren]; |
| 659 |
< |
for (i = 0; i < nmats; i++) { |
| 659 |
> |
for (i = 0; i < nmats; i++) |
| 660 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 661 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 662 |
|
break; |
| 664 |
|
mop[i].inspec, cur_row); |
| 665 |
|
return(0); |
| 666 |
|
} |
| 653 |
– |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
| 654 |
– |
mop[i].imx.ncols, DTdouble, ofp)) |
| 655 |
– |
return(0); |
| 656 |
– |
} |
| 667 |
|
if (i < nmats) |
| 668 |
|
break; |
| 669 |
+ |
for (i = 0; i < nmats; i++) |
| 670 |
+ |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
| 671 |
+ |
mop[i].imx.ncols, DTdouble, ofp)) |
| 672 |
+ |
return(0); |
| 673 |
|
if (fflush(ofp) == EOF) |
| 674 |
|
return(0); |
| 675 |
|
} |
| 772 |
|
return(0); |
| 773 |
|
} |
| 774 |
|
rmx_free(mres); mres = NULL; |
| 775 |
< |
if (inchild >= 0) { |
| 776 |
< |
i = getc(stdin); /* child waits for turn to output */ |
| 777 |
< |
if (i != EOF) ungetc(i, stdin); |
| 764 |
< |
} |
| 775 |
> |
if (inchild >= 0) /* children share stdout */ |
| 776 |
> |
while (nr_out < cur_row) |
| 777 |
> |
pause(); /* wait for our turn */ |
| 778 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
| 779 |
|
res->rmp->ncols, res->rmp->dtype, stdout)) |
| 780 |
|
return(0); |
| 781 |
< |
if (inchild >= 0 && fflush(stdout) == EOF) |
| 782 |
< |
return(0); |
| 781 |
> |
if (inchild >= 0) { /* flush and notify group */ |
| 782 |
> |
if (fflush(stdout) == EOF) |
| 783 |
> |
return(0); |
| 784 |
> |
killpg(gpid, SIGIO); /* increments everyone's nr_out */ |
| 785 |
> |
} |
| 786 |
|
} |
| 787 |
|
return(inchild >= 0 || fflush(stdout) != EOF); |
| 788 |
|
memerror: |
| 1005 |
|
return(1); |
| 1006 |
|
} |
| 1007 |
|
doptimize(1); /* optimize definitions */ |
| 1008 |
< |
if (spawned_children(nproc)) /* running in children? */ |
| 1008 |
> |
if (spawned_children(nproc)) /* running in parent process? */ |
| 1009 |
|
return(parent_loop() ? 0 : 1); |
| 1010 |
|
/* process & write rows */ |
| 1011 |
|
return(combine_input() ? 0 : 1); |