首页 > 其他 > 详细

perl 消息队列

时间:2019-12-10 12:32:52      阅读:78      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/perl
use IPC::SysV qw(IPC_PRIVATE IPC_CREAT S_IRWXU);
use IPC::Msg;
my $queue = IPC::Msg->new(IPC_PRIVATE, S_IRWXU | IPC_CREAT);
$msgtype = 6;
$msgdata = "hello world!";
my $pid = fork();
if (not defined $pid) {
	print "resources not available.\n";
} elsif ($pid == 0) {
	print "I am the child.\n";
	sleep 3;
	print "I am the child 1.1.\n";
	$queue->snd($msgtype, $msgdata);
	print "I am the child 1.2.\n";
	sleep 2;
	print "I am the child 2.\n";
	exit(0);
} else {
	print "I am the parent 1.\n";
	$type = $queue->rcv($buf, 256);
	print "I am the parent 2.\n";
	print "type => $type.\n";
	$ds = $queue->stat;
	print $buf;
	print $ds;
	print "end pid => $pid.\n";
	$res = $queue->remove;
	print "remove res => $res.\n";
	if (!defined $res) {
		print "hello => $!.\n";
	}
	waitpid($pid, 0);
}

 子进程发送消息,父进程接收消息。其中queue->rcv是阻塞的。

perl 消息队列

原文:https://www.cnblogs.com/longchang/p/12015769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!