用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - perl代码库

perl 命名管道使过程看起来像一个文件

2012-10-14 作者: 神马举报

[perl]代码库

#-----------------------------
#% mkfifo /path/to/named.pipe
#-----------------------------
open ( FIFO, "< /path/to/named.pipe" )         or die $!;
while ( <FIFO> )
{
	print "Got: $_";
}
close ( FIFO );
#-----------------------------
open ( FIFO, "> /path/to/named.pipe" )         or die $!;
print FIFO "Smoke this.\n";
close ( FIFO );
#-----------------------------
#% mkfifo ~/.plan                    # isn't this everywhere yet?
#% mknod  ~/.plan p                  # in case you don't have mkfifo
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# dateplan - place current date and time in .plan file
while ( 1 )
{
	open ( FIFO, "> $ENV{HOME}/.plan" )
	or die "Couldn't open $ENV{HOME}/.plan for writing: $!\n";
	print FIFO "The current time is ", scalar ( localtime ), "\n";
	close FIFO;
	sleep 1;
}

#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# fifolog - read and record log msgs from fifo

use IO::File;

$SIG {ALRM} = sub { close ( FIFO ) };
# move on to the next queued process

while ( 1 )
{
	alarm ( 0 );
# turn off alarm for blocking open
	open ( FIFO, "< /tmp/log" )        or die "Can't open /tmp/log : $!\n";
	alarm ( 1 );
# you have 1 second to log

	$service = <FIFO>;
	next unless defined $service;
# interrupted or nothing logged
	chomp $service;

	$message = <FIFO>;
	next unless defined $message;
# interrupted or nothing logged
	chomp $message;

	alarm ( 0 );
# turn off alarms for message processing

	if ( $service eq "http" )
	{
# ignoring
	}
	elsif ( $service eq "login" )
	{
# log to /var/log/login
		if ( open ( LOG, ">> /tmp/login" ) )
		{
			print LOG scalar ( localtime ), " $service $message\n";
			close ( LOG );
		}
		else
		{
			warn "Couldn't log $service $message to /var/log/login : $!\n";
		}
	}
}

#-----------------------------
use POSIX qw ( :errno_h );

$SIG {PIPE} = 'IGNORE';
# ...
$status = print FIFO "Are you there?\n";
if ( !$status && $! == EPIPE )
{
	warn "My reader has forsaken me!\n";
	next;
}
#-----------------------------
use POSIX;
print _POSIX_PIPE_BUF, "\n";
#-----------------------------


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...