in reply to Creating a Pipe

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