On Thursday, Nov 27, 2003, at 09:58 US/Pacific, Douglas Lentz wrote:
Philipp Traeder wrote:[..]
Good morning everybody,
[..]# ...
elsif ($cmd eq 'long_action') {
if (!fork) {
# execute the action in the child process
sleep 10;
# TODO: notify the user that the action is finished.
exit; } }
Re: (A) What's the best way for the child to inform the parent that it's done?.
I can't speak from experience on this, but I have a copy of Lincoln Stein's (He wrote the Crypt::CBC module, among others) Network Programming in Perl. Lincoln recommends the pipe() function and says (verbatim quote) "It is commonly used in conjunction with the fork() function in order to create a parent-child pair that can exchange data. The parent process keeps one filehandle open and closes the other, while the child process does the opposite. The parent and child process can now communicate across the pipe as they work in parallel."
$result = pipe(READHANDLE,WRITEHANDLE) # pipe is a core perl function doc'd in the camel book.
Sounds like what you need.
Re: (B) I'm still thinking about this one.
Given that his question (A) is about 'informing' the parent that it is finished, he might really want to look at
perldoc -q signal
and specifically set up a sig handler for setting up a SIGCHLD so that he can set a flag,
my $all_my_children = {};
where the pid that would be returned from the fork, which would of course need to be picked up, is recorded.
cf perldoc perlipc
and look for 'sub REAPER'
Then in the main loop check the status of "all_my_children" to see if any of them have changed - and if so editorialize. This of course means that the long_activity will need to exit with say 0 if ok, or some number if not good, and the initialization would be say
$all_my_children->{$pid} = 'A';
and as a part of the loop to check the status, one gets the return value and then deletes it from the hash.
This may also help with the 'B' side question - as well without getting into the more complex set of issues of doing the pipe(), fork() manage the multiple selects that would need to poll through the long_activities to see if new incoming data needs to be collected, or is the primate banging on the keyboard.
But one can take that strategy.
ciao drieux
---
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]