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 =]
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.
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. :)
精彩评论