views:

100

answers:

4

hi

for a project i need to send some data from c program to Perl program that both in running mode (Perl program in sleep mode and c program running, and c program sends data to Perl).

i know that, can write this program by socket and shared memory but i have performance problem. i think shared memory is better solution, and how can i signal to Perl program to wake up and resume run and read data that c send?

+2  A: 

Use a database like MySql.

If you're that worried about performance implement the perl code in C, or vice versa.

Luca Matteis
no,i can't. i have heavy performance problem
haw3d
+5  A: 

You seem to have two questions here:

  1. how to share memory between the Perl program and your C program, and
  2. how to signal the Perl program there is new data available

Assuming you're on a system that allows SysV IPC calls, you can use IPC::ShareLite to share a chunk of memory between the two processes.

As usual with shared memory, you will have to ensure locks are in place. The module manual page for IPC::ShareLite seems to explain the intricacies and the method calls fairly well.

Regarding signaling the Perl program there is new data available, there's nothing stopping you from using... signals to achieve that! Your C program can send a SIGUSR1 to the Perl program, and the Perl program will access the shared memory and do stuff when receiving the signal, and sleep otherwise.

Have a look at perldoc perlipc for that, but the gist of it is something along these lines:

use strict;
use warnings;
use IPC::ShareLite;

# init shared memory

sub do_work {
    # use shared memory, as you just received a signal
    # indicating there's new data available
    my $signame = shift;
    # ...
}
$SIG{USR1} = \&do_work; # when signaled with SIGUSR1, call do_work()

# else just sleep
while(1) { sleep 1; }

Hope this helps,

-marco-

mfontani
+3  A: 
  • Have a look on topic :"Embedding Perl (Using Perl from C)"-Chapter 21 Perl Programming Third Edition - Larry wall

  • See Internals and C language interface specially this part-Perl calling conventions from C.

    Then, you will come to know how to send data efficiently between C and Perl Program.

Nikhil Jain
+1  A: 

Hi -

You keep referring to "performance problem".

Could you please be more specific about exactly what your performance requirements are? We need to know this before we can suggest an optimal solution. Otherwise, we're just guessing.

In the "guessing" category, I would recommend:

  • pipes
  • database

Either of these alternatives would be excellent alternatives in a vast majority of "real world" scenarios I could think of.

We definitely need more details about your particular scenario.

Thank you in advance .. PSM

To explain: it would be nice to have some idea of why the program is slow: does it keep the CPU busy at 100%? Does reading and writing data take too much time? Do you run out of memory?
reinierpost