Re: BEGIN, INIT etc...
am 30.03.2008 03:39:21 von Joost Diepenmaat
Ben Morrow writes:
> Quoth Joost Diepenmaat :
>> Ben Morrow writes:
>>
>> > I am confused. How is one (on a fork/exec-based system, or perl's
>> > emulation of such under Win32) supposed to avoid calling exec when
>> > necessary?
>>
>> By using fork(), ofcourse! Am I missing something?
>
> Err... yes? fork creates a clone of the current process, exec changes
> which file the current process is executing. Any method of executing an
> external program, on Unix, ends up calling exec.
But for the purposes of END blocks, using fork() before an exec() would
allow the END blocks to run anyway:
# simplified:
END {
# clean up stuff here
# assuming close-on-exec & equivalents are correctly set
}
if (! fork()) { # assumin fork() doesn't fail here.
exec $whatever;
exit;
}
exit;
This is assuming the fork() or exec() themselves won't introduce new
issues.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Re: BEGIN, INIT etc...
am 30.03.2008 04:10:47 von szr
Ben Morrow wrote:
> Quoth Joost Diepenmaat :
>> Ben Morrow writes:
>>
>>> Quoth Joost Diepenmaat :
>>>> Ben Morrow writes:
>>>>
>>>>> I am confused. How is one (on a fork/exec-based system, or perl's
>>>>> emulation of such under Win32) supposed to avoid calling exec when
>>>>> necessary?
>>>>
>>>> By using fork(), ofcourse! Am I missing something?
>>>
>>> Err... yes? fork creates a clone of the current process, exec
>>> changes which file the current process is executing. Any method of
>>> executing an external program, on Unix, ends up calling exec.
>>
>> But for the purposes of END blocks, using fork() before an exec()
>> would allow the END blocks to run anyway:
>
> Sorry, I misunderstood you. Yes, that will work, of course.
>
>> # simplified:
>>
>> END {
>> # clean up stuff here
>> # assuming close-on-exec & equivalents are correctly set
>> }
>>
>> if (! fork()) { # assumin fork() doesn't fail here.
>> exec $whatever;
>> exit;
>
> You need POSIX::_exit here, or the END blocks get run twice (once for
> each process).
This could actually desirable in some cases, such as when you need to
free/clean-up in each child? (Albiet it would probably be done
differently.)
--
szr
Re: BEGIN, INIT etc...
am 30.03.2008 04:55:12 von Ben Morrow
Quoth Joost Diepenmaat :
> Ben Morrow writes:
>
> > Quoth Joost Diepenmaat :
> >> Ben Morrow writes:
> >>
> >> > I am confused. How is one (on a fork/exec-based system, or perl's
> >> > emulation of such under Win32) supposed to avoid calling exec when
> >> > necessary?
> >>
> >> By using fork(), ofcourse! Am I missing something?
> >
> > Err... yes? fork creates a clone of the current process, exec changes
> > which file the current process is executing. Any method of executing an
> > external program, on Unix, ends up calling exec.
>
> But for the purposes of END blocks, using fork() before an exec() would
> allow the END blocks to run anyway:
Sorry, I misunderstood you. Yes, that will work, of course.
> # simplified:
>
> END {
> # clean up stuff here
> # assuming close-on-exec & equivalents are correctly set
> }
>
> if (! fork()) { # assumin fork() doesn't fail here.
> exec $whatever;
> exit;
You need POSIX::_exit here, or the END blocks get run twice (once for
each process).
> }
> exit;
You probably want to wait here before exitting, as otherwise the execed
process is orphaned, and your parent gets SIGCHLD early. You probably
also want to set up signal handlers to pass signals along, etc... see
the source for the shell of your choice :). Or you could just use
system, which does all that for you.
Ben