Page 1 of 1

Time Out in message queues in PHP

Posted: Thu Jan 13, 2011 1:14 am
by praveenpvs
Hi Everyone,

I am writing PHP script which will communicate with C program through mesage queue.
In the PHP, i am waiting for the message from C program using the
msg_receive
function.
I want to come out of receiving after say 30 secs if i do not get any message.
msg_receive has one option, MSG_IPC_NOWAIT which will come out immediately if no data is available.
Is there way to stop the msg_receive blocking call after 30 secs if no data is available?
I tried my best to search for it but could not succeed .I am new to PHP programming.
Can you any one help me out?
Thanks a lot for your kind help.

regards
Praveen.

Re: Time Out in message queues in PHP

Posted: Thu Jan 13, 2011 8:38 am
by Darhazer
Use MSG_IPC_NOWAIT and a loop calling msg_receive / sleep
and check for 30 seconds timeout on your own, using time() before entering loop and comparing current time with initial one

Re: Time Out in message queues in PHP

Posted: Tue Jan 18, 2011 11:24 pm
by praveenpvs
Hi Darhazer and all...

Thanks darhazer for your reply....
I was looking for indefinite blocking on msg_receive().
I could figure finally. use msg_receive() as usual i mean with flags parameter to be 0.
Before calling msg_receive(), set the alarm using pcntl_alarm(seconds). pcntl_alarm(secs) sends the Signal SIGALRM after specified secs.
handle the SIGALARM signal in the signal handler. once it gets the signal, msg_receive() comes out of blocking with the error code set to EINTR.
I hope this may be useful to someone.
Thanks for your help.

regards
Praveen