473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pipe problem, Is it a bug in Perl

Hello everyone,

I have a C program that constantly returned the % of the job completed to my Perl script so to execute this program I have these lines:

Expand|Select|Wrap|Line Numbers
  1. use IO::Handle;
  2. $| = 1;
  3.  
  4. $data1 = "./myC.exe 10 10 1 abc";
  5.  
  6. open PS, "$data1 |";
  7. PS->autoflush(1);
  8. while ($line = readline(*PS)) {
  9.     print $line;
  10.     print "\n";
  11. }
  12.  
I have noticed that the results are only returned to my Perl script after my C program is completed and not while it is running. The C program usually takes about 10 hours to complete and every half an hour or so it prints to STDOUT how many percent of the job has been completed so that the Perl script can take it and push it to the web page. I have tried

Expand|Select|Wrap|Line Numbers
  1. use IO::Handle;
  2. $| = 1;
  3.  
  4. $data1 = "./myC.exe 10 10 1 abc";
  5.  
  6. open PS, "$data1 |";
  7. PS->autoflush(1);
  8. while (<PS>) {
  9.     print;
  10.     print "\n";
  11. }
  12.  
but no luck. It appeared that the result are buffered or stucked somewhere while the C program is running and only get returned to my Perl script after the C program exited. Any help would be appreciated.
Thanks in advance.
Jul 22 '07 #1
10 1737
KevinADC
4,059 Expert 2GB
I've never used IO::Handle like you are doing. I have always used it to create an IO::Handle object and use methods on the object. But since you are using a pipe, look into IO::Pipe, which is also a core module.

http://perldoc.perl.org/IO/Pipe.html

You could also just use open() and not use a IO module.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my $data1 = './myC.exe 10 10 1 abc';
  4.  
  5. $|=1;
  6. open PIPE, "$data1 |" or die "$!";
  7. while (<PIPE>) {
  8.    print $_,"\n"
  9. }
  10. close PIPE;
Jul 22 '07 #2
Same problem. The output did not get displayed on the screen until the C program finished running. Any other suggestions?
Jul 22 '07 #3
KevinADC
4,059 Expert 2GB
are you running this as a cgi from the web browser or from the command line?
Jul 23 '07 #4
From the command line. I have 2 terminals displayed. On one terminal I see the C program sending requests to a database and sending back how many percent of the requests have been processed as thing progress. But on the Perl terminal, nothing is printed to the screen until the C program has finished.

The responses from the C program are:
+::010

+::020

+::030

+::040

+::050

+::060

+::070

+::080

+::090

+::100


where +::010 means 10% of the requests have been processed. When I run the c program directly from the command line, I can see the % completed being printed on the screen as the C program progressed. However, when calling it from Perl, all the requests are done before all of the above lines get printed at the same time. I just don't get it. Thanks for your help.
Jul 23 '07 #5
KevinADC
4,059 Expert 2GB
Sorry mate, I don't know what the problem is, seems like it should work. Hopefully Miller of Jeff or someone else will have an idea.
Jul 23 '07 #6
numberwhun
3,509 Expert Mod 2GB
Wow, that is really odd. To me, it should work as well. My next suggestion, unless Miller knows, would be to post this up on to Perlmonks.org and see if they know why?

Regards,

Jeff
Jul 23 '07 #7
miller
1,089 Expert 1GB
open used in this context is a blocking operation. Meaning, open will not actually finish until the secondary process has exitted.

Based off the script that you provided, what you probably want is this:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $data1 = "./myC.exe 10 10 1 abc";
  4.  
  5. system("$data1 &");
  6.  
This executes the command as a background process that share the same STDOUT and STDERR as the perl script. The system command is also blocking, so you can still list commands afterwords that you want executed sequentially. But all the output of $data1 will be sent to STDOUT immediately instead of waiting until the very end.

If you want to do some type of translation of the output of the command, then you'll have to do use IPC instead.

- Miller
Jul 24 '07 #8
KevinADC
4,059 Expert 2GB
Good suggestion. IPC::Open2 might be appropriate for this task. Maybe this link will help:

http://perldoc.perl.org/perlipc.html
Jul 24 '07 #9
I posted the question on Perl Monk and a few folks helped me solved this problem. The problem was the C program did not flushed its buffer after each printf statement. I can see the output on STDOUT b/c it is not buffered but the pipe is actually buffered so printf needs to be flushed with fflush.
Thanks for all who contributed.
Aug 1 '07 #10
miller
1,089 Expert 1GB
Yes, I observed your question there, and was happy to let the other monks throw in their vast wisdom. Glad you were able to figure out an answer.

- Miller
Aug 1 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: funtoosh | last post by:
Hi Scenario: I have a shell script e.g. a.bash This script wraps a program called "generate" like this: generate > /tmp/y.txt 2>&1 # both stdout and stderr r redirected to y.txt
4
by: Brian | last post by:
'Ello, I'm writing a perl script to spawn child processes to execute jobs. The jobs are system calls. I use open(SYSCALL, "cmd |), to make the system calls because I need the pipe so I can log...
1
by: momena | last post by:
Hi experts, I need to write a perl script that will be called from a series of executables chained thru pipes that read from STDIN and writes to STDOUT. The following code works fine under regular...
4
by: Pascal Ehlert | last post by:
I don't know if this is the right newsgroup because the question is maybe a bit linux specific so if not slap me ;-) I'm trying to send the output of a perl-script to a socket. So I'm opening a...
3
by: chudson007 | last post by:
Can somebody help me with a delimiter problem I have. I have several PIPE (|) delimted text files which I need to import to SQL. With one of the files I keep encountering the following error;...
2
by: David | last post by:
im trying to figure out how to pipe data to an application with c++ Here is how I would do it in perl open(OSA, "| /usr/bin/osascript") || die "Can't pipe to osascript"; print OSA "some text to...
2
by: Igna | last post by:
Hello. I have to write a pipe to joint a GUI in perl and a simulation program in c. I have read all the docs found in perl.com and now I am trying to make a test with this simple program if it is...
1
by: afelotreyu | last post by:
Hello, I currently have an application in perl that starts a text based torrent client, once I give the necessary parameters to the torrent.exe it will start printing out the following information...
1
by: Trevor17 | last post by:
Hello All, I am currently in a Perl programming class and our assignment was: Create a filehandle with the open function that uses a pipe to list all the files in your current directory and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.