perl 消息队列

#!/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是阻塞的。

查看消息队列: ipcs

清除消息队列:ipcrm -q <msgid>

posted @ 2019-12-10 11:47  kissrule  阅读(318)  评论(0编辑  收藏  举报