Practice--linux Message Queuing msg for interprocess communication

Source: Internet
Author: User
Tags message queue posix

Continue to insist, perhaps not a deep understanding, but at least to ensure that the impression.


Message Queuing (also called Message Queuing) can overcome some of the drawbacks of early UNIX communication mechanisms. As one of the early UNIX communication mechanisms, the signal can transmit a limited amount of information, and although the POSIX 1003.1b in the real-time signal of the extension, so that the signal in the transmission of information to a considerable degree of improvement, but the signal this way of communication more like "instant" communication mode, It requires the process of receiving the signal to react to the signal within a certain time frame, so that the signal is meaningful for the lifetime of the receiving signal process, and the information transmitted by the signal is close to the concept of continuous process (process-persistent), see appendix 1, pipelines and famous pipelines and famous pipelines are typical with the process of continuous IPC, and can only transmit unformatted byte stream will undoubtedly inconvenience the application development, in addition, its buffer size is also constrained.

A message queue is a linked list of messages. You can think of a message as a record, with a specific format and a specific priority. A process that has write permission to a message queue can add new messages to a certain rule, and a process that has read access to a message queue can read messages from the message queue. Message Queuing is persistent with the kernel (see Appendix 1).

There are currently two main types of Message Queuing: POSIX Message Queuing and System V Message Queuing, which is currently heavily used by the System V message queue. Considering the portability of the program, the newly developed application should use POSIX Message Queuing as much as possible.

In the sequence of this series of topics (deep understanding of the Linux interprocess Communication (IPC)), there are two implementations for Message Queuing, semaphores, and shared memory areas: POSIX and System V. The Linux kernel (kernel 2.4.18) supports POSIX semaphores, POSIX shared memory areas, and POSIX Message Queuing. But for one of the major Linux distributions, redhad8.0 (kernel 2.4.18), there is no support for the POSIX interprocess communication API, but it should only be a matter of time.


/***************** * * test.c* *******************/#include<sys/msg.h>#include<sys/types.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>voidMsg_stat (int,structmsqid_ds); main () {intgflags, Sflags, rflags;    key_t key; intMsgId; intReval; structmsgsbuf{intMtype; Charmtext[1];    }msg_sbuf; structmsgmbuf{intMtype; Charmtext[Ten];    }msg_rbuf; structMsqid_ds Msg_ginfo, Msg_sinfo; Char* Msgpath ="/tmp/msgqueue"; Key=ftok (Msgpath,'a'); GFlags= ipc_creat|ipc_excl; MsgId= Msgget (Key, gflags|00666); if(MsgId = =-1) {printf ("msg Create error\n"); return;    } msg_stat (MsgId, msg_ginfo); Sflags=ipc_nowait; Msg_sbuf.mtype=Ten; msg_sbuf.mtext[0] ='a'; Reval= Msgsnd (MsgId, &msg_sbuf,sizeof(MSG_SBUF), sflags); if(Reval = =-1) {printf ("message Send error\n");    } msg_stat (MsgId, msg_ginfo); Rflags= ipc_nowait|Msg_noerror; Reval= MSGRCV (MsgId, &msg_rbuf,4,Ten, Rflags); if(Reval = =-1) printf ("Read MSG error\n"); Elseprintf ("read from msg queue%d bytes\n", Reval);    Msg_stat (MsgId, msg_ginfo); Msg_sinfo.msg_perm.uid=8; Msg_sinfo.msg_perm.gid=8; Msg_sinfo.msg_qbytes=16388; Reval= Msgctl (MsgId, Ipc_set, &msg_sinfo); if(Reval = =-1) {printf ("msg Set Info error\n"); return;    } msg_stat (MsgId, msg_ginfo); Reval=Msgctl (MsgId, Ipc_rmid, NULL); if(Reval = =-1) {printf ("unlink msg Queue error\n"); return; }}voidMsg_stat (intMsgIdstructMsqid_ds Msg_info) {    intReval; Sleep (1); Reval= Msgctl (MsgId, Ipc_stat, &msg_info); if(Reval = =-1) {printf ("get msg Info error\n"); return; } printf ("\ n"); printf ("Current number of bytes in queue is%d\n", msg_info.msg_cbytes); printf ("Number of messages in queue is%d\n", Msg_info.msg_qnum); printf ("Max number of bytes on queue is%d\n", msg_info.msg_qbytes); printf ("pid of last msgsnd is%d\n", Msg_info.msg_lspid); printf ("pid of last Msgrcv is%d\n", Msg_info.msg_lrpid); printf ("Last msgsnd time is %s", CTime (&(Msg_info.msg_stime))); printf ("Last MSGRCV time is %s", CTime (&(Msg_info.msg_rtime))); printf ("Last change time is %s", CTime (&(Msg_info.msg_ctime))); printf ("msg uid is%d\n", Msg_info.msg_perm.uid); printf ("msg GID is%d\n", Msg_info.msg_perm.gid);}

Practice--linux Message Queuing msg for interprocess communication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.