Current location: Hot Scripts Forums » Programming Languages » Perl » Multi threading


Multi threading

Reply
  #1 (permalink)  
Old 04-06-09, 03:58 AM
sandy1028 sandy1028 is offline
Newbie Coder
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Multi threading

I am using this code to create the threads using fork. This will read all the files in a directory and writes to a log file. Now this code creates th elog file. But some of the files are missed due to overlapping. How can I avoid this using locks

Code:
#!/usr/bin/perl
use Parallel::ForkManager;

my $processor = shift;
$tc=5; # threads
$fc = 100; # splits.. each thread should process 100 at a time

my $pm = new Parallel::ForkManager($tc+1);
    $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; $tmpFiles[$ident] = undef; } );
    foreach my $i (0..$#tmpFiles) {
      # Forks and returns the pid for the child:
      my $pid = $pm->start($i) and next;

      $SIG{INT} = 'DEFAULT';
      my $filename = $tmpFiles[$i]->filename();
      my $file = IO::File->new("<$filename") or die "Can't open $filename\n";
      while((my $line) = $file->getline()) {
        last unless defined($line);
        chomp $line;
        my ($dir, $file) = split(/\t/, $line);
        $processor->($dir, $file, $config, $log);
      }
      $pm->finish; # Terminates the child process
}
How to lock the file writer ?.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating multi lingual project nitesh98 ASP.NET 2 08-13-08 05:35 PM
[SOLVED] PHP Storing multi Dimensional array data in MySQL scott2500uk PHP 3 04-11-08 04:17 AM
Make multi page Hamed PHP 4 07-29-07 06:35 AM
multi forum need help mybb Hamed PHP 0 05-04-07 02:30 AM
sebflipper's Multi Pro Host phpBB dharprog PHP 0 11-12-06 12:34 PM


All times are GMT -5. The time now is 04:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.