LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2011, 03:10 PM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
perl fork & pipe help


Hi,
I am learning about forks and pipes in perl,and i seem to be having a small problem in communicating between a child and a parent process using pipes.

Code:
#!/bin/perl
use strict;
use warnings;

my $pid;
my $result;
pipe (READ,WRITE);
$pid=fork();
if ($pid == 0 ) {
#child process

$result=`tail -10 /var/log/messages`;
print WRITE ($result);
exit(1);
}
else {
#parent process
my $stat=<READ>;
print "Printed by parent ($$) - $stat\n";
waitpid($pid,0);
}
Output:

Code:
Printed by parent (3623) - Apr  9 00:39:30 hp-laptop kernel: [   23.450059] EXT4-fs (sda5): re-mounted. Opts: errors=remount-ro,user_xattr,commit=600
For some reason the parent process is printing only the first line returned by the child process through a pipe,can any one tell me where am i going wrong over here? Thanks.
 
Old 04-08-2011, 05:29 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
When you assign to a scalar, a read returns one record, which is by default, a line. Perhaps you should make "$stat" into "@stat".
--- rod.
 
Old 04-09-2011, 01:19 AM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Hi,

I had tried making $stat to @stat,the programme just hangs,doesnt give any output when changing the scalar to an array.
 
Old 04-09-2011, 04:12 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by kdelover View Post
Hi,

I had tried making $stat to @stat,the programme just hangs,doesnt give any output when changing the scalar to an array.
You have to start from something like this:

Code:
while(defined(my $line = <READ>))
  {
  warn "parent: \$line=$line";
  }
 
Old 04-09-2011, 06:20 AM   #5
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Hi,

The while loop actually works,but the programme doesn't quit now after printing those 10 lines.I think the pipe is expecting an EOF character,which it never gets.
 
Old 04-09-2011, 08:42 AM   #6
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
I solved the problem by using a named pipe,i don't know why cant i use a pipe(read,write) function.

Quote:
#parent process
open (FIFO,'<','./mypipe');
my @arr=<FIFO>;
close (FIFO);
print "@arr\n";
waitpid ($pid,0);

}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pipe and fork Trouble ajb181 Linux - Embedded & Single-board computer 2 10-01-2009 09:04 PM
Sockets, pthread, pipe, and fork -- a messaging server ggravarr Programming 3 06-28-2005 02:39 PM
how to use fork/pipe to decrement a global varialbe Y and exit when Y is 0 keiwu Programming 1 02-19-2005 11:33 PM
Wierd behaviour using fork() and pipe() cybermix Programming 4 01-08-2004 03:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration