delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2011/03/03/04:57:37

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=0.1 required=5.0 tests=AWL,BAYES_00,TW_MQ,T_RP_MATCHES_RCVD
X-Spam-Check-By: sourceware.org
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
Message-ID: <0C11C5BF0B29FD43A8D0250F711D497F89D5AD0D39@ex01-ubitronix.ubitronix.local>
x-esetresult: clean, is OK
x-esetid: 457DA920DB39A135153D
MIME-Version: 1.0
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019