Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Using exec() (http://www.velocityreviews.com/forums/t909441-using-exec.html)

Bill H 01-14-2009 02:17 PM

Using exec()
 
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

Joost Diepenmaat 01-14-2009 02:23 PM

Re: Using exec()
 
Bill H <bill@ts1000.us> writes:

> 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.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Jürgen Exner 01-14-2009 05:12 PM

Re: Using exec()
 
Bill H <bill@ts1000.us> wrote:
>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

Bill H 01-14-2009 09:34 PM

Re: Using exec()
 
On Jan 14, 12:12*pm, Jürgen Exner <jurge...@hotmail.com> wrote:
> Bill H <b...@ts1000.us> wrote:
> >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


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

Tim McDaniel 01-14-2009 09:49 PM

Re: Using exec()
 
In article <2544da32-be11-4f07-9f82-f4aa36534b34@r36g2000prf.googlegroups.com>,
Bill H <bill@ts1000.us> wrote:
>What I need to do:
>
>print "hello ";
>not_exec_or_system("perl otherscript,pm");
>print "world"; # World prints immediately and otherscript.pm is


As Joost Diepenmaat <joost@zeekat.nl> wrote, you may be able to do
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().

--
Tim McDaniel, tmcd@panix.com

Jürgen Exner 01-14-2009 10:15 PM

Re: Using exec()
 
Bill H <bill@ts1000.us> wrote:
>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

Tad J McClellan 01-14-2009 11:39 PM

Re: Using exec()
 
Bill H <bill@ts1000.us> wrote:

> I see



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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

xhoster@gmail.com 01-15-2009 01:09 AM

Re: Using exec()
 
Tad J McClellan <tadmc@seesig.invalid> wrote:
> Bill H <bill@ts1000.us> wrote:
>
> > I see

>
> 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()?


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.

Tim Greer 01-15-2009 01:17 AM

Re: Using exec()
 
xhoster@gmail.com wrote:

> Tad J McClellan <tadmc@seesig.invalid> wrote:
>> Bill H <bill@ts1000.us> wrote:
>>
>> > I see

>>
>> 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()?

>
> 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).
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


All times are GMT. The time now is 01:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.