Using fork()
am 13.11.2007 17:15:11 von 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 {
$gotit = 1;
}
}
} else {
while (1) {
my $input = ;
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?
Re: Using fork()
am 13.11.2007 18:00:24 von krahnj
Q wrote:
>
> I'm trying to use fork and have the processes interact with each
> other... Is this possible?
perldoc perlipc
John
--
use Perl;
program
fulfillment
Re: Using fork()
am 14.11.2007 06:22:22 von Charles DeRykus
On Nov 13, 8:15 am, Q wrote:
> 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 {
>
> $gotit = 1;
> }
> }} else {
>
> while (1) {
> my $input = ;
> 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 = ;
print $input,"\n";
kill 'USR1', $parent
or die "can't signal $parent";
}
}
Re: Using fork()
am 14.11.2007 20:23:02 von sheinrich
On Nov 13, 5:15 pm, Q wrote:
> 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 {
>
> $gotit = 1;
> }
> }} else {
>
> while (1) {
> my $input = ;
> 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
Re: Using fork()
am 16.11.2007 04:24:33 von fishfry
In article <1194970511.814528.228370@22g2000hsm.googlegroups.com>,
Q wrote:
> 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 {
>
> $gotit = 1;
> }
> }
> } else {
> while (1) {
> my $input = ;
> print $input;
> $gotit = 0; #when I get input, I want to set gotit back to 0 so
> the parent starts again.
> }
> }
Other responders didn't mention this so I wanted to point out that your
basic understanding is incorrect. Post-fork, the two processes do not
share the same memory space. The variable $gotit belonging to the parent
is not the same as the one belonging to the child.
In order for the two programs to communicate, they need to use sockets,
pipes, shared memory segments, or signals, as others pointed out
(assuming this is on a Un*x-like system).