File: test_read_select.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 (43 lines) | stat: -rwxr-xr-x 917 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
#!/usr/bin/perl
# Test code for the evaluation of threads efficiency
# This version uses only a single select to read the result of the remote execution
# of several 'ls' commands
# This is the best performing of the three versions

use strict;
use IO::Select;

my $num_commands = 64;
my $select = IO::Select->new;
my $i;
for ($i=0; $i<$num_commands; $i++)
  {
    my $fd;
    open $fd,"ls|" or die $!;
    $select->add($fd);
  }
while ($i)
  {
    my @select_result = $select->can_read;
    while (scalar(@select_result))
      {
        my $buffer;
        my $fd = shift @select_result;
        my $result = sysread($fd, $buffer, 4096);
        if ($result > 0)
          {
            print $buffer;
          }
        elsif ($result == 0)
          {
            $select->remove($fd);
            close($fd);
            $i--;
          }
        else
          {
            die $!;
          }
      }
  }