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 | |
|
Re: Creating a Pipe
by marioroy (Prior) on Feb 17, 2016 at 01:28 UTC | |
|
Re: Creating a Pipe
by Anonymous Monk on Feb 17, 2016 at 01:14 UTC |