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).
| [reply] |
|
|
| [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.
| [reply] |
|
|
|
|
|
|
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...
| [reply] |
|
|
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.
| [reply] |
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. | [reply] [d/l] |
|
|
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??
| [reply] |
|
|
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.
| [reply] |