问题
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