views:

545

answers:

5

How can I make the same variable shared between the forked process? Or do I need to write to a file in the parent then read the value saved to the file in the child once the file exists? $something never appears to get set in this so it just loops in the sleep

my $something = -1;
&doit();
sub doit
{

 my $pid = fork();
 if ($pid == 0)
 {
      while ($something == -1)
      {
         print "sleep 1\n";
         sleep 1;
      }
      &function2();
 }
 else
 {
     print "parent start\n";
    sleep 2;
    $something = 1;
    print "parent end: $something\n";
 }
}

sub function2 {
   print "END\n";
}
+5  A: 

Variables aren't normally shared between processes, if you want to communicate 2 processes you better use pipes or shared memory or any other IPC.

Arkaitz Jimenez
Don't link to pirated copies of books.
Sinan Ünür
Didn't notice, sorry for that
Arkaitz Jimenez
+8  A: 

perldoc -f fork:

File descriptors (and sometimes locks on those descriptors) are shared, while everything else is copied.

See also Bidirectional Communication with Yourself in perldoc perlipc.

Update: On second thought, do you want something like this?

#!/usr/bin/perl

use strict;
use warnings;

my $pid = fork;

die "Cannot fork: $!" unless defined $pid;

if ($pid == 0) {
    print "Child start\n";
    my $end;
    local $SIG{HUP} = sub { $end = 1 };

    until ($end) {
        print "Sleep 1\n";
        sleep 1;
    }
    function2();
}
else {
    print "Parent start\n";
    sleep 5;
    kill HUP => $pid;
    waitpid($pid, 0);
}

sub function2 {
    print "END\n";
}

Output:

C:\Temp> w
Parent start
Child start
Sleep 1
Sleep 1
Sleep 1
Sleep 1
Sleep 1
END
Sinan Ünür
A: 

I believe you want to use threads; processes are not threads (although at one point in time Linux threads were implemented using a special type of process which shared memory with its parent).

Chris Jester-Young
Note that by default all variables in perl threads are thread-local - see perldoc threads::shared for more information
bdonlan
I very much doubt that. The use cases for perl threads are few and far between.
ysth
@ysth And sharing data between parallel tasks is one of them.
Schwern
@Schwern: yes, is more convenient with threads, but it's a leap to go from there to "you want to use threads".
ysth
@ysth: Eh, what I meant was, "given what you say you want to do, this is probably a good way to go". Not necessarily saying that people should use threads willy-nilly, or anything like that.
Chris Jester-Young
+1  A: 

If you really want to share state among multiple processes using an interface that superficially looks like read/write access to variables, you may want to have a look at IPC::Shareable.

hillu
A: 

You have several options. Threads, sockets, IPC and writing to a file with file locking. Personally I'd recommend threads, they're very easy and safe in Perl, most installations have them compiled and they're fairly performant once a thread has been created.

An interesting alternative is the forks module which emulates threads using a combination of fork() and sockets. I've never used it myself but Elizabeth Mattijsen knows her threads.

Schwern