grjoe21 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, i have an assignment for my perl class which is the following:

Create a pipe
Fork a sub-process
Parent gets a message from the user, sends it to the child
Child gets the message, prints it to the screen
Repeat until user doesn't enter a message

This is what I have so far and cant seem to get it to work properly, please help me with this simple code!

 

pipe(PIPE_READ,PIPE_WRITE); autoflush PIPE_WRITE 1; my $pid = fork(); if ($pid = fork) { &write_pipe ($pid); waitpid($pid,0); } elsif (defined $pid) { &read_pipe; } else { die "cannot fork: $!"; } sub write_pipe { print "pid $$ \n"; print "Enter message: "; sleep 1; my $usr_msg = <>; print "Parent pid = $$ message = $usr_msg"; print PIPE_WRITE "$usr_msg\n"; } sub read_pipe { print "child pid = $pid"; my $msg_read = <PIPE_READ>; print "received from pipe $msg_read"; }

Replies are listed 'Best First'.
Re: Creating a Pipe
by shmem (Chancellor) on Feb 17, 2016 at 08:41 UTC

    Several issues.

    1. you are doing 2 forks.
    2. you confounded parent and child code. The child writes to, the parent reads from the pipe.
      if ($pid = fork) { # parent ... } elsif ($pid == 0) { child ... exit; }
    3. don't forget to exit the child in the elsif branch to stop it executing any following code of the parent.
    4. you need a signal handler in the parent for it to get notified about the child having exited.
      Otherwise the parent will try to get a line from its pipe handle when there isn't any.

    Last, you need loops in the parent and the child functions for the repeating to happen.

    Try to fix your program first, then read the spoiler ;-)

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Creating a Pipe
by marioroy (Prior) on Feb 17, 2016 at 01:28 UTC

    Update: I misread the assignment. A single pipe-pair is all that's needed for the assignment, not two. The assignment asks for message(s) to come from the user, likely from STDIN. The demonstration is left intact and not changed in order to demonstrate bi-directional communication via pipes.

    Greetings grjoe21, and welcome to the monastery.

    A pipe is uni-directional whereas a socket is bi-directional. Therefore, the demonstration below requires two pipe pairs for bi-directional communication between the parent and child processes.

    #!/usr/bin/env perl use strict; use warnings; # Also, see Interactive Client with IO::Socket for another # demonstration at: http://perldoc.perl.org/perlipc.html pipe( my $p_rdr, my $c_wtr ); pipe( my $c_rdr, my $p_wtr ); autoflush $c_wtr, 1; autoflush $p_wtr, 1; my $pid = fork() // die "can't fork: $!"; if ( $pid ) { # parent process my $reply; while ( defined ( $reply = <$p_rdr> ) ) { last if ( length $reply == 1 ); print $p_wtr "Hello, $reply"; } waitpid $pid, 0; } else { # child process my $reply; my @names = qw/ Sun Moon Wind Air /; for my $name ( @names ) { print $c_wtr "$name\n"; $reply = <$c_rdr>; print $reply; } print $c_wtr "\n"; exit 0; }

    Output.

    Hello, Sun Hello, Moon Hello, Wind Hello, Air

    Regards, Mario

Re: Creating a Pipe
by Anonymous Monk on Feb 17, 2016 at 01:14 UTC

    Did teacher mention perlipc?

    Add more error checking