why is there no poll/select like mechanism for message queue?

前端 未结 5 1955
感情败类
感情败类 2021-01-14 01:19

Like we can do, poll/epoll/select on an fd, we can not on msg queue id. I found some non standard methods to make msgqueue-id to fd, but afterall its a non standard. So my q

相关标签:
5条回答
  • 2021-01-14 02:10

    As Mat points out, POSIX MQs can be used with select/poll in Linux. Beyond that, mq_notify() provides you with the options to receive a signal or spawn a new thread when an empty MQ receives a message. This is another means to avoid blocking or polling.

    0 讨论(0)
  • 2021-01-14 02:12

    The SysV MsgQ gives you provision to block your msgrcv() call for a particular message type or for any message type using IPC_WAIT. Poll/epoll/select are used to help you write a event-driven program in which the user application doesn't waste cpu cycles polling for a particular event and leaves it to the better judgement of the kernel. This is what you can also achieve using the SysV msg Q.

    0 讨论(0)
  • 2021-01-14 02:13

    From the mq_overview man page:

    Polling message queue descriptors

    On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using select(2), poll(2), or epoll(7). This is not portable.

    So you can use poll and friends on message queues - just make sure you use the modern variant.

    0 讨论(0)
  • 2021-01-14 02:16

    I'm surprised that it doesn't just work, but if it doesn't, I suspect the reason is to avoid encouraging such a non-portable practice. While message queue descriptors are allowed to be file descriptors, they're not required to be, and code that assumes they're file descriptors (and thus occupy the same "numberspace") is non-portable.

    If you need file descriptors, you'd probably be better off just using Unix sockets or some other mechanism in place of message queues. Message queues seem to be intended for use with real-time programming with threads where select/poll-based event-driven IO is generally not used.

    0 讨论(0)
  • 2021-01-14 02:25

    IBM AIX has extension for poll() on System V queues. But Linux does not have this feature. On the other hand, Linux Posix queue implementation allows to select/poll/epoll on queues.

    0 讨论(0)
提交回复
热议问题