For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Re: Bidirection comunication wtih ppipes- doesn´t work ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Bidirection comunication wtih ppipes- doesn´t work ?
Andrew Wansink

2005-03-25, 8:56 am

On Thu, 24 Mar 2005 11:29:04 +0000, José Pedro Silva Pinto wrote:

> Hi,
>
> I want make a bi-directional communication between two processes, using pipes.
>
> To make it possible, I create 2 pipes, on for on way ( pipe(LEI_1,WRITE_1) ) an another to opposite way (pipe(READ_2,WRITE_2);).
>
> When I run my program, the process stops on PROCESS 2 END, if I comment the line $buf = <LEI_1>;, it runs well
>


You seem to be doing a blocking read on LEI_1 in the parent but
none of the child processes ever writes to LEI_1. That is why
your process blocks.

You might do well to read the section on "socketpair" in the
perlipc manual (perldoc peripc.) socketpair is an effective
way to get a pair of bidirectional handles to a pipe.

andy


> Code (run well )
>
> #!/opt/perl5/bin/perl5.6.1
>
> $SIG{'CHLD'} = 'IGNORE';
> pipe(LEI_1,WRITE_1);
> pipe(READ_2,WRITE_2);
> if ($pid = fork)
> {
> close LEI_1;
> close WRITE_2;
> print "PROCESS 1 \n";
> printf WRITE_1 "Hello father:";
> $pai = <READ_2>;
> print "Message from father: $pai \n ";
> sleep 2;
> print "PROCESS 1 END \n";
> exit(0);
> }
> if ($pid = fork)
> {
> print "PROCESS 2 \n";
> sleep 1;
> print "PROCESS 2 END \n";
> exit(0);
> }
> close WRITE_1;
> close READ_2;
> #$buf = <LEI_1>;
> print "message from son : $buf \n";
> printf WRITE_2 "Hello Son";
> exit(0);
>
> ****************************************
******************************
>
> OUTPUT:
>
> PROCESS 1
> PROCESS 2
> message from son :
> PROCESS 2 END
> Message from father: Hello Son
> PROCESS 1 END
>
>
> ****************************************
******************************
>
>
>
> CODE (NOT RUN) (With $buf = <LEI_1>; uncommented)
>
> #!/opt/perl5/bin/perl5.6.1
>
> $SIG{'CHLD'} = 'IGNORE';
> pipe(LEI_1,WRITE_1);
> pipe(READ_2,WRITE_2);
> if ($pid = fork)
> {
> close LEI_1;
> close WRITE_2;
> print "PROCESS 1 \n";
> printf WRITE_1 "Hello father:";
> $pai = <READ_2>;
> print "Message from father: $pai \n ";
> sleep 2;
> print "PROCESS 1 END \n";
> exit(0);
> }
> if ($pid = fork)
> {
> print "PROCESS 2 \n";
> sleep 1;
> print "PROCESS 2 END \n";
> exit(0);
> }
> close WRITE_1;
> close READ_2;
> #$buf = <LEI_1>;
> print "message from son : $buf \n";
> printf WRITE_2 "Hello Son";
> exit(0);
>
> ****************************************
************
>
> OUTPUT:
>
> PROCESS 1
> PROCESS 2
> PROCESS 2 END
>
> ****************************************
************
>
>
> So, it's very strange, so I can not have two pipes?, what's the problem.
>
>
> Thanks


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com