Hello PerlMonks,
Please help me with the following problem..

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


In reply to exec creates two processes.. how to kill them? by santosh_sugur

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.