Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Redirecting C Program output to perl?

  • 29-01-2008 11:27pm
    #1
    Closed Accounts Posts: 570 ✭✭✭


    Hi guys,

    Bit of a problem here - I have a Unix C Program (which I didn't write) and it constantly outputs info every few seconds while it is running (using printf , then fflush (stdout)).

    Now I am writing a perl program that needs to capture this output in real time. But usings system(); and exec(); to call the C program, they both need to wait until the process is completed before returning to perl.

    Is there any way around this? Is it possible to capture data from a running process in perl or do you need to wait until it is complete?

    Thanks in advance for any help, would be very much appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    if its on linux can you not pipe/redirect the output into the perl program?

    You could also write to file and read in perl


  • Closed Accounts Posts: 570 ✭✭✭BrandonBlock


    Well , the "C Program" is actually called from within perl using a button, and may not have to be executed at all so I can't redirect the output into perl at runtime anyway. I'll give the file thing a try though but I'm not sure if bash redirects to files in "realtime" so to speak. The C Program is running like this example: (in minutes and seconds)

    0:00 - Perl prog begins executing
    0:35 - Perl prog calls C prog (i.e. user presses button)
    0:36 - C prog outputs something
    (0:36) - (Perl prog needs that output instantly)
    0:40 - C prog outputs something else
    (0:40) - (Again, perl needs this instantly, and so on)
    0:41 - C prog outputs something else
    0:53 - C prog outputs something else
    0:54 - Perl prog kills C prog


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Ah i see - am I guess you could write to file but I guess you could use threads to overcome the problem of having to wait for the C program to finish?

    I amn't too familiar with perl though


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Here's an example, using ping in place of your C program.
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    if (my $ping_pid = open(PING, "ping 127.0.0.1 |")) {
        while (my $ping_output = <PING>) {
            # Do something with the output, let's say print
            print $ping_output;
    
            # Kill the C program based on some arbitrary condition (in this case
            # the output of the program itself).
            if ($ping_output =~ /icmp_seq=69 /o) {
                kill 9, $ping_pid;
            }
        }
    }
    


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Well , the "C Program" is actually called from within perl using a button, and may not have to be executed at all so I

    If you get to execute the C program yourself from your own perl program, then IRLConor's example is the one to use. The program run with 'open' runs immediately in parallel with the perl program. You get to read its output whenever you feel like it.

    If you don't have control (it's not your perl program), then you'll have to play games with either named pipes or doing a fork, exec:

    Eg:
    open FOO, "cat -n file|";
    or
    open FOO, "-|" or exec 'cat', '-n', 'file';

    In the second instance, reading from FOO gets the STDOUT of the execed cat.

    Fun eh!

    Conor.


  • Advertisement
  • Closed Accounts Posts: 570 ✭✭✭BrandonBlock


    Cheers lads, IRLConor's method is working to an extent, but the guy who wrote the C added a fflush(stdout) after every printf so that only the last line is ever displayed on the stdout (bash shell in this case) which is causing problems with the perl program. I might have to try recompiling his code and remove that line. Thanks all the same ;)


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Cheers lads, IRLConor's method is working to an extent, but the guy who wrote the C added a fflush(stdout) after every printf so that only the last line is ever displayed on the stdout (bash shell in this case) which is causing problems with the perl program. I might have to try recompiling his code and remove that line. Thanks all the same ;)

    Can't imagine that would make any difference! As a test you might try redirecting the C program into a file and having a look at the file to see what's coming out. I've seen programs decide to print with \r instead of \n, leading to each new line erasing the previous line on the console.

    C.


  • Closed Accounts Posts: 570 ✭✭✭BrandonBlock


    carveone wrote: »
    Can't imagine that would make any difference! As a test you might try redirecting the C program into a file and having a look at the file to see what's coming out. I've seen programs decide to print with \r instead of \n, leading to each new line erasing the previous line on the console.

    C.

    Ahh brilliant!! That was it , thanks so much! I recompiled yer mans program with \n in place of \r and it's working now. Thanks to everyone else who helped aswell.


Advertisement