in reply to Re: Reading from a pipe
in thread Reading from a pipe

Thanks it works,Lets say if i fork 3-4 child processes and all of them write into the pipe,and on the parent side can i read at a time what all child processes wrote by doing a  @read_from_children = <$read>. Looks it doesn't work.I still have to do a loop and read.

Replies are listed 'Best First'.
Re^3: Reading from a pipe
by Eliya (Vicar) on Nov 20, 2011 at 14:18 UTC

    The problem is that the readline blocks until it has read a line (irrespective of whether you use it in list or scalar context). So despite your non-blocking waitpid(-1,WUNTRACED) you won't start a new child before the old one has written a line to the pipe. In other words, as you have it, your pings will still run sequentially (just as they would without the additional fork/wait wrapped around the backticks call).

    If you want to run several child processes in parallel, you'd have to have independent pipes, and (for example) read from them in a select loop when they're ready (the other form of select, that is, not the one you're already using in your code).

      Thanks for the explanation Eliya. Basically,i am writing a script which forks multiple processes which ping different hosts for 40-50 sec and report to the parent script with values. I'd like to do the pinging part in parallel,not sequentially. So,if i have a 10 child ping processes,do i need to create 10 different pipes ( hope i am getting this right).Lastly,what sort of IPC is better in such cases.
      Thanks for the explanation Eliya. Basically,i am writing a script which forks multiple processes which ping different hosts for 40-50 sec and report to the parent script with values. I'd like to do the pinging part in parallel,not sequentially. So,if i have a 10 child ping processes,do i need to create 10 different pipes ( hope i am getting this right).Lastly,what sort of IPC is better in such cases.