I closed the file descriptors immediately when they were not needed any more. I wanted to send only one string, then I closed the writer[1]. But you can keep it open until you're done (note: the following code is "high-level"):else { /* In the parent */ /* close unneeded endpoints */ close(reader[1]); close(writer[0]); /* Send something to the filter */ write(writer[1], msg, strlen(msg)); close(writer[1]); /* HERE I CLOSE PARENT->FILTER */ /* Read response from the filter */ printf("filter says:\n"); while ((nread = read(reader[0], buf, 9)) > 0) { buf[nread] = '\x0'; printf("%s", buf); } if (nread < 0) { perror("read(): "); return 1; } printf("\n"); close(reader[0]); /* HERE I CLOSE FILTER->PARENT */ }
A little note is due here about read_and_consume: be very, very careful about synchronisation between the two processes. You probably want to use the filter in a line-oriented fashion (from what I see from your post): be careful that read() blocks! Otherwise, you could prepare all the input (i.e. all the lines that you have to filter), send them all to the filter at once and read the answers. But I'll cut it here before someone reminds me that we're in PerlMonks, not CMonks ;)else { /* In the parent */ /* close unneeded endpoints */ close(reader[1]); close(writer[0]); while (msg = next_string_to_filter()) { /* Send something to the filter */ write(writer[1], msg, strlen(msg)); /* Read response from the filter */ read_and_consume_answer(reader[0]); } /* Close communication with filter */ close(writer[1]); close(reader[0]); }
Any recommendations on where to RTFM about agnostic filters in perl?With agnostic I tried to express the fact that the filter does not have to know anything more than "read from STDIN, write to STDOUT", which is what any program normally does. I fear that you aren't going to find much about "agnostic filters" in any language :)
More than this, be careful of buffering, use strict; and use warnings; ;)
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
In reply to Re^3: How to hook up C's socketpair() with Perl's?
by polettix
in thread How to hook up C's socketpair() with Perl's?
by mhutch7714
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |