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

Discussion in 'Perl Misc' started by kath, Nov 14, 2007.

  1. kath

    kath Guest

    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.
     
    kath, Nov 14, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kraft

    Shared Variable Vs. Session Variable

    John Kraft, Oct 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,896
    Cowboy \(Gregory A. Beamer\)
    Oct 21, 2003
  2. DJ Dev
    Replies:
    3
    Views:
    16,169
    Gandalf
    Feb 8, 2004
  3. Joe Fallon
    Replies:
    3
    Views:
    776
    =?Utf-8?B?Z3V5?=
    Jul 16, 2004
  4. Immortal Nephi
    Replies:
    2
    Views:
    587
    Eric Pruneau
    Jul 1, 2008
  5. Replies:
    1
    Views:
    1,296
    santosh
    Jul 15, 2008
Loading...

Share This Page