santosh_sugur has asked for the wisdom of the Perl Monks concerning the following question:
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.
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#!/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;
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# ./test.pl This is parent 29307 This is child pid 29309 #
My questions:# 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
Thanks in Advance,
Santosh
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: exec creates two processes.. how to kill them?
by jettero (Monsignor) on Jan 10, 2008 at 16:26 UTC | |
by santosh_sugur (Initiate) on Jan 10, 2008 at 16:54 UTC | |
|
Re: exec creates two processes.. how to kill them?
by Eimi Metamorphoumai (Deacon) on Jan 10, 2008 at 17:18 UTC | |
by chem (Acolyte) on Jan 10, 2008 at 17:57 UTC | |
by Fletch (Bishop) on Jan 10, 2008 at 20:02 UTC | |
|
Re: exec creates two processes.. how to kill them?
by almut (Canon) on Jan 10, 2008 at 18:59 UTC | |
by santosh_sugur (Initiate) on Jan 11, 2008 at 08:56 UTC |