how to: create/use/modify shared variable in Perl 5.6.1
am 14.11.2007 08:33:24 von kathHello group,
Following is the workarround to implement multithread in Perl 5.6.1
1. i loop through a hash(for eg.) to check existence of the file
2. if the file does not exists
2.1 make an entry in SHARED variable
mentioning that a process has been spawned to create this perticular
file(eg. bangalore.xml)
2.2 spawn a process to create a file(for eg.
bangalore.xml)
2.2.1 in the child process
create a file
2.2.2 after creating file,
make a note in SHARED variable that child is exiting after creating a
file
if file exists
close file-handle
NOTE: Parent should not wait for child to finish its job, because we
have more one file(location) to look up. And we cannot wait for the
one child creating bangalore.xml to finish, to spawn a child to create
sofia.xml file
Where im stuck?
I don't know to use create and use shared variable in Perl :
(
Can any one help me here pls.
here is the code.
#---- SBREnviron.pl -------
package SBREnviron;
%LOCATIONS = (
'bangalore' => {
'xml_file' => q(bangalore.xml)
},
'sofia' => {
'xml_file' => q(sofia.xml)
},
#-----------------
# -------- Fork.pl -------------
require 'SBREnviron.pl';
use FileHandle;
%process_track= ();
$foo=1000;
while($foo--){
while(my ($k, $v) = each %SBREnviron::LOCATIONS){
my $fh = new FileHandle;
my $dbh;
eval{
print "\nPARENT($foo): open_file: trying to open
$SBREnviron::LOCATIONS{$k}->{'xml_file'} ";
open ($fh, $SBREnviron::LOCATIONS{$k}->{'xml_file'}) or die"ERROR:
$!";
};
#if ($@ =~ /ERROR: file does not exists/){
if ( ($@ =~ /ERROR:/)){
print "\nPARENT($foo): file_exists: NO";
print "\n\tPARENT($foo): checking for spawned process";
if (not exists $process_track{$k}){
print "\n\tPARENT: isSpawned: NO";
#print "\n\tPARENT($foo): no process is spawned for $k, so im
going to spawn a child to create $SBREnviron::LOCATIONS{$k}-
>{'xml_file'} file.";
# make a note that a process has spawn for this already
$process_track{$k} = 1;
print "\nPARENT: shared_var(so called): $process_track{$k}";
my $pid;
$pid = fork();
if ($pid == 0){
# child process
print "\n\t---\n\tCHILD:im the child, and will create
$SBREnviron::LOCATIONS{$k}->{'xml_file'} file in a short while\t\t---
";
sleep 15;
# just a create a file
my $fh = new FileHandle;
open ($fh, ">$SBREnviron::LOCATIONS{$k}->{'xml_file'}");
close($fh);
# delete the entry from hash, representing the child process has
finished
print "\n\t---\n\tCHILD: im deleting an entry $k\t\t---";
delete $process_track{$k};
print "\nPARENT: shared_var(so called): $process_track{$k}" if
(exists $proces_track{$k});
#process will exit once it is finished.
exit(0);
} # end of child process
}else{
print "\n\tPARENT: isSpawned: YES";#print "\nPARENT($foo): a
process is already running for this $k";
print "\nPARENT: shared_var(so called): $process_track{$k}";
}
} # end of if-file-doesnot-exists
else{
close($fh);
print "\n PARENT($foo): $SBREnviron::LOCATIONS{$k}->{'xml_file'}
exists, so closing the filehandle"};
} # end of loop through locations
sleep(2);
print "\n\n";
}# end of infinite loop
Thanks and regards,
kath.