Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Spawning processes from scalars

by fluffyvoidwarrior (Monk)
on Apr 25, 2011 at 13:18 UTC ( #901189=perlquestion: print w/replies, xml ) Need Help??
fluffyvoidwarrior has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Can anyone tell me how to run a binary executable that has been slurped into a scalar without writing it to disk? If it is actually possible, of course. I'm looking to add a compiled "cutting edge" to a very large perl program. Thanks for any advice...

Replies are listed 'Best First'.
Re: Spawning processes from scalars
by Corion (Pope) on Apr 25, 2011 at 13:22 UTC

    That's basically not possible, at least not unless you tell us on what operating system this should happen.

    For very specialized cases, you might get away by using a combination of fork to create a copy of your current process and then jumping into the binary code of the executable. But again, that requires very much specialization, as no fixups and no dynamic loading will have happened to the binary executable. Normally, all these tasks are done by the operating system, and you will have to replicate these.

    Also see File::Temp, chmod and system for how to launch the file.

      Oops, sorry, Ubuntu server 10.04 64bit. I have no idea how to do it and can't find any info anywhere so I thought it probably wasn't a realistic option. The best I can think of is to use a ram disk and then wipe it.

        There only is the hard way of copying your process (via fork), patching up your scalar like your OS would, and then jumping (via XS or assembly code) to the start of the process.

        On Solaris, the /tmp "directory" resides in RAM and is not flushed to disk - if it really is a pressing matter of performance, you could mount /tmp as a ramdisk on your system as well. Note that for security reasons, programs in /tmp often are not allowed to execute, because malicious code uses this directory as a vector to drop files. So you might want to set up a directory similar but different from /tmp.

        That is fairly easy to do under ubuntu. Firstly create a tmpfs partition of a suitable size, and define a mount point.

        In your /etc/fstab:

        none /binary_exec tmpfs size=2G 0 0

        Or on the command line (as root)

        mount none /binary_exec -t tmpfs -o size=2G

        The mount point for the tmpfs partition must be created as a directory first. It need not be in the root of your file-system.

        You can be quite generous with the size of the tmpfs volume, as it is only the upper limit for the file-system, so you won't consume 2G of RAM just by creating the file-system, only the total size of the files you write there, and in any case file data in tmpfs will get swapped out if the fs is large enough to cause memory pressure.

        Once you have created the temporary partition, you can write your executable binary there from your perl scalar in the normal way. It should be very quick as you are just copying bytes from one part of ram to another. From there it is trivial to run the executable. You can also delete files just as quickly.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://901189]
Approved by ikegami
Front-paged by tye
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2017-07-21 03:37 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (318 votes). Check out past polls.