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
In reply to exec creates two processes.. how to kill them? by santosh_sugur
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |