Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to: create/use/modify shared variable in Perl 5.6.1

Reply
Thread Tools

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

 
 
kath
Guest
Posts: n/a
 
      11-14-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
global variable is shared in shared library ankugoe7@gmail.com C Programming 1 07-15-2008 04:26 PM
Shared Public Variables and Shared Methods Joe Fallon ASP .Net 3 07-16-2004 07:11 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Shared Variable Vs. Session Variable John Kraft ASP .Net 2 10-21-2003 07:56 PM



Advertisments