Thread Win32 - Pipe erzeugung funktioniert nicht (3 answers)
Opened by Snicker at 2008-10-07 14:18

murphy
 2008-10-07 19:28
#115250 #115250
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
Sollte man in diesem einfachen Fall nicht eigentlich auch mit my $pid = open my $pipe, '-|'; auskommen?

Zum Beispiel so:
Code (perl): (dl )
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
use 5.010;
use strict;
use warnings;

use Symbol qw/gensym/;

sub spawn_worker(&) {
  my ($thunk) = @_;
  
  my $pipe = gensym;
  my $pid = open $pipe, '-|';
  die "Unable to create worker: $!" unless (defined($pid));
  
  if ($pid == 0) {
    $thunk->($pipe);
    exit 0;
  }
  else {
    return ($pid, $pipe);
  }
}

sub collect_work($*) {
  my ($pid, $pipe) = @_;
  my $_;
  
  print "$pid >> $_" while (<$pipe>);
  die "Unable to read from worker $pid: $!" if ($!);
  
  close $pipe or die "Unable to close worker $pid: $!";
}

my %workers = ();
for my $idx (0..10) {
  my ($pid, $pipe) = spawn_worker {
    print "Hello, I'm worker $idx\n";
  };
  
  $workers{$pid} = $pipe;
}

while (my ($pid, $pipe) = each %workers) {
  collect_work $pid, $pipe;
}
When C++ is your hammer, every problem looks like your thumb.

View full thread Win32 - Pipe erzeugung funktioniert nicht