Mail Archives: cygwin/2011/03/03/04:57:37
Hi,
I have a problem with the mqueues.
If I use mq_timedreceive() and do not set O_NONBLOCK, mq_timedreceive() blo=
cks forever and eats 100% of one cpu core.
If I use mq_receive() and set O_NONBLOCK, mq_receive() blocks forever (0% c=
pu).
Thanks,
Manuel
Here is some test code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mqueue.h>
#include <errno.h>
#include <fcntl.h>
#define QUEUE_NAME "testQ"
#define SEND_TEST "SendTest"
#define MAX_QUEUE_ENTRY_SIZE 20
int main(void) {
int res;
mqd_t msgQueue;
struct mq_attr attrQueue;
char recBuffer[MAX_QUEUE_ENTRY_SIZE];
struct timespec abs_timeout;
/* Message-Queue attribute structure */
memset(&attrQueue,0x00,sizeof(attrQueue));
attrQueue.mq_flags =3D 0; /* no exceptional behavior=
(just O_NONBLOCK currently available) */
attrQueue.mq_maxmsg =3D 10; /* room for at most 10 mes=
sages in the queue */
attrQueue.mq_msgsize =3D MAX_QUEUE_ENTRY_SIZE; /* maximum size of a messa=
ge */
attrQueue.mq_curmsgs =3D 0; /* this (current number of=
messages) will be ignored */
res =3D mq_unlink(QUEUE_NAME);
if ((res =3D=3D -1) && (errno !=3D ENOENT))
{
// Don't print anything if the queue can't be unlinked, when it just do=
esn't exist.
printf("Failed to unlink msg queue %s: %s %d\n", QUEUE_NAME, sys_errlis=
t[errno],errno); fflush(stdout);
}
/* mq_open() for creating a new queue*/
msgQueue =3D mq_open(QUEUE_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_I=
RWXG, &attrQueue);
//msgQueue =3D mq_open(QUEUE_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S=
_IRWXG | O_NONBLOCK, &attrQueue);
if( msgQueue =3D=3D (mqd_t)-1 )
{
printf("Failed to create msg queue %s: %s %d\n", QUEUE_NAME, sys_errlis=
t[errno],errno); fflush(stdout);
return -1;
}
printf("Queue is open.\n"); fflush(stdout);
#if 0
// Send data.
res =3D mq_send(msgQueue, SEND_TEST, strlen(SEND_TEST), 0);
if (res =3D=3D -1)
{
printf("Failed to send request message.\n");
return -1;
}
printf("Sent data.\n"); fflush(stdout);
#endif
clock_gettime(CLOCK_REALTIME, &abs_timeout);
abs_timeout.tv_sec +=3D 1; // 1 Second timeout
// Wait for the response message.
res =3D mq_timedreceive(msgQueue, recBuffer, MAX_QUEUE_ENTRY_SIZE, NULL, =
&abs_timeout);
//res =3D mq_receive(msgQueue, recBuffer, MAX_QUEUE_ENTRY_SIZE, NULL);
if (res =3D=3D -1)
{
printf("Failed to receive message from read queue: %s %d.\n", sys_errli=
st[errno],errno); fflush(stdout);
return -1;
}
printf("Received data.\n"); fflush(stdout);
return EXIT_SUCCESS;
}
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
- Raw text -