Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Killing the child process is not working

by srlbharu (Acolyte)
on Mar 20, 2013 at 05:45 UTC ( #1024446=perlquestion: print w/ replies, xml ) Need Help??
srlbharu has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I created the child process using fork and keep the child in the infinite loop. After some time expires, I wanted to kill the child process using kill -9, $pid; which doesnot help. Below is the code:

$|=1; $TimeOut = 100; $StopTime = time + $TimeOut; if($cmd =~ /INFINITE/i){ unless (fork){ $child_pid = $$; eval $cmd; while(1){ sleep(1); if (time >= $StopTime){ print "killing child process $$"; kill -9, $child_pid; } } exit 0; } } else{ eval $cmd; }
Here the parent process will aslo run for a long time. I wanted to kill the child before the parent dies. I am running the script in windows XP with perl 5.6 version. Please let me know if any alternatives to kill the child or if I did something wrong.

Comment on Killing the child process is not working
Select or Download Code
Re: Killing the child process is not working
by kcott (Abbot) on Mar 20, 2013 at 06:26 UTC

    G'day srlbharu,

    I'm not entirely certain what you're trying to achieve here. Sending a negative signal targets a process group not an individual process. To terminate the current process, you can just use die, exit or one of the Carp routines.

    I suspect you're lacking knowledge or understanding of one or more of the following. (Also note that many of these contain links labelled "Portability issues:".)

    -- Ken

Re: Killing the child process is not working
by BrowserUk (Pope) on Mar 20, 2013 at 07:29 UTC
    $child_pid = $$;

    $$ is not the child process identifier. You have the parent process trying to kill itself.

    But, more fundamentally, fork does not work properly on Windows; and you are wasting your time writing new code that tries to use it.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Yes I agree, But I know only one way to create the child to run it parallel to parent in perl 5.6 using fork

      Trying to upgrade the perl version leads to lot of compatibility issues so that I stopped my trails to upgrade

      Here I am getting the error: "Attemt to free uneferenced scalar" with perl 5.6 while exiting the child process and then terminating the parent process also. Hence I keep the child in infinite loop and once the parent is going to be completed I want to kill the child by sending signal like CTRL+C

      I tried the ways like die, exit, Win32::Process::Killprocess and kill 9,$pid; to kill the child no use

        But I know only one way to create the child to run it parallel to parent in perl 5.6 using fork

        If all you want it to run an asynchronous process, try my $childPid = system 1, q[theCommand and args];.

        But don;t expect much help if you run into problems. On 5.6, you are on your own. Noone wants to be bothered trying to re-debug code that has long since been fixed.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024446]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-07-12 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (242 votes), past polls