interprocess Communication-Queues

Source: Internet
Author: User
Tags message queue sprintf



Message Queuing
Message Queuing is referenced by identity, and Message Queuing resembles a list of messages, referenced by a queue identifier, and identified by key to obtain Msgget (key_t key, int flag).


To create a queue procedure:
1. Determine if the key,key of the queue can be customized, or you can use Ftok to get a key
① use Ftok to get a key value Ftok (the const char * path, int id) path must be an existing file path, otherwise it will always be blocked where the ID value range 0-255
②key Custom, then put the key value in the common header file, convenient to include the header file of other files to use
③key a custom, and then put the key in a file, different processes can access the file by accessing the key
2. Create a message queue with key and get the queue ID
3. Send data msgsnd or read data Msgrcv to the queue for communication


Reading the data in the message queue is either FIFO or randomly read according to the set read data type
The process that created the message queue ends, but the message queue and the data in the queue still exist, unless the deletion is displayed:
1. Read or delete message queues by other processes
2. deleted by the system


function Msgget
Msgget-get a message Queue identifier
Get Message Queue identity

Functions Overview
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

int Msgget (key_t key, int msgflg);

Returns the identity of the queue associated with key, if the value of key is Ipc_private or the value of key is not equal to the key of the other existing queue, and MSGFLG specifies Ipc_creat, a new queue is created

If MSGFLG is set to ipc_creat| IPC_EXCL, if the corresponding key already exists in the queue that already exists, the creation fails and errno is eexist.

When creating the queue, the parameter msgflg the least significant bit is the definition of the queue permissions, these permission bits and the open parameter mode usage, except the executable permission can not be used
If a queue is created, then the structure associated with it is msqid_ds initialized:
Msg_perm.cuid and msg_perm.uid the user ID of the process that will be set as the reference queue

Msg_perm.cgid and msg_perm.gid the group ID of the process that is set to refer to the queue

Msg_perm.mode Low 9-bit and MSGFLG low 9-bit correspondence

Msg_qnum, Msg_lspid, Msg_lrpid, Msg_stime and msg_rtime initialized to 0

Msg_ctime set to Current time

Msg_qbytes is initialized to the maximum number of bytes in the system limit queue

If the queue you want to create already exists and has read and write permissions, check whether the return value can be viewed successfully

The ID of the queue successfully returned (non-negative), failed return-1

