File: test_read_threads.pl

package info (click to toggle)
taktuk 3.7.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,252 kB
  • sloc: perl: 6,715; ansic: 1,211; makefile: 188; sh: 161
file content (49 lines) | stat: -rwxr-xr-x 899 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/perl
# Test code for the evaluation of threads efficiency
# This version uses only threads to read the result of the remote execution
# of several 'ls' commands
# This is the worst performing

use strict;
use threads;
use threads::shared;

my $num_commands = 100;
my $count : shared = 0;

sub thread_func()
{
  my $fd;
  open $fd,"ls|";
  lock($count);
  $count++;
  print "Opened $count\n";
  if ($count < $num_commands)
    {
      cond_wait($count);
    }
  else
    {
      cond_broadcast($count);
    }
  my $buffer;
  my $result = sysread($fd,$buffer,4096);
  while ($result > 0)
    {
      print $buffer;
      $result = sysread($fd, $buffer, 4096);
    }
  die $! if ($result < 0);
  close $fd;
}

my $i;
my @threads;
for ($i=0; $i<$num_commands; $i++)
  {
    $threads[$i] = threads->create("thread_func");
  }
for ($i=0; $i<$num_commands; $i++)
  {
    $threads[$i]->join;
  }