Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Fellow monks,

I am writing some code where I have two processes created using fork() which communicate via sockets. I need to have only one of those processes execute functions, but the call to those functions may come from either process. So, I am trying to come up with a scheme to pass a function call, along with all arguments, from one process to the other via the socket. Here's what I have so far:

use warnings; use strict; use Socket; use IO::Handle; use Data::Dumper; socketpair(WORKER_SOCKET, GUI_SOCKET, AF_UNIX, SOCK_STREAM, PF_UNSPEC) + || die "socketpair: $!"; WORKER_SOCKET->autoflush(1); GUI_SOCKET->autoflush(1); if (my $pid = fork()) { close GUI_SOCKET; my $line = <WORKER_SOCKET>; if ($line =~ /^EXECUTE:(.*)$/) { # my $fx = "extracted function name"; # my @args = "extracted args" &{\&{$fx}}(@args); } } else { die "cannot fork: $!" unless defined $pid; close WORKER_SOCKET; # redirect the STDOUT of the WORKER script to GUI (and main Bacnet +Tool # communication) process. STDOUT->autoflush(1); my $a = 'baz'; my @b = qw(foo bar); my $c = \@b; print GUI_SOCKET "EXECUTE:doit($a, @b, $c)\n"; } sub doit { my @q = @_; print "\nDoing it " . Dumper(\@q); }
I have tried using the FreezeThaw module to serialize/deserialize the arguments on opposite sides of the socket. However, this does not work for arguments that are references, such as the 3rd argument in my example. Any ideas how else I can make such a remote function call

In reply to Executing functions from another process by gri6507

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-16 18:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found