码迷,mamicode.com
首页 > 其他好文 > 详细

perl 消息队列

时间:2019-12-10 13:02:06      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:==   wait   child   type   bsp   private   gty   nbsp   发送消息   

#!/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 消息队列

标签:==   wait   child   type   bsp   private   gty   nbsp   发送消息   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!