Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Taking control back once Interactive script completes

by afoken (Chancellor)
on Feb 27, 2020 at 20:00 UTC ( [id://11113511]=note: print w/replies, xml ) Need Help??


in reply to Re: Taking control back once Interactive script completes
in thread Taking control back once Interactive script completes

No need to fork and no need to exec. system will do nicely.

Behind the scenes, system() combines fork() and exec() with the missing part of the story: wait resp. waitpid waiting for the child process in the parent process. Or, at least it does so on Unix-like systems. What happens on the children of CP/M (DOS, Windows, OS/2) is a different story ...

But on Unix and friends, all that's really missing is wait():

# Lots of random code # unless (fork()) { exec "inter.exe", "--active"; } wait(); # Some remaining code

To make that a little more robust, check if fork() returns undef. It may happen from time to time, RTFM. Also, you may want to keep the PID of the child process in the parent process and explicitly wait for that process to return:

# Lots of random code # my $pid=fork() // die "Can't fork: $!"; # on ancient perls: my $pid=fork(); defined($pid) or die "Can't fork: +$!"; unless ($pid) { exec "inter.exe", "--active"; } waitpid($pid,0); # Some remaining code

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://11113511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found