Jim
Jim

Reputation: 19572

How can I loop over data from a pipe?

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.

Upvotes: 2

Views: 238

Answers (1)

mob
mob

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

Related Questions