in reply to Call Intercepts
One problem is that you don't call exit in your child process (i.e. at the end of if ( $pid == 0 ) { ... }). Adding that, the program behaves for me like (I think) what you want to achieve — though I'm not entirely sure I've understood correctly... (i.e. it reports to have detected CTRL-C, finishes whatever countDown loop it was in, then quits) With an existing /tmp/test.txt, that is — which you don't seem to create anywhere in the script itself. open(FH, "+<$testFile1") ... does not create the file, rather, it dies with "No such file or directory" if the file doesn't already exist. In this case, test1()/test2() won't ever be called. Likewise, the message "$testFile1 file CREATED\n" is kinda bogus, as it'll only be printed if the file already did exist...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Call Intercepts
by perlnewbie9292 (Novice) on Dec 01, 2009 at 12:59 UTC | |
The code below works but when I added the IF to check if a file exits it does not work as desired when it reaches the ELSE statement (else { print "$testFile1) when the lck file indeed exists. While that section of code loops until the lck file is removed if a ctrl-c is sent while that section of code is running then the script just gets stuck and does not continue to wait until the lck file is removed. So as long as the ctrl-c is hit outside that else statement section it works as desired. With the code below which I removed the check of file existence the code work as desired. If I launch the script and hit ctrl-c then it only exits once it has reached the end of the test2 subroutine. To answer you other questions: testFile1 will always exits in that location. lck file gets created by the script although right now I have no added code to have it removed as I am trying to get this one part working first. Thanks again for all the help. Output when script is run with no interuption
OUTPUT WHEN CTRL-C is hit right after the script has been launched
| [reply] [d/l] [select] |
by almut (Canon) on Dec 01, 2009 at 16:34 UTC | |
it does not work as desired when it reaches the ELSE statement (else { print "$testFile1) when the lck file indeed exists. (...) the script just gets stuck and does not continue You're still not using exit in the child! :) The problem with this is that the child process continues to run code outside of the if ( $pid == 0 ) {...}, i.e. you have two processes (parent and child) running the same code, which is not what you want (in this particular case, each child forks a new process, while its parent hangs in waitpid() waiting for it to terminate, which it doesn't because it in turn is waiting for its own child...) Add
and you'll see what I mean — i.e. that it hangs after "doing waitpid(0,0)..." after you have pressed CTRL-C (in which case you stop forking new children due to $wantToQuit now being true), and that you're accumulating more and more processes otherwise — run ps axf from another terminal after a while, and you'll see something like this
with all those processes hanging in waitpid() because their children haven't yet exited. This problem only manifests in case of the else branch being run, because otherwise you are in fact exiting with the exit 0; at the end of test2(). Add an exit here
and the problem is gone. And if you want to execute the test*() routines in the else case, too (which I'm not quite sure as you're saying "...only exit if all code has been run through its entirety"), you'd have to take them out of the if branch... | [reply] [d/l] [select] |
by perlnewbie9292 (Novice) on Dec 01, 2009 at 16:55 UTC | |
| [reply] |