Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] how to kill child processes when one exec spawn two processes

Expand Messages
  • Santosh Sugur
    Thanks David. Frankly I did not think of head -c 1024 /dev/urandom testfile2 as a solution.. makes me feel foolish now. And yes I should use unlink instead
    Message 1 of 4 , Jan 11, 2008
      Thanks David.
      Frankly I did not think of head -c 1024 /dev/urandom > testfile2 as a
      solution.. makes me feel foolish now. And yes I should use unlink instead of
      rm.. thats what the folks at perlmonks also said where I had posted this
      same question after I didn't get a reply from this group for a long time.

      Regards,
      Santosh

      On Jan 11, 2008 3:11 PM, david wright <david_v_wright@...> wrote:

      > And happy new year to you sir.
      >
      > > My questions:
      > > 1. When I kill the process process 29310 testfile stops
      > > growing, but how do I get that to happen from within the code?
      >
      > kill the parent
      >
      > > 2. Why does process 29309 still show when I have killed it
      > > in my code?
      >
      > you didn't
      >
      > [0:dwright/perl> perldoc -f kill
      > kill SIGNAL, LIST
      >
      > Unlike in the shell, if SIGNAL is negative, it
      > kills process groups instead of processes.
      >
      >
      > this will accomplish what you seek:
      > kill -9 => $parent;
      > # also for fun see what happens if you do this: kill 9 => $pid;
      >
      > [0:dwright/perl>perl pb_ques.pl
      > This is parent 724
      > This is child pid 725
      > Killed
      > [137:dwright/perl> ps -ef|grep urandom
      > dwright 729 32516 0 09:21 pts/0 00:00:00 grep urandom
      > [137:dwright/perl> ls -ltr testfile
      > -rw-r--r-- 1 dwright users 5439488 Jan 11 09:21 testfile
      >
      > [137:dwright/perl> wc -l testfile
      > 21305 testfile
      >
      >
      > This is very awkward to read ( and uses unnecessary system call):
      > if (-f "./testfile") {
      > !system "rm -f testfile" or die "Couldn't delete already present
      > testfile\n";
      > }
      >
      > this should accomplish the same behaviour:
      > if (-e "./testfile") { unlink "./testfile"; }
      >
      >
      > Additionally, I have to assume you wrote this script to learn/practice
      > using 'fork'. The task you are trying to accomplish is easily done as such:
      >
      >
      > [0:dwright/perl> head -n 21305 /dev/urandom > testfile2
      > [0:dwright/perl> wc -l testfile*
      > 21305 testfile
      > 21305 testfile2
      > 42610 total
      >
      >
      >
      >
      >
      >
      > --- On Thu, 1/10/08, Santosh Sugur <santosh.sugur@...> wrote:
      >
      > > From: Santosh Sugur <santosh.sugur@...>
      > > Subject: [PBML] how to kill child processes when one exec spawn two
      > processes
      > > To: perl-beginner@yahoogroups.com
      > > Date: Thursday, January 10, 2008, 9:21 AM
      > > Hello Folks,
      > >
      > > This is my first question here in the new year.. so happy
      > > new year to you
      > > all!!
      > > ----------------------------------------------------------
      > > My goal is to fill a file - called testfile with data by
      > > cat'ing
      > > /dev/urandom so I have written the following code it. I
      > > just want to fill
      > > the file for about 1 second and then I want to kill the
      > > process.
      > >
      > > #!/usr/bin/perl
      > > use warnings;
      > > use strict;
      > >
      > > if (-f "./testfile")
      > > {
      > > !system "rm -f testfile" or die
      > > "Couldn't delete already present
      > > testfile\n";
      > > }
      > > my $pid;
      > > my $parent = $$;
      > > open (INFILE, "+>testfile") or die
      > > "Can't create testfile: $!";
      > > print "This is parent $parent \n";
      > > defined($pid = fork) or die "Cannot fork: $!";
      > > unless($pid)
      > > {
      > > # Child process is here
      > > print "This is child pid $$\n";
      > > exec "cat /dev/urandom > testfile";
      > > }
      > > sleep 1;
      > > close INFILE;
      > > kill -9 => $pid;
      > >
      > > When exec is executed here actually two processes are
      > > created and though it
      > > baffled me initially I realized there were actually two
      > > things going on in
      > > the exec call; one is the cat /dev/urandom and the second I
      > > believe is the
      > > shell is being invoked to carry out the redirection.
      > >
      > > So the output of the program is as follows
      > > # ./test.pl
      > > This is parent 29307
      > > This is child pid 29309
      > > #
      > >
      > > Now even after the prgram finishes execution (that is I get
      > > the prompt
      > > back), the testfile is still being populated from the
      > > output of
      > > /dev/urandom. This should not happen.
      > >
      > > and a ps -ef gives the following
      > >
      > > # ps -ef|grep urandom
      > > root 29309 1 0 00:53 pts/2 00:00:00 sh -c cat
      > > /dev/urandom >
      > > testfile
      > > root 29310 29309 99 00:53 pts/2 00:00:12 cat
      > > /dev/urandom
      > > My questions:
      > > 1. When I kill the process process 29310 testfile stops
      > > growing, but how do
      > > I get that to happen from within the code?
      > > 2. Why does process 29309 still show when I have killed it
      > > in my code?
      > >
      > >
      > > Thanks in Advance,
      > > Santosh
      > >
      > >
      > > [Non-text portions of this message have been removed]
      > >
      > >
      > >
      > > Unsubscribing info is here:
      > > http://help.yahoo.com/help/us/groups/groups-32.html
      > > Yahoo! Groups Links
      > >
      > >
      > >


      [Non-text portions of this message have been removed]
    • merlyn@stonehenge.com
      ... david this should accomplish the same behaviour: david if (-e ./testfile ) { unlink ./testfile ; } Or even just: unlink testfile ; # and ignore the
      Message 2 of 4 , Jan 11, 2008
        >>>>> "david" == david wright <david_v_wright@...> writes:

        david> this should accomplish the same behaviour:
        david> if (-e "./testfile") { unlink "./testfile"; }

        Or even just:

        unlink "testfile"; # and ignore the error

        Or if you're scared of a weird error:

        unlink "testfile" or $! =~ /no such/i or die "rm testfile: $!";

        That way, if the "error" is not "no such file", you'll die reporting
        the error.

        Try to avoid creating race conditions deliberately. There's enough
        of them created accidentally. :)

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      Your message has been successfully submitted and would be delivered to recipients shortly.