Both perl and C languages provide message queue mechanisms, and message queues can be compatible with each other. when the system architecture is involved, the core computing module can be implemented in C language, and the business logic layer can be implemented in Perl, and the two layers can communicate between processes in different languages through message queue. this article uses two sample programs to explain how Perl implements message queues. Another example program explains how to use message queues between Perl and C for inter-process communication. i. use the IPC: SysV and IPC: Msg modules to access the Unix message queue. Example 1: access to the message queue in a single process (Perl implementation) File: msg_single_process.pl1. #! /Usr/bin/perl2. 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 (); Code explanation: 4. using the IPC: SysV module, qw (IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT IPC_NOWAIT) is equivalent to ("IPC_PRIVATE S_IRWXU S_IRWXG S_IRWXO IPC_CREAT regular"), which indicates that this program will introduce several symbols from, you need to specify the module name in the subsequent code to use these symbols. 5. use IPC: Msg module (Message Queue); 6. define private variables. 7. use ftok To Get A key_t value through the file name. The current directory is used here. similar to the ftok function in C language, this file must actually exist; 8. obtain the Msg object. Similar to the msgget function; 9. Specify the message type; 10. Obtain the Message Queue identifier; 12. Send a message. Similar to the msgsnd function; 13. receive messages. Similar to the msgrcv function; 15. delete a message queue. For more information, see IPC: Msg module definition: http://search.cpan.org/~mhx/IPC-SysV-2.03/lib/IPC/Msg.pm Example 2. Use message queue for communication between two processes (Perl implementation) File: msg_snd.pl #! /Usr/bin/perluse 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', 'pet', '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/perluse 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 (); 2. the receiving End of message queue communication between C and Perl processes uses Perl to implement msg_rcv.pl #! /Usr/bin/perluse 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, 1024) or die" receive message failed: $! "; Print" BUFFER: ", $ buf," \ n "; my $ pos = index ($ buf," end ", 0); print" POS :", $ pos, "\ n"; if ($ pos! =-1) {$ running = 0 ;}$ msg-> remove (); the sender uses C code to implement msg_snd.c # include <stdio. h> # include <string. h> # include <errno. h> # include <unistd. h> # include <sys/msg. h> # define MAX_TEXT 512 struct my_msg_st {long int my_msg_type; char some_text [MAX_TEXT] ;}; int main () {int index; struct my_msg_st some_data; int msgid; msgid = msgget (key_t) 1234,066 6 | IPC_CREAT); if (msgid =-1) {fprintf (stderr, "msgget failed With error: % d \ n ", errno); exit (EXIT_FAILURE);} for (index = 0; index <10; index ++) {memset (some_data.some_text, 0, MAX_TEXT); some_data.my_msg_type = 1; snprintf (some_data.some_text, MAX_TEXT, "% s ~ % D "," index ", index); if (msgsnd (msgid, (void *) & some_data, MAX_TEXT, 0) =-1) {fprintf (stderr, "msgsnd failed \ n"); exit (EXIT_FAILURE) ;}/ * ending */memset (latency, 0, MAX_TEXT); latency = 1; snprintf (some_data.some_text, MAX_TEXT, "% s", "end"); if (msgsnd (msgid, (void *) & some_data, MAX_TEXT, 0) =-1) {fprintf (stderr, "msgsnd failed \ n"); exit (EXIT_FAILURE);} exit (EXIT _ SUCCESS);} compile and run: $ gcc-g-Wall-o msg_snd msg_snd_c.c $./msg_rcv.pl & $./msg_snd output: MSG_ID: 393219 BUFFER: index ~ 0 BUFFER: index ~ 1 BUFFER: index ~ 2 BUFFER: index ~ 3 BUFFER: index ~ 4 BUFFER: index ~ 5 BUFFER: index ~ 6 BUFFER: index ~ 7 BUFFER: index ~ 8 BUFFER: index ~ 9 BUFFER: end