Thread Fork und Output Flush (4 answers)
Opened by ms at 2009-04-04 20:29

ms
 2009-04-04 21:24
#120295 #120295
User since
2009-04-04
3 Artikel
BenutzerIn
[default_avatar]
Interessant, cmd.exe gibt mir nun in 1:5 Faellen die Ausgabe, die ich konstant unter MSYS beobachten kann.
Ich glaube, ich muss mir mal Informationen ueber die Pufferung unter verschiedenen Shells zusammengooglen.
Nicht, dass die Vorhersagbarkeit der Ausgabe wichtig ist (momentan jedenfalls) - aber mich interessiert die Ursache des unterschiedlichen Verhaltens.

MSYS Output
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
BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
one two three four five

BEFORE FOR BRACKET
AFTER FOR BRACKET
one two three four five




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
#!/usr/bin/env perl -w
$| = 1;
@array = qw(one two three four five);

$num = "10";

for ( 1 .. $num ) {
        my $pid = fork();
        if ($pid) {

                # parent
                push( @childs, $pid );
        }
        elsif ( $pid == 0 ) {

                # child
                print "@array\n\n";
                sleep 3;
                exit(0);
        }
        else {
                die "couldn’t fork: $!\n";
        }
        print "BEFORE FOR BRACKET\n";
}

print "AFTER FOR BRACKET\n";

foreach (@childs) {
        waitpid( $_, 0 );
}

exit(0);
__END__

View full thread Fork und Output Flush