Using exec()

B

Bill H

According to the perldoc exec() never returns when it is called, which
is fine, but I can't see anywhere if the calling script will continue
on or does it stop when you do the exec.

The way I want to use it is I have a script that uses PDF::API to make
a pdf based on user input, then using imagemagik it makes viewable
jpgs out of the pages. What I want to do, is when the script has
completed the above, I want to start another program that combines
pdf's that have been made for latter viewing, but don't want the user
to have to wait for it. Will the exec let me start another perl script
but allow the calling script to finish up and exit as if it had never
called an exec?

Bill H
 
J

Joost Diepenmaat

Bill H said:
According to the perldoc exec() never returns when it is called, which
is fine, but I can't see anywhere if the calling script will continue
on or does it stop when you do the exec.

exec() replaces the current process by the new one. The only time the
"calling script" will continue is if the exec() call fails. If exec
succeeds there is no "calling script" anymore.
The way I want to use it is I have a script that uses PDF::API to make
a pdf based on user input, then using imagemagik it makes viewable
jpgs out of the pages. What I want to do, is when the script has
completed the above, I want to start another program that combines
pdf's that have been made for latter viewing, but don't want the user
to have to wait for it. Will the exec let me start another perl script
but allow the calling script to finish up and exit as if it had never
called an exec?

Not on its own, no. You probably want some combination of fork() and
exec(). What I personally tend to do is

system("/some/function bla bla &") and die;

to start a program in the background.
 
J

Jürgen Exner

Bill H said:
According to the perldoc exec() never returns when it is called,

No. It never returns if the call was successfull. That is different.
which
is fine, but I can't see anywhere if the calling script will continue
on or does it stop when you do the exec.

Well, if the call doesn't return, then how could the script continue?
In any case, what happens behind the scenes is that your Perl code is
being replaced(!) by the called external program and the process starts
executing that new code.
Will the exec let me start another perl script
but allow the calling script to finish up and exit as if it had never
called an exec?

No. exec() replaces the code and the old code is gone. I think you may
be looking for system() instead of for exec().

jue
 
B

Bill H

No. It never returns if the call was successfull. That is different.


Well, if the call doesn't return, then how could the script continue?
In any case, what happens behind the scenes is that your Perl code is
being replaced(!) by the called external program and the process starts
executing that new code.


No. exec() replaces the code and the old code is gone. I think you may
be looking for system() instead of for exec().

jue

Jurgen, from your response and the others (thank you), I see I have to
use system() in some manner with fork() to get what I want.

An example using exec()

print "hello ";
exec("perl otherscript.pm");
print "world"; # World never prints

An example using system()

print "hello ";
system("perl otherscript.pm");
print "world "; # World prints when otherscript.pm is done

What I need to do:

print "hello ";
not_exec_or_system("perl otherscript,pm");
print "world"; # World prints immediately and otherscript.pm is
running also

Bill H
 
T

Tim McDaniel

What I need to do:

print "hello ";
not_exec_or_system("perl otherscript,pm");
print "world"; # World prints immediately and otherscript.pm is

that with

print "hello ";
system("perl otherscript.pm &");
print "world"; # World prints immediately and
# otherscript.pm is running also

That presumes that your system's command interpreter uses terminal "&"
to run a program in background: that works on Linux but not Windows
cmd.exe. It also doesn't allow you to monitor the progress of
otherscript.pm: neither the caller nor anything else can tell whether
it's still running (except for heroic measures, or unless it itself
signals its progress via a status file), or whether it ends with an
error exit (even something like otherscript.pm not being an existing
file, so it dies at once).

If one of those conditions doesn't work for you, then you have to
implement the "&" functionality yourself.
- Method 1: use fork(). The parent does NOT wait() or
waitpid() for the child. The child does an exec() of
perl otherscript.pm.
- Method 2: use fork(). The parent does NOT wait() or
waitpid() for the child. The child does a system() of
perl otherscript.pm. The child examines the return value of
system() to see whether it was a "crash on takeoff", or whether
there was a non-zero exit status, or a normal run, and does whatever
seems indicated (log an error, e-mail someone, whatever). The child
ends with an exit().
 
J

Jürgen Exner

Bill H said:
Jurgen, from your response and the others (thank you), I see I have to
use system() in some manner with fork() to get what I want.

Well, maybe.
What I need to do:

print "hello ";
not_exec_or_system("perl otherscript,pm");
print "world"; # World prints immediately and otherscript.pm is
running also

So you want that other program and the calling script to run in
parallel, in particular you don't want the calling script to wait until
the other program has finished. Is this correct?

If yes, then yes, you could use fork() and then exec() the other program
in the child process.
Or you could start the other program using system() and run it in the
background using whatever method your command shell and/or OS provides
to run programs in the background.
Both methods are valid and will work.

jue
 
T

Tad J McClellan

Bill H said:


You are not yet seeing correctly.

I have to
use system() in some manner with fork() to get what I want.


You have to use exec() in some manner with fork() or use system()
and put the process into the background.

An example using exec()


Where's the fork() that you said you needed with exec()?

print "hello ";
exec("perl otherscript.pm");
print "world"; # World never prints

An example using system()

print "hello ";
system("perl otherscript.pm");


system('perl otherscript.pm &');

print "world "; # World prints when otherscript.pm is done


print "world "; # World prints immediately and otherscript.pm is running also
 
X

xhoster

Tad J McClellan said:
You are not yet seeing correctly.


You have to use exec() in some manner with fork() or use system()
and put the process into the background.


Where's the fork() that you said you needed with exec()?

It's not there. He is showing examples of what he tried that didn't work,
which lead him to conclude he needed fork. I thought that was pretty
obvious. More people should show us how they reached their conclusions,
so we can point out if they are mistaken.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Tim Greer

It's not there. He is showing examples of what he tried that didn't
work,
which lead him to conclude he needed fork. I thought that was pretty
obvious. More people should show us how they reached their
conclusions, so we can point out if they are mistaken.

Xho

I thought it was pretty straight forward as well, but I seem to be
having trouble wording things today myself, so I suppose people could
have read it differently (and clearly did).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top