Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: exec creates two processes.. how to kill them?

by Eimi Metamorphoumai (Deacon)
on Jan 10, 2008 at 17:18 UTC ( #661662=note: print w/replies, xml ) Need Help??


in reply to exec creates two processes.. how to kill them?

You've already got an answer for a better way to do it (and I'll mention in passing that you should probably use unlink instead of shelling out for rm), but I'll answer the question you actually asked.

What's happening is that, because of the redirection, perl is spawning a shell, and passing the command line you specified to it. That's your 29309 process. That shell, then, sets up the stdin and stdout filehandles, and itself forks another process (the cat process, 29310). As far as how to fix it, there are almost certainly ways to parse the output of ps or do all sorts of crazy things, but the simplest answer is to just do the copying internally in perl. In fact, in that case instead of copying "for about a second", you can decide on exactly how much data you want to copy, and just use that.

Replies are listed 'Best First'.
Re^2: exec creates two processes.. how to kill them?
by chem (Acolyte) on Jan 10, 2008 at 17:57 UTC
    To make it complete, here's a working code that reopen the STDOUT in the child process in order to avoid altogether the redirection in the command. Then, the exec do not spawn the shell.
    #!/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 = $$; print "This is parent $parent \n"; defined($pid = fork) or die "Cannot fork: $!"; unless($pid) { print "This is child pid $$\n"; open (STDOUT, '>', 'testfile') or die "Can't create testfile: $!"; select STDOUT; $| = 1; my @cmd = qw(cat /dev/urandom); # Child process is here exec { $cmd[0] } @cmd; } sleep 1; kill 15 => $pid; wait;
Re^2: exec creates two processes.. how to kill them?
by Fletch (Chancellor) on Jan 10, 2008 at 20:02 UTC

    The slightly less crazy way to do it if you're bound and determined to use the shell to do redirection (which is indeed silly in this particular case) is to use the shell's exec command to tell it to exec the command you're asking it to run in place of itself (well, that perl is indirectly asking it to run on your behalf because you used the single argument form of exec with a string containing shell metacharacters).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-10-23 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (238 votes). Check out past polls.

    Notices?