Hi

I've been playing on and off with the system call select (3) (http://linux.die.net/man/3/select) in order to learn a bit Perl 6 and NativeCall. But I get no traction, so I'd like to know what I'm doing wrong.

Before I start I'd like to wonder a little about the fd usage of moar. Perl 5 has three open fd's, moar has 13[1]. I wonder if they're all necessary.

Calling select (3) seems straight forward, but I can't seem to get it right.

First, checking with a Perl 5 program [3], you'll see that fd 15 sets the last bit in a two-byte "word".

Variations over [2] give inconclusive results, so I know I'm doing something (probably very basic) wrong. But what?

The current incancation tries to dup the STDIN fd to 14, which should be the second last bit in the 2nd uint8. But I've tried to mask in many other ways, with no better result.

Anyone who can spot the obvious?

/kaare

[1] Output from ls -la /proc/<id>/fd:

dr-x------ 2 kaare users  0 30 apr 10:25 ./
dr-xr-xr-x 9 kaare users  0 30 apr 10:25 ../
lrwx------ 1 kaare users 64 30 apr 10:25 0 -> /dev/tty
lrwx------ 1 kaare users 64 30 apr 10:25 1 -> /dev/tty
lr-x------ 1 kaare users 64 30 apr 10:25 10 -> /dev/null
lrwx------ 1 kaare users 64 30 apr 10:25 11 -> /dev/tty
lrwx------ 1 kaare users 64 30 apr 10:25 12 -> /dev/tty
lrwx------ 1 kaare users 64 30 apr 10:25 13 -> /home/kaare/devel/perl6/test/lib/.precomp/.lock
lrwx------ 1 kaare users 64 30 apr 10:25 2 -> /dev/tty
lr-x------ 1 kaare users 64 30 apr 10:25 3 -> pipe:[1744938]
l-wx------ 1 kaare users 64 30 apr 10:25 4 -> pipe:[1744938]
lrwx------ 1 kaare users 64 30 apr 10:25 5 -> anon_inode:[eventpoll]
lr-x------ 1 kaare users 64 30 apr 10:25 6 -> pipe:[1744939]
l-wx------ 1 kaare users 64 30 apr 10:25 7 -> pipe:[1744939]
lrwx------ 1 kaare users 64 30 apr 10:25 8 -> anon_inode:[eventfd]
lrwx------ 1 kaare users 64 30 apr 10:25 9 -> /dev/tty

[2] Perl 6:

use NativeCall;

class Timeval is repr('CStruct') {
    has int64 $.seconds;
    has int64 $.microseconds;
}

sub select(int32, CArray[uint8], Pointer, Pointer, Timeval) is native returns int { * }

sub dup(int32 --> int32) is native { * };
sub dup2(int32, int32 --> int32) is native { * };

my $nfds = dup(0);
warn $nfds;
my $readfds = CArray[uint8].new(0, 2);
my $timeout = 5;
my $tv = Timeval.new(seconds => $timeout.Int, microseconds => 0);
warn select($nfds, $readfds, Pointer, Pointer, $tv);

[3] Perl 5:
#!/usr/bin/env perl
use strict;
use warnings;
use POSIX qw/dup2/;
my $new = dup2(0, 15);
warn $new;
my $vector = "";
vec($vector, $new, 1) = 1;
my $bits = unpack("b*", $vector);
warn $bits;
my $f = select($vector, undef, undef, 5);
warn $f;

#!/usr/bin/env perl

use strict;
use warnings;
use POSIX qw/dup2/;

my $new = dup2(0, 15);
warn $new;
my $vector = "";
vec($vector, $new, 1) = 1;
my $bits = unpack("b*", $vector);
warn $bits;
my $f = select($vector, undef, undef, 5);
warn $f;

Reply via email to