Using fork()

Q

Q

I'm trying to use fork and have the processes interact with each
other... Is this possible? Something like:

my $pid = fork();
my $gotit = 0;

if ($pid) {
while (1) {
if $gotit = 0 {
<do a thing>
$gotit = 1;
}
}
} else {
while (1) {
my $input = <STDIN>;
print $input;
$gotit = 0; #when I get input, I want to set gotit back to 0 so
the parent starts again.
}
}

Is there a good way to accomplish this?
 
C

comp.llang.perl.moderated

I'm trying to use fork and have the processes interact with each
other... Is this possible? Something like:

my $pid = fork();
my $gotit = 0;

if ($pid) {
while (1) {
if $gotit = 0 {
<do a thing>
$gotit = 1;
}
}} else {

while (1) {
my $input = <STDIN>;
print $input;
$gotit = 0; #when I get input, I want to set gotit back to 0 so
the parent starts again.
}

Here's a possible solution with the child sending
a SIGUSR1 signal to the parent. (I'm not sure how you intend the child
or the parent to exit their loops though. Exercise for the reader.
perldoc perlipc for other possibilities )

--
Charles DeRykus


my $pid = fork();
die "fork failed: $!" unless defined $pid;

my $gotit = 1;

if ($pid) { # parent
local $SIG{'USR1'} = sub { $gotit = 0; };
while (1) {
if ( $gotit == 0 ) {
print "I GOT IT\n";
$gotit = 1;
}
sleep 2;
}
waitpid $pid, 0;

} else { #child
my $parent = getppid();
while (1) {
my $input = <STDIN>;
print $input,"\n";
kill 'USR1', $parent
or die "can't signal $parent";
}
}
 
S

sheinrich

I'm trying to use fork and have the processes interact with each
other... Is this possible? Something like:

my $pid = fork();
my $gotit = 0;

if ($pid) {
while (1) {
if $gotit = 0 {
<do a thing>
$gotit = 1;
}
}} else {

while (1) {
my $input = <STDIN>;
print $input;
$gotit = 0; #when I get input, I want to set gotit back to 0 so
the parent starts again.
}

}

Is there a good way to accomplish this?

Some months ago I wrote a module for exactly this purpose.
You can get it from here (pod included):
http://www.atablis.com/temp/PreforkAgent.pm

Some other modules are in the CPAN Parallel namespace.

Cheers,
Steffen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top