Talk C together (97th back: C language instance -- use message queue for inter-process communication 1)

Source: Internet
Author: User

Talk C together (97th back: C language instance -- use message queue for inter-process communication 1)

Hello, everyone. In the previous session, we talked about the example of using shared memory for inter-process communication. This example is as follows:Use message queue for inter-process communication. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Message Queue is a specific object of the abstract concept such as the SystemV IPC structure, which is the same as the shared memory. Message Queue provides a queue for different processes to use. data can be transmitted between processes through this queue to implement inter-process communication.

Before introducing how to use MQ, we should introduce several functions that are used to operate MQ.

Msgget Function
int msgget(key_t key,int msgflag)

This function is used to create a new message queue or obtain an existing message queue.

The first parameter is a key value used to operate the structure of the IPC in the kernel, that is, the structure of the message queue in the kernel. (as described in the previous chapter) the second parameter is the message queue permission tag. The permission is the same as the File Permission. If the function runs successfully, the Message Queue identifier is returned; otherwise,-1 is returned; we can use this identifier to use the Message Queue; msgsnd Function
int msgsnd(int msg_id, const void *msg_ptr,size_t msg_sz,int msgflg)

This function is used to add a message to the Message Queue so that the process can obtain the message from the message queue;

When using this function, we need to define a message type and calculate the memory space of this type. The message type can be defined according to the program's needs. It is common to define a struct type. However, the first member of the type must be a long int type member, which is used to determine the message type.

The first parameter is the identifier of the message queue, which can be obtained through the msgget function. The second parameter is a pointer pointing to the message to be added to the message queue; the third parameter is an int value, indicating the size of the message to be added to the Message Queue. The fourth parameter is a single-digit tag, this flag is used to control the actions when the message queue is full or the system limit is reached. If the function runs successfully, 0 is returned; otherwise,-1 is returned;

When using this function, the fourth parameter is usually IPC_NOWAIT, indicating that after the message queue is full, the function does not send messages to the message queue and immediately returns-1. if this flag is not set, after the message queue is full, the process for sending the message is suspended until the message has space in the message queue and then sends the message to the message queue.

Msgrcv Function
int msgrcv(int msgid,void *msg_ptr,size_t msg_sz,long int msgtype,int msgflg)

This function is used to obtain or receive messages from a message queue;

The first parameter is the identifier of the message queue, which can be obtained through the msgget function; the second parameter is a pointer pointing to the message to be obtained from the Message Queue; the third parameter is an int value, indicating the size of the message. The fourth parameter is a long int value, indicating the priority of the received message; the fifth parameter is a single-digit tag used to control the action when no message is received in the message queue. When the function runs successfully, the number of bytes of the received message is returned. Otherwise,-1 is returned;

When this function is used, the value of the fifth parameter is the same as the value of the fourth parameter in the msgsnd function, and the function action is similar, except that the function is converted from sending a message to receiving a message. The fourth parameter of this function is usually 0, indicating that messages are received in the order they are sent;
If its value is n (n> 0), it indicates the type of messages whose receiving type value is n;
If the value is-n (n> 0), the receiving type is equal to or less than n;
The type value is the first member in the message type defined here.

Msgctl Function
int msgctl(int msg_id, int cmd,struct msgid_ds *buf)

This function is used to perform operations on message queues. A common operation is to delete message queues;

The first parameter is the identifier of the message queue, which can be obtained through the msgget function. The second parameter is a command that indicates the operation on the message queue. There are only three commands for use: IPC_STAT, IPC_SET and IPC_RMID; the third parameter is a struct pointer, which contains message queue permissions and owner information. If the function runs successfully, 0 is returned; otherwise,-1 is returned;

We usually use this function to delete a message queue. In this case, we need to assign IPC_RMID to the second parameter to delete the message queue. The third parameter can be a null pointer. The other two commands of the second parameter: IPC_STAT indicates to associate the content of the third parameter with the message queue; IPC_SET indicates to set the content of the third parameter to the value of the message queue. The type of the third parameter, which we mentioned in the previous chapter, is similar to the SystemV IPC structure. Besides necessary members, it also has its own unique members.

The usage of this function is similar to the shmctl function described in the previous chapter. You can compare it.

I found the type of the third parameter from the source code, detailed definition is as follows :( IN THE linux-4.0.3/include/linux/msg. h file)

struct msg_queue {    struct kern_ipc_perm q_perm;    time_t q_stime;         /* last msgsnd time */    time_t q_rtime;         /* last msgrcv time */    time_t q_ctime;         /* last change time */    unsigned long q_cbytes;     /* current number of bytes on queue */    unsigned long q_qnum;       /* number of messages in queue */    unsigned long q_qbytes;     /* max number of bytes on queue */    pid_t q_lspid;          /* pid of last msgsnd */    pid_t q_lrpid;          /* last receive pid */    struct list_head q_messages;    struct list_head q_receivers;    struct list_head q_senders;};

Let's talk about the example of using message queue for inter-process communication. I want to know what examples will be provided later, and I will try again.

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: 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.