ajeet@perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl monks.. I have a problem, i want to share my data among threads, which are created using forks. I tried by passing references and changing them, hoping that changes will be reflected in all the threads...but didn't work. Please give some idea, about it. I am working Solaris System, without forks.pm module. Thanks in Advance

Replies are listed 'Best First'.
Re: Sharing in fork
by ikegami (Patriarch) on Mar 23, 2010 at 06:33 UTC

    i want to share my data among threads, which are created using forks.

    Which one is it? Are you using threads or forked processes? I'm guessing the latter. Each process has its own memory space, so changes to a variable in one process isn't going to change the anything in another process. You need to setup a pipe, some shared memory or some other communication channel between the processes if you want to communicate between them. And maybe use a higher level interface over that channel (e.g. Cache::Mmap, forks, etc).

      Thank You Sir for your feedback Right.. I am talking about the latter one. Problem scenario is something like this. A main process will create a queue,initialize head and tail. Now it will fork twice and create two other process. one of them will be responsible for enqueing and other is for dequeing, I had chosen fork to do this, i tried manipulating using reference. So, Sir please suggest a way through which the queue can be shared among both forked process and changes made by one process should be visible by other process. I am stuck at this, I dont want to use thread, for that i need to re-install my perl interpreter,(that i can not do).

      Waiting for your reply...

        I dont want to use thread, for that i need to re-install my perl interpreter

        Then use forks and forks::shared, which uses sockets for inter-process communication.  Believe it, there is no direct way to share variables between child and parent, because each process has its own address space.

        A reply falls below the community's threshold of quality. You may see it by logging in.
      Sir, I want to know one thing that, will the installation of forks.pm will affect my base installation of perl?If yes the can i uninstall the package later? I searched net, but does not found any information, which provides information about this...

        What does affect mean here? It's a module. If you don't use it, it doesn't do anything.

        I don't know what's available as tools for uninstalling. It's not something one needs to do, really. Hand removing the .pm and .so/.dll files should be simple enough. You also have to option of installing it to a private directory you can blow away at will.

Re: Sharing in fork
by 7stud (Deacon) on Mar 23, 2010 at 08:28 UTC

    perl's threads are not like other language's threads; they operate much like processes in that they don't share variables. Here's a thread example:

    use strict; use warnings; use 5.010; use threads; use threads::shared; my $val :shared = 10; my $other_val = 'hello'; sub do_stuff { my $thr_id = shift; sleep int(rand 3); $other_val .= ' world'; { lock $val; #other threads cannot access $val until after #this block exits say "thread$thr_id sees:"; say "\t\$val = $val"; say "\t\$other_val = $other_val"; $val += 2; } } for (1 .. 10) { threads->new(\&do_stuff, $_); } for my $thr ( threads->list() ) { $thr->join(); } --output:-- thread4 sees: $val = 10 $other_val = hello world thread6 sees: $val = 12 $other_val = hello world thread1 sees: $val = 14 $other_val = hello world thread3 sees: $val = 16 $other_val = hello world thread5 sees: $val = 18 $other_val = hello world thread8 sees: $val = 20 $other_val = hello world thread10 sees: $val = 22 $other_val = hello world thread2 sees: $val = 24 $other_val = hello world thread7 sees: $val = 26 $other_val = hello world thread9 sees: $val = 28 $other_val = hello world
    See perthrtut for more info. Or see perlipc for other ideas.
      Actually, I dont want to rebuild my perl interpreter, that's why i decided to go with fork. Now, is it possible to get the data shared without installing forks.pm module??

        There are lots of ways to communicate between two processes. See perlipc. I prefer to use a database.

        There is no way to actually share data between two processes in Perl. You can try something like MMap if you want to make mostly static data available.