CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
does anybody know what this is doing?

 



bongu
New User

Feb 27, 2008, 6:31 PM

Post #1 of 3 (1443 views)
does anybody know what this is doing? Can't Post

#!/usr/bin/perl

use Fcntl;

my $r;
my $read;
my $w;
my $write;

pipe($r,$w);
fcntl($r,F_SETFD,0);

my $pid = fork();
if ($pid) {
close($w);
$read = fileno($r);
}
else {
close($r);
$write = fileno($w);
print $w "foobar\n";
chmod '0444', "/proc/$pid/fd/$write";
close($w);
exit 0;
}

do something with /dev/fd/$read


KevinR
Veteran


Feb 27, 2008, 10:48 PM

Post #2 of 3 (1438 views)
Re: [bongu] does anybody know what this is doing? [In reply to] Can't Post

I'm not sure. Maybe somone else will be able to explain it. If not, ask on www.perlmonks.com
-------------------------------------------------


BorisE
Novice

May 18, 2008, 10:59 AM

Post #3 of 3 (1285 views)
Re: [bongu] does anybody know what this is doing? [In reply to] Can't Post

Looks like a forking code example (Hope I'm not stating the obvious here). The program should run normally as far as fork at which point the process duplicates itself, both processes continuing from that point. In the parent process "fork" returns the PID of the child, in the child process I think it returns undef. By testing $pid it determines which branch it took, the child branch writes some stuff to a pipe and the parent branch is probably meant to read it.

I've no idea at all what the fcntrl and chmod are for.

There's probably a good reason its laid out like that but personally I'd prefer to do something like


Code
  my $pid=fork();  
unless ($pid)
{
# Do child stuff
exit;
}
# Do parent stuff here



BTW I probably shouldn't post in ADVANCED yet but I found this with a forum search, I'm actually searching for advice on passing file handles to subroutines.


(This post was edited by BorisE on May 18, 2008, 11:01 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives