2

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:

  1. Is it possible to make the reader read from the pipe, and then block until new data come from the pipe like in a loop?
  2. If yes what is the way to kill the child process when the parent process has no data to send?
  3. Is there a limitation on how many open read/write pipes I have per program? E.g. if I fork 10 processes and have 20 pipes (10 read/10 write) is it a bad idea?

I am sorry if the questions are too basic but my experience is with threads in another language.

Miller
  • 34,962
  • 4
  • 39
  • 60
Jim
  • 18,826
  • 34
  • 135
  • 254

1 Answers1

3

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.

Community
  • 1
  • 1
mob
  • 117,087
  • 18
  • 149
  • 283
  • How small a buffer per pipe are we talking about?Few bytes?To pass just a few primitive values?So using some form of shared memory is the preferred way instead? – Jim Apr 10 '14 at 22:06
  • 64K is typical, but it could be smaller. – mob Apr 10 '14 at 22:44
  • So how would parent child usually talk in a perl code?Via shared memory is the recommended way? – Jim Apr 11 '14 at 07:13