Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Spawning processes from scalars

by Corion (Patriarch)
on Apr 25, 2011 at 13:22 UTC ( [id://901192]=note: print w/replies, xml ) Need Help??


in reply to Spawning processes from scalars

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.

Replies are listed 'Best First'.
Re^2: Spawning processes from scalars
by Anonymous Monk on Apr 25, 2011 at 13:42 UTC
Re^2: Spawning processes from scalars
by fluffyvoidwarrior (Monk) on Apr 25, 2011 at 14:30 UTC
    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.

        Thanks. Not really an option. So it's plan B then...

      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.

        Thanks. I'll give that a go since it is effectively the same thing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://901192]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-29 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found