Failure is set to errno, the value is described as follows:
The queue that corresponds to the eacces parameter key already exists, and the calling process does not have permissions on the queue or Cap_ipc_owner (bypasses checking the system's permissions on the object)
The queue that corresponds to the eexist parameter key already exists, and MSGFLG is set to Ipc_creat and IPC_EXCL
Enomem A new queue is created, but the system has no more memory
ENOSPC exceeds the maximum storage data allowed in the queue created by the system (65536)

Attention
The 1.ipc_private type is key_t, not a flag field, and when used for parameter key invocation, the system ignores other parameters except MSGFLG's low 9-bit permission bit, about the system restriction associated with the Megget call: The maximum number of queues allowed by the Msgmni system, System-dependent (Linux limit values can be modified or obtained via/proc/sys/kernel/msgmni 31683)
2. Before the linux2.3.20 version, when Msgget () is called and the queue has been deleted, it returns EIDRM
3. There are many uncertainties when using ipc_private to create a queue, it is recommended to use Ipc_new

#define Fun1_id10001#define fun2_id10002#define Usr_path "/usr/test/ok/test" #define USR_ID109870TYPEDEF struct _IPC_ DATA_INFO_{LONGS_TYPE;CHARS_DATA[70000];} Ipc_data_info;int ipc_msg_fun1 (pid_t a_id) {key_tb_msg_key;intb_msg_id;intb_ret, b_loop, b_id;charb_tmp_buf[1024] = { 0};char *b_tmp_ptr = (char *) malloc (65530); ipc_data_infob_ipc_snd = {0}, B_ipc_rcv = {0};struct Msqid_dsb_ipc_buf = {0};/ /msgctl ();//b_msg_key = Ftok (Usr_path, usr_id); B_msg_key = Ftok (Usr_path, usr_id); if (B_msg_key < 0) {printf ("Ftok Fail%d, line =%d, fun =%s, file =%s\n ", B_msg_key, __line__, __func__, __line__); return-1;} b_msg_id = Msgget (B_msg_key, ipc_creat|0666), if (b_msg_id < 0) {printf ("Msgget fail%d, line =%d, fun =%s, file =%s\n" , b_msg_id, __line__, __func__, __line__); return-1;} memset (&b_ipc_buf, 0, sizeof (B_IPC_BUF)), Msgctl (b_msg_id, Ipc_stat, &b_ipc_buf);p rintf ("id =%d, key =%d, limit =%d\n ", b_msg_id, B_msg_key, b_ipc_buf.msg_qbytes);//msgctl (b_msg_id, Ipc_rmid, &b_ipc_buf); b_ipc_snd.s_type = Fun1_id;b_ipc_rcv.s_type = Fun2_id;b_loop = 0;while (1) {memset (b_tmp_ptr, 0, sizeof (B_TMP_PTR)); sprintf (B_tmp_ptr, "Test_data%d", b_loop); memset (b_ipc_snd.s_data, 0, sizeof (b_ipc_snd.s_data)); memcpy (B_ipc_snd.s _data, B_tmp_ptr, strlen (b_tmp_ptr)), B_ret = Msgsnd (b_msg_id, &b_ipc_snd, 1024x768, 0); if (B_ret <-1) {printf ("msgsnd Fail%d, line =%d, fun =%s, file =%s\n ", B_ret, __line__, __func__, __file__); return-1;} printf ("Send data success data =%s\n", b_ipc_snd.s_data), #if 0b_ret = MSGRCV (b_msg_id, &AMP;B_IPC_RCV, 1024x768, B_ipc_rcv.s_ Type, 0), if (B_ret <-1) {printf ("msgsnd fail%d, line =%d, fun =%s, file =%s\n", B_ret, __line__, __func__, __file__) ; return-1;} printf ("recv data =%s\n", b_ipc_rcv.s_data), #endifb_loop ++;if (B_loop > 10000) {b_loop = 0;} Sleep (2);} return 0;} int ipc_msg_fun2 (pid_t a_id) {key_tb_msg_key;intb_msg_id;intb_ret, b_loop;charb_tmp_buf[1024] = {0};ipc_data_infob_ Ipc_snd = {0}, B_ipc_rcv = {0};b_msg_key = Ftok (Usr_path, usr_id); if (b_msg_kEY < 0) {printf ("Ftok fail%d, line =%d, fun =%s, file =%s\n", B_msg_key, __line__, __func__, __line__); return-1;} b_msg_id = Msgget (B_msg_key, ipc_creat|0666), if (b_msg_id < 0) {printf ("Msgget fail%d, line =%d, fun =%s, file =%s\n" , b_msg_id, __line__, __func__, __line__); return-1;} B_ipc_snd.s_type = Fun2_id;b_ipc_rcv.s_type = Fun1_id;b_loop = 0;while (1) {#if 0memset (b_tmp_buf, 0, sizeof (B_TMP_BUF)); sprintf (B_tmp_buf, "Test_data1%d", b_loop); memset (b_ipc_snd.s_data, 0, sizeof (b_ipc_snd.s_data)); memcpy (b_ipc_ Snd.s_data, B_tmp_buf, strlen (B_TMP_BUF)); B_ret = Msgsnd (b_msg_id, &b_ipc_snd, strlen (B_ipc_snd.s_data), 0); if (b_ RET <-1) {printf ("msgsnd fail%d, line =%d, fun =%s, file =%s\n", B_ret, __line__, __func__, __file__); return-1;} printf ("Send data success\n"); #endifmemset (&AMP;B_IPC_RCV, 0, sizeof (B_IPC_RCV)); B_ret = MSGRCV (b_msg_id, &b_ipc_ Rcv, 1024x768, B_ipc_rcv.s_type, 0); if (B_ret <-1) {printf ("msgsnd fail%d, line =%d, fun =%s, file =%s\n", B_ret, __line__, __func__, __file__); return-1;} printf ("recv data =%s\n", b_ipc_rcv.s_data), #if 0b_loop++;if (B_loop > 10000) {b_loop = 0;} #endifsleep (1);} return 0;} int main () {/*============================================*//* queue */intb_loop = 0, B_ret;pid_tb_id[10];b_id[b_loop] = Fork (); if (B_id[b_loop] < 0) {printf ("fork fail%d, line =%d, fun =%s, file =%s\n", b_id, __line__, __func__, __file__ ); return-1;} else if (0 = = B_id[b_loop]) {/* Child process */IPC_MSG_FUN1 (B_id[b_loop]);} B_loop++;b_id[b_loop] = fork (), if (B_id[b_loop] < 0) {printf ("fork fail%d, line =%d, fun =%s, file =%s\n", b_id, __li ne__, __func__, __file__); return-1;} else if (0 = = B_id[b_loop]) {/* Child process */ipc_msg_fun2 (B_id[b_loop]);} B_loop++;while (1) {B_ret = Wait (NULL); if (B_ret <-1) {printf ("Son process over%d, line =%d, fun =%s, file =%s\n", B_r ET, __line__, __func__, __file__); break;}} Return 0;/*============================================*/}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

interprocess Communication-Queues

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: info-contact@alibabacloud.com 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.