To:   [EMAIL PROTECTED]
cc:

Subject:  Win32::Semaphore

Hello,

I'm trying to get some synchronisation working between several child
processes, using a counting semaphore.
Probably I'm doing something wrong, because I get errors when releasing the
semaphore, stating that the handle is invalid.
Because the release works the first time, I think I must do something with
the inherited filehandles.

Any help???

==============================================================================================
use strict;
use Win32::Semaphore;

use vars qw($sem);

$SIG{CHLD} = sub {wait()};

sub test_sem();

exit test_sem();


sub test_sem() {
     my ($pid, $initial, $maximum, $name, $wksta) = (0,5,5,'SEINPAAL','');
     my ($sec,$min,$hour);

#    Create counting semaphore for a maximum of  5 outstanding childs
#    each child decrements count with 1 at exit, enabling parent to spawn another
     $sem = Win32::Semaphore->new($initial,$maximum,$name);


     for (my $i = 1; $i < 25; $i++)  {
          $sem->wait(); #wait for semaphore to become available
          print "Spawning child for $i\n";
          $pid = fork;
          if ($pid == 0 ) { # Child
               die "cannot fork: $!" unless defined $pid;
               $sem->open($name) or die "Cannot open semaphore $!";
               ($sec,$min,$hour,) = localtime(time);
               print sprintf("Child %d starts on %02i:%02i:%02i\n",$$, $hour, $min, 
$sec);
               for (my $j = 0; $j < 100000; $j++)  {
                         # Do some work
                         my $k = $j / 2
               }
               ($sec,$min,$hour,) = localtime(time);
               print sprintf("Child %d ends on %02i:%02i:%02i\n",$$, $hour, $min, 
$sec);
               my ($inc, $prev) = (1,999);
               $sem->release($inc,$prev) or die "Could not release semaphore : $^E";
               print "Child $$ released on $prev\n";
               exit
          }
     }
}
==============================================================================================

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to