How system() & output works

M

mingclee1

Hello,

I am running into a weird problem:

environment: unix solaris 9

I am running the following perl from command line:

test.pl:
#!/usr/local/bin/perl
use strict;
print "begin process";
system("ming2.pl");
print "after calling the processn";

ming2.pl:
#!/usr/local/bin/perl
use strict;
print "in the child process 1";
sleep(5);
print "in the child process 2";


The output is:
in the child process 1 | in the child process 2 | begin process | after
calling the process

I used | to make the output easier to read, they don't really exist in
the output.

It seemed to have executed the child process before anything in the
parent process has been executed.

However if I added \n to print "begin process": print "begin process\n"
in test.pl, then the output:
begin process
in the child process 1in the child process 2after calling the process

which shows that the parent process waits for the child process to
return. This should be the right behavior for system() command.

So, why the weird behavior? Any advise is highly appreciated!

Thanks
 
B

Brian McCauley

I am running into a weird problem:

[ snip output buffering problem ]

See FAQ: "How do I flush/unbuffer an output filehandle? Why must I do
this?"
 
J

John W. Krahn

Hello,

I am running into a weird problem:

environment: unix solaris 9

I am running the following perl from command line:


test.pl:
#!/usr/local/bin/perl
use strict;
print "begin process";
system("ming2.pl");
print "after calling the processn";

ming2.pl:
#!/usr/local/bin/perl
use strict;
print "in the child process 1";
sleep(5);
print "in the child process 2";


The output is:
in the child process 1 | in the child process 2 | begin process | after
calling the process

I used | to make the output easier to read, they don't really exist in
the output.

It seemed to have executed the child process before anything in the
parent process has been executed.

However if I added \n to print "begin process": print "begin process\n"
in test.pl, then the output:
begin process
in the child process 1in the child process 2after calling the process

which shows that the parent process waits for the child process to
return. This should be the right behavior for system() command.

So, why the weird behavior? Any advise is highly appreciated!

perldoc -q "How do I flush/unbuffer an output filehandle"



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top