System V Message Queue - Timed receive

Deadly 提交于 2020-01-02 18:25:19

问题


Im using System V Message Queue, which does not have a "timed receive" function, like POSIX MQ. But now i need it (this timed receive func.).

One can ask: "so why dont you use POSIX instead of Sys V?". Because in my benchmarks SysV MQ was +- 20% faster than POSIX MQ.

Now the question. How to fake a "timed receive" for Sys V MQ?

I wrote a piece of code that does it testing msgrcv with IPC_NOWAIT in a loop with usleep. I think its ugly, so i need ideas to write a beautiful one =]


回答1:


You could make a little code that first fork a child process that sleeps for X quantity of time. After it wakes up, it sends a signal (like SIGUSR1) to the father, that interrumpts the father and cancel the msgrcv. Then the child process dies. But you have to remember that you need to specify a handler for that signal, because the default one just exits the process.




回答2:


This article shows the same thing you noticed, that you need to loop on it: http://linuxgazette.net/issue92/hawk.html

Then it goes on to propose a kernel modification to work around this. YMMV, but I suspect you won't be in a position to hack your kernel just to do this. :)



来源:https://stackoverflow.com/questions/4110198/system-v-message-queue-timed-receive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!