Reputation: 19572
I found some code that 2 processes in Perl can communicate via a pipe. Example:
if ($pid = fork) {
close $reader;
print $writer "Parent Pid $$ is sending this\n";
close $writer;
waitpid($pid,0);
}
else {
close $writer;
chomp($line = <$reader>);
print "Child Pid $$ just read this: `$line'\n";
close $reader;
exit;
}
Now I have the following questions:
I am sorry if the questions are too basic but my experience is with threads in another language.
Upvotes: 2
Views: 238
Reputation: 118605
With some important caveats(*
), I/O to a pipe in Perl is a lot like I/O to any other filehandle.
The readline
(<>
) operator will wait for input on a pipe just like it will from a socket or STDIN
. When you close
the write end of the pipe, the read end will receive end of file (and readline
will return undef
). I can demonstrate these concepts with some small modifications to your script:
pipe $reader, $writer;
if ($pid = fork) {
close $reader;
sleep 5;
for (1..10) {
print $writer "Parent Pid $$ is sending this\n";
}
close $writer;
waitpid($pid,0);
}
else {
close $writer;
# <$reader> will block until parent produces something
# and will return undef when parent closes the write end of the pipe
while ($line = <$reader>) {
chomp($line);
print "Child Pid $$ just read this: `$line'\n";
}
close $reader;
exit;
}
3 . There is usually an operating-system-imposed limit on the number of open filehandles in a process, and open pipe handles count against this value, but 10 or 20 pipes would not be a problem.
*
one important caveat is the small buffer size that pipes have, restrictingly small on some OS. If you fill this buffer, the write end of the pipe can block on a write operation until the read end takes something out of the buffer. If you don't manage your reads and writes carefully, your program could deadlock.
Upvotes: 3