Analysis of Message Queue functions in linux

Source: Internet
Author: User

Linux Message Queue function ------------------------------------------------------- linux Message Queue function ------------------------------ header file: # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> www.2cto.com ------------------------------------------ 1. An ID value must be specified for the system to establish IPC communication (such as message queue and shared memory. Generally, this id value is obtained through the ftok function. The ftok prototype is as follows: key_t ftok (char * fname, int id) fname indicates the name of the file you specified, and id indicates the subserial number. In the general UNIX implementation, the index node number of the file is taken out, and the return value of key_t is obtained by adding the sub-number before it. For example, if the index node number of a specified file is 65538, it is converted into a hexadecimal value of 0x010002, And the ID value you specified is 38, it is converted into a hexadecimal value of 0x26, then the final key_t return value is 0x26010002. The method for querying the file index node number is: ls-I after deleting the reconstruction file, the index node number is allocated by the operating system according to the usage of the file system at that time, so it is different from the original one, therefore, the index node number is different. To ensure that the key_t value remains unchanged, ensure that the ftok file is not deleted or that a fixed key_t value is specified without ftok, for example: # define IPCKEY 0x111char path [256]; sprintf (path, "% s/etc/config. ini ", (char *) getenv (" HOME "); msgid = ftok (path, IPCKEY); [/code] the same program, it is used to ensure that two groups of identical programs under two different users obtain the IPC key values that do not interfere with each other. Because etc/config. ini (hypothesis) is the key configuration file of the application system, so there is no problem of being easily deleted-even if it is deleted, it will soon be discovered and rebuilt (and the application system will be restarted ). Ftok () is designed for this purpose. ------------------------------------------------ 2. int msgget (key_t key, int msgflg); // create a message queue parameter: key: The key associated with the message queue. When IPC_PRIVATE is set, the Message Queue msgflg is created: the establishment flag and access permission of the message queue. The low position of msgflg is used to determine the access permission of the message queue. IPC_CREAT: if the key does not exist, create IPC_EXCL: if the key exists, return failure IPC_NOWAIT: If you need to wait, directly return an error. If IPC_CREAT is used separately, msgget () either the ID of a newly created message queue or the ID of a queue with the same keyword value are returned. If IPC_EXCL and IPC_CREAT are used together, msgget () will either create a new message queue or return a failure value-1 if the queue already exists. Description: if the message queue is successful, the system returns the Message Queue Identifier-1: errno = EACCESS (permission not allowed) EEXIST (the queue already exists and cannot be created) if the message queue identifier fails) EIDRM (queue marked as deleted) ENOENT (queue does not exist) ENOMEM (insufficient memory when creating the queue) ENOSPC (exceeds the maximum queue limit) www.2cto.com example: msgid = msgget (IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 00666); // create a message queue Queue (optional values: 3 and int msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg ); // send a message to the Message Queue parameter msqid: The identifier of the message queue. Msgp: pointer to the message buffer, which is used to temporarily store sent and received messages. It is a user-defined general structure, in the following format: struct msgbuf {long mtype; /* message type, must be> 0 */char mtext [1];/* message text */}; msgsz: Message Size. Msgflg: indicates the action that a process should take when the queue data is full (msgsnd) or empty (msgrcv. If it is set to IPC_NOWAIT, no message is sent when the message queue is full and the calling process immediately returns the error message EAGAIN. If it is set to 0, the calling process is blocked until the message queue is not full. Return Description: when the execution is successful, msgsnd () returns 0. If the execution fails,-1 is returned. For example: msgsnd (g_msg_id, & msg_snd, sizeof (msg_snd.msg_item), IPC_NOWAIT ); // messages sent in non-blocking mode: Optional 4. ssize_t msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp, int msgflg); // read the information parameter from the message queue: msgtyp: msgtyp = 0: receives the first message of any type in the queue. Msgtyp> 0: receives the first message of the msgtyp type. Msgtyp <0: receives the first message of the lowest type (smaller than or equal to the absolute value of msgtyp. For other parameters, see the msgsnd function. Return Description: if the execution is successful, msgrcv () returns 0. If the execution fails,-1 is returned. For example: msgrcv (msgid, & msg_rbuf, sizeof (msg_rbuf.msg_item ); // block receiving: www.2cto.com Listen 5, int msgctl (int msqid, int cmd, struct msqid_ds * buf); // Message Queue attribute control parameter: msqid: Message Queue identifier. Cmd: the control command to be executed. The following options are available: IPC_STAT: Read Message Queue attributes. Obtain the msqid_ds structure of the queue and store it in the structure pointed to by the buf. IPC_SET: Set message queue attributes. IPC_RMID: deletes a message queue. IPC_INFO: basic information about reading a message queue. This command is equivalent to the ipcs command. These four commands (IPC_STAT, IPC_SET, IPC_INFO, and IPC_RMID) can also be used for semaphores and shared storage. Buf: temporary variable of the msqid_ds struct type. It is used to store read message queue attributes or message queue attributes to be modified. Example: msgctl (qid, IPC_RMID, NULL) // Delete A Message Queue from the pj81102 Column

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.