POSIX message queues and series functions for Linux network programming

Source: Internet
Author: User
Tags file system message queue posix

First, in the previous introduction of System V Message Queuing knowledge, now to a little look at POSIX Message Queuing.

In fact, Message Queuing is a place where you can exchange data between processes, and the biggest difference between two standard message queues may be just the difference between API functions, such as System V's series functions are msgxxx, and POSIX is mq_xxx. POSIX message queues also have some limitations on message length, man 7 Mq_overview:

simba@ubuntu:~/documents/code/linux_programming/unp/posix$ Cat/proc/sys/fs/mqueue/msg_max

10

simba@ubuntu:~/documents/code/linux_programming/unp/posix$ Cat/proc/sys/fs/mqueue/msgsize_max

8192

simba@ubuntu:~/documents/code/linux_programming/unp/posix$ Cat/proc/sys/fs/mqueue/queues_max

256

That is, a message queue can have up to 10 messages, the maximum length of each message is 8192 bytes, and a system can have up to 256 message queues.

Also, on Linux, POSIX Message Queuing is implemented as a virtual file system and must be mounted to a directory to be seen, such as

# Mkdir/dev/mqueue

# mount-t Mqueue None/dev/mqueue

View the status of message queues through the cat command, assuming MYMQ is the name of a message queue created

$ cat/dev/mqueue/mymq

qsize:129 Notify:2 signo:0 notify_pid:8260

Qsize: Data length for all messages in Message Queuing

Notify_pid: A process uses mq_notify to register a message to arrive at an asynchronous notification event, that is, the PID of this process

NOTIFY: Notification mode: 0 is sigev_signal; 1 is sigev_none; and 2 is sigev_thread.

Signo: Indicates the number of the signal when it is notified by means of a signal.

Second, series functions, compile time plus the-LRT option, that is, connect the LIBRT library (real-time library)

#include <fcntl.h>/* for o_* Constants * *

#include <sys/stat.h>/* for Mode constants * *

#include <mqueue.h>

Features: Used to create and access a message queue

Prototype

mqd_t mq_open (const char *name, int oflag);

mqd_t mq_open (const char *name, int oflag, mode_t mode, struct mq_attr);

Parameters

Name: One of the message queue names must be in/leading, and subsequent cannot have other/, the shape such as/somename length can not exceed Name_max (255)

Oflag: Similar to the Open function, can be o_rdonly, o_wronly, O_RDWR, can also be bitwise or up o_creat, o_excl, O_nonblock;

Mode: If Oflag specifies O_creat, you need to set mode.

Return value: Successful return of message queue file descriptor; failed return-1

Function: Turn off Message Queuing

Prototype

mqd_t mq_close (mqd_t mqdes);

Parameters

Mqdes: Message Queuing descriptor

Return value: successful return 0; failure return-1

Features: Deleting message 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.