how to: create/use/modify shared variable in Perl 5.6.1

K

kath

Hello 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top