|
|
|
|
|
Posted by Ben Morrow on March 29, 2008, 9:55 pm
Please log in for more thread options
>
> >>
> >> > 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
|
|
Posted by Joost Diepenmaat on March 29, 2008, 10:07 pm
Please log in for more thread options
>> 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).
Sorry, that's my mistake: exec() never returns, unless it fails, and I
tried to disregard every complication to make the point.
>> }
>> 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.
Sure, this is just the extremely simplified version :-)
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
|
|
Posted by szr on March 29, 2008, 10:10 pm
Please log in for more thread options Ben Morrow wrote:
>>
>>>>
>>>>> 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
|
|
Posted by Joost Diepenmaat on March 29, 2008, 10:17 pm
Please log in for more thread options > Ben Morrow wrote:
>>> 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.)
Normally, the exec() would succeed, and the exit() would not be called
at all. I tried to ignore all error handling, and that exit() statement
just slipped through.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
|
|
Posted by Ilya Zakharevich on March 30, 2008, 3:43 am
Please log in for more thread options [A complimentary Cc of this posting was sent to
Ben Morrow
> > do not forget to never use exec()"?
>
> 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?
The situations when it is *necessary* to call exec() do exist. But
they are extremely rare. fork() is useful slightly more often; but
again, these situations are in no way frequent.
[The introduction of fork()/exec() mess is one of the major weaknesses
of *nix. Fortunately, Perl API makes it practically never needed.]
Yours,
Ilya
|
| Similar Threads | Posted | | Q on BEGIN and INIT | April 19, 2005, 3:05 pm |
| Should I use BEGIN, CHECK, or INIT? | October 27, 2004, 8:32 pm |
| CHECK and INIT | May 19, 2006, 11:00 am |
| BEGIN <> BEGIN | May 31, 2006, 5:32 am |
| Where to begin with Perl 6 | September 5, 2005, 11:03 am |
| BEGIN { package Foo; use Foo } | December 24, 2005, 6:49 pm |
| Nub question - were to begin? | April 26, 2006, 2:33 pm |
| bless an object in a BEGIN block (Singleton) | October 13, 2006, 3:02 am |
| BEGIN not safe after errors--compilation aborted | February 22, 2008, 11:26 am |
| Regular expression to match any line that DOESN'T begin with a particular string | March 22, 2006, 7:48 pm |
|
|
|
|