Debian Codegraph @

eg/abuse/blocking_writes

raw
1 #!/usr/bin/perl
2
3 ## Submitted by Borislav Deianov <[email protected]>
4 ## This stresses the blocking write to see if it blocks.
5
6 use Fcntl;
7 use IO::Pty;
8 use IPC::Run qw(run);
9
10 sub makecmd {
11 return [
12 'perl', '-e',
13 '<STDIN>, print "\n" x ' . $_[0] . '; while(<STDIN>){last if /end/}'
14 ];
15 }
16
17 pipe R, W;
18 fcntl( W, F_SETFL, O_NONBLOCK );
19 while ( syswrite( W, "\n", 1 ) ) { $pipebuf++ }
20 print "pipe buffer size is $pipebuf\n";
21 $in = "\n" x ( $pipebuf * 3 ) . "end\n";
22
23 print "reading from scalar via pipe... ";
24 run( makecmd( $pipebuf * 3 ), '<', \$in, '>', \$out );
25 print "done\n";
26
27 print "reading from code via pipe... ";
28 run( makecmd( $pipebuf * 3 ), '<', sub { $t = $in; undef $in; $t }, '>', \$out );
29 print "done\n";
30
31 $pty = IO::Pty->new();
32 $pty->blocking(0);
33 $slave = $pty->slave();
34 while ( $pty->syswrite( "\n", 1 ) ) { $ptybuf++ }
35 print "pty buffer size is $ptybuf\n";
36 $in = "\n" x ( $ptybuf * 3 ) . "end\n";
37
38 print "reading via pty... ";
39 run( makecmd( $ptybuf * 3 ), '<pty<', \$in, '>', \$out );
40 print "done\n";