This is the mail archive of the
cygwin
mailing list for the Cygwin project.
1.7.8: mq_timedreceive() blocks, mq_receive() with O_NONBLOCK blocks, too
- From: Manuel Wienand <Manuel dot Wienand at ubitronix dot com>
- To: "cygwin at cygwin dot com" <cygwin at cygwin dot com>
- Date: Thu, 3 Mar 2011 10:57:37 +0100
- Subject: 1.7.8: mq_timedreceive() blocks, mq_receive() with O_NONBLOCK blocks, too
Hi,
I have a problem with the mqueues.
If I use mq_timedreceive() and do not set O_NONBLOCK, mq_timedreceive() blocks forever and eats 100% of one cpu core.
If I use mq_receive() and set O_NONBLOCK, mq_receive() blocks forever (0% cpu).
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 = 0; /* no exceptional behavior (just O_NONBLOCK currently available) */
attrQueue.mq_maxmsg = 10; /* room for at most 10 messages in the queue */
attrQueue.mq_msgsize = MAX_QUEUE_ENTRY_SIZE; /* maximum size of a message */
attrQueue.mq_curmsgs = 0; /* this (current number of messages) will be ignored */
res = mq_unlink(QUEUE_NAME);
if ((res == -1) && (errno != ENOENT))
{
// Don't print anything if the queue can't be unlinked, when it just doesn't exist.
printf("Failed to unlink msg queue %s: %s %d\n", QUEUE_NAME, sys_errlist[errno],errno); fflush(stdout);
}
/* mq_open() for creating a new queue*/
msgQueue = mq_open(QUEUE_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, &attrQueue);
//msgQueue = mq_open(QUEUE_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG | O_NONBLOCK, &attrQueue);
if( msgQueue == (mqd_t)-1 )
{
printf("Failed to create msg queue %s: %s %d\n", QUEUE_NAME, sys_errlist[errno],errno); fflush(stdout);
return -1;
}
printf("Queue is open.\n"); fflush(stdout);
#if 0
// Send data.
res = mq_send(msgQueue, SEND_TEST, strlen(SEND_TEST), 0);
if (res == -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 += 1; // 1 Second timeout
// Wait for the response message.
res = mq_timedreceive(msgQueue, recBuffer, MAX_QUEUE_ENTRY_SIZE, NULL, &abs_timeout);
//res = mq_receive(msgQueue, recBuffer, MAX_QUEUE_ENTRY_SIZE, NULL);
if (res == -1)
{
printf("Failed to receive message from read queue: %s %d.\n", sys_errlist[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