前言:
perl和
C语言都提供了消息队列的机制,而且这两种语言中的消息队列可以相互兼容.
可以用
C语言实现核心计算模块,用Perl实现业务逻辑层,
而这两层间可以通过消息队列来实现不同语言的进程间的通信.
本文先用两个示例程序讲解Perl对消息队列的实现。
再一个示例程序讲解Perl与C之间使用消息队列进行进程间的通信.
一. 使用IPC::SysV和IPC::Msg模块访问Unix消息队列
例一, 单个进程中实现消息队列的访问(Perl实现)
File: msg_single_process.pl
1. #!/usr/bin/perl
2. use strict;
3. use warnings;
4. use IPC::SysV qw(IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT IPC_NOWAIT);
5. use IPC::Msg;
6. my($key, $msg, $msgid, $msgtype, $buf);
7. $key = IPC::SysV::ftok(".",'1');
8. $msg = new IPC::Msg($key, 0666 | IPC_CREAT) or die "create message queue: $!";
9. $msgtype = 1;
10.$msgid = $msg->id();
11.print "MSG_ID: ",$msgid,"\n";
12.$msg->snd($msgtype, "test", IPC_NOWAIT) or die "send message failed: $!";
13.$msg->rcv($buf, 1024) or die "receive message failed: $!";
14.print "BUFFER: ",$buf,"\n";
15.$msg->remove();
代码解释:
4.使用IPC::SysV模块,
qw(IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT IPC_NOWAIT)
等同于("IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT IPC_NOWAIT"),
表示本程序将从该模块引入若干符号,
在后续的代码里面需要要指定模块名就可以使用这些符号;
5.使用IPC::Msg模块(消息队列);
6.定义私有变量;
7.用ftok通过文件名获得一个key_t值,这里使用是当前目录.
类似C语言中的ftok函数,这个文件必须实际存在;
8.获得Msg对象。类似msgget函数;
9.指定消息类型;
10.获得消息队列的标识符;
12.发送消息。类似msgsnd函数;
13.接收消息。类似msgrcv函数;
15.删除消息队列;
更多的信息,可以参考IPC::Msg模块定义:
http://search.cpan.org/~mhx/IPC-SysV-2.03/lib/IPC/Msg.pm
例二.在两个进程间使用消息队列进行通信(Perl实现)
File: msg_snd.pl
#!/usr/bin/perl
use strict;
use warnings;
use IPC::SysV qw(IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT IPC_NOWAIT);
use IPC::Msg;
my($key, $msg, $msgid, $msgtype, $buf);
$key = IPC::SysV::ftok(".",'1');
$msg = new IPC::Msg($key, 0666 | IPC_CREAT) or die "create message queue: $!";
$msgtype = 1;
$msgid = $msg->id();
print "MSG_ID: ",$msgid,"\n";
my $pal;
foreach $pal('Tom','Dick','Harry','Pete','Hank')
{
$msg->snd($msgtype, "Hi, $pal", IPC_NOWAIT) or die "send message failed: $!";
print "BUFFER: ",$pal,"\n";
}
# end
$msg->snd($msgtype, "end", IPC_NOWAIT) or die "send message failed: $!";
print "BUFFER: end\n";
File: msg_rcv.pl
#!/usr/bin/perl
use strict;
use warnings;
use IPC::SysV qw(IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT IPC_NOWAIT);
use IPC::Msg;
my($key, $msg, $msgid, $msgtype, $buf);
$key = IPC::SysV::ftok(".",'1');
$msg = new IPC::Msg($key, 0666 | IPC_CREAT) or die "create message queue: $!";
$msgtype = 1;
$msgid = $msg->id();
print "MSG_ID: ",$msgid,"\n";
my $running = 1;
while ($running)
{
$msg->rcv($buf, 1024) or die "receive message failed: $!";
print "BUFFER: ",$buf,"\n";
if($buf eq "end")
{
$running = 0;
}
}
$msg->remove();
二. C与Perl进程间的消息队列通信
接收端使用Perl实现
msg_rcv.pl
#!/usr/bin/perl
use strict;
use warnings;
use IPC::SysV qw(IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT IPC_NOWAIT);
use IPC::Msg;
my($key, $msg, $msgid, $msgtype, $buf);
#$key = IPC::SysV::ftok(".",'1');
$key = 1234;
$msg = new IPC::Msg($key, 0666 | IPC_CREAT) or die "create message queue: $!";
$msgtype = 1;
my $running = 1;
while ($running)
{
$msg->rcv($buf, 10