Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Set env variables as part of a command

by devilock76 (Novice)
on Feb 18, 2017 at 17:42 UTC ( [id://1182277]=note: print w/replies, xml ) Need Help??


in reply to Re: Set env variables as part of a command
in thread Set env variables as part of a command

I like that, thank you. It will have to wait till Monday till I can try it. So forking the process is that like creating a subshell or a sub perl process I am in. Sorry if I am being dense, but like I said what perl I have is rusty unfortunately.
  • Comment on Re^2: Set env variables as part of a command

Replies are listed 'Best First'.
Re^3: Set env variables as part of a command
by afoken (Chancellor) on Feb 18, 2017 at 18:08 UTC
    So forking the process is that like creating a subshell

    Excatly. That's the beauty of fork+exec, inherited from C. (Things are quite different on Windows. Perl emulates fork and exec, which makes some things quite messy.)

    fork() creates an exact clone of your currently running process, including environment, file handles and all data, except for process ID and parent process ID. (Most operating systems now use a copy-on-write mechanism, so that immediately after fork(), both process use exactly the same data. The first memory write in parent or child process transparently creates a real copy of the affected memory page. But that's an implementation detail. In the old days, fork() did a complete copy.) And yes, a successful fork() returns twice: once in the parent process, returning the process ID of the child process, and once in the child process, returning 0.

    exec() replaces the current process with another executable. Again, everything but the actual executable <UPDATE>and signal handlers</UPDATE> stays unchanged. Things like file handles, environment, and even the process ID stay the same. Command line arguments do change, to those passed to exec(). <UPDATE>Non-ignored signal handlers are reset to defaults.</UPDATE>

    Between fork() and exec(), in the (temporary) child process, you can change every aspect of the process you want: current directory, environment, file handles (for things like redirection), current user, current group.

    It's so simple that fork() does not even take arguments, and exec() just needs the name of the executable and the new command line arguments.

    Compare that with CreateProcess(), requiring no less than 10 arguments, four of them pointers to structures with 2 to 18 members, so that you effectively pass more than 30 parameters to CreateProcess(). Do you think that's sufficient? No, sorry. If you want to run a process as a different user, you need another function called CreateProcessAsUser(), taking 11 arguments. Obviously, that's still not sufficient, there are two more functions CreateProcessWithLogonW() and CreateProcessWithTokenW(). Don't ask me what happens there, I simply don't want to know that.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^3: Set env variables as part of a command
by devilock76 (Novice) on Feb 18, 2017 at 18:00 UTC
    Ok so yeah googled, learned what fork does. Let me ask another probably basic question I could not find in some googling. When I fork the program I execute I need to create an array. I need to return that array to the parent process. How would one do that?
      When I fork the program I execute I need to create an array. I need to return that array to the parent process. How would one do that?

      By serialising and unserialising. Create the array in the child process, write it to a file handle of a pipe, and read that pipe in the parent process. Perl wraps that in open('-|') / open('|-') See perlipc, especially section "Safe Pipe Opens". For simple arrays of simple strings not containing control characters, writing each string on a new line might be sufficient. Otherwise, use Storable, JSON, XML, YAML or something like that.

      Update: You might need to fork once again from your first child to be able to "return" an array after the external program has finished.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found