in reply to communication between C and perl

I'm not so good with C, but I'm really interested how that works with the named pipes.
If a small example is too much to ask, may be a link ?

Thnx LuCa

Replies are listed 'Best First'.
Re^2: communication between C and perl
by zentara (Cardinal) on Feb 09, 2007 at 12:27 UTC
    Try these. They are super simple and don't take errors and broken-pipe conditions into account. Start the c program, then start the perl program. The perl program could be launched from c if you wanted.
    #include <stdio.h> int main(void) { FILE *fp; /* char *line; updated correction */ char line[256]; if (mkfifo ("age-pipe", 0755) != 0) {printf ("Could not make this fifo\n");} else {printf ("FIFO was successfully made!\n");} fp = fopen ("age-pipe", "r"); for(;;){ fgets (line,256, fp); printf ("message: %s\n", line); } close (fp); }
    #!/usr/bin/perl use strict; use warnings; open(FIFO, "> age-pipe") or die $!; select FIFO; $|=1; select STDOUT;$|=1; my $input = 'a'; while(1){ print "Enter your age:\n"; #my $input = <STDIN>; print FIFO "$input\n"; sleep(1); $input++ } close(FIFO);

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      When I run your example I get
      ~/tmp/cperl> ./a.out FIFO was successfully made! Segmentation fault
      I get the segmentation fault (from the c program) when I start the perl script :(

      Anyway, what is so different about this approach and simply using the number 63 ?

      LuCa
        Yeah, thats weird. I probably cheated on the way line is defined, and my compiler automagically fixed it. Instead of
        char *line; /* try */ char line[256];
        C is very finicky about allocating space for strings, and will segfault without telling why. This is one of the primary reasons people like Perl over C.

        Try this free book: Advanced Linux Programming and read the chapter on IPC.

        The difference between my script and your's using &63, is that mine uses a named-pipe which can be accessed by different process. Using &63, you are using a shared filehandle referenced by it's fileno, and requires that both scripts share them..... that means 1 script must fork off the other, instead of them being started separately. Of course, I'm not an expert c programmer, so take that with a grain of salt. This isn't the place to discuss c anyways.


        I'm not really a human, but I play one on earth. Cogito ergo sum a bum