at_yasu's blog

ロード的なことを

fifo device

Perl で fifo デバイスを作ったりするメモ書き。

順番に、デバイスにアクセスしてきた者に対して、適当なIDを渡すプログラム。

#!/usr/bin/perl -w
use strict;
use Fcntl;

print "FIFO device test\n";

$ENV{PATH}=qw{/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc:/usr/games};

chdir('/');
my $file = "/home/yasui/perl/fifo/yasui_test";

unless (-p $file) {
        require POSIX;
        POSIX::mkfifo($file, 0666) or die $!;
}

print "create $file\n";

# push
my $id = 0;
while (1) {
sysopen FIFO,$file, O_WRONLY or die $!;
print FIFO "John: ".$id;
close(FIFO);
exit(0) if $id eq 10;
$id++;

select undef,undef,undef,0.2;
}

後は、cat /path/to/device とすれば、IDを渡してくれる。