【问题标题】:mq_receive throws error saying mesage too longmq_receive 抛出错误说消息太长
【发布时间】:2017-11-23 14:56:37
【问题描述】:

我是 linux 编程的新手,试图实现一个简单的 msg 队列工作。 但是收到错误消息说长,下面是我的代码,如果有任何更正,请提出建议。

我知道类似的问题被问了很多次,但我找不到我的问题的解决方案,因此发布了代码。

#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>           
#include <sys/stat.h>        
#include <mqueue.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>

void* producerThRoutine (void *arg);
void* ConsumerThRoutine (void *arg);

int main()
{
  int retVal = 0;
  pthread_t producerThId,consumerThID;
  mqd_t myMQdes;
  struct mq_attr attr;  

  attr.mq_flags = 0;  
  attr.mq_maxmsg = 1024;  
  attr.mq_msgsize = 10;  
  attr.mq_curmsgs = 0; 

  myMQdes = mq_open("/myMessageQueue",O_CREAT | O_RDWR,S_IWUSR | S_IRUSR,&attr);
  if(myMQdes == (mqd_t) -1){
    perror("Message Queue creation failed\n");
    exit(EXIT_FAILURE);
  }

  retVal = pthread_create(&producerThId,NULL,producerThRoutine,&myMQdes);
  if(retVal != 0){
    perror("\n producerThRoutine creation failed \n");
    exit(EXIT_FAILURE); 
  }

  retVal = pthread_create(&consumerThID,NULL,ConsumerThRoutine,&myMQdes);
  if(retVal != 0){
    perror("\n ConsumerThRoutine creation failed \n");
    exit(EXIT_FAILURE); 
  }

  retVal = pthread_join(producerThId,NULL);
  if(retVal != 0){
    perror("\n pthread_join for producer thread failed \n");
    exit(EXIT_FAILURE); 
  }

  retVal = pthread_join(consumerThID,NULL);
  if(retVal != 0){
    perror("\n pthread_join for consumer thread failed \n");
    exit(EXIT_FAILURE); 
  }
  mq_close(myMQdes);
  mq_unlink("/myMessageQueue");
  return 0; 
}


void* producerThRoutine (void *arg)
{
  char c;   
  char EOS = '\0';
  int retVal;
  mqd_t *pMQDes = (mqd_t *) arg;
  printf("Enter the string you want to convert to upper case \n");

  while( (c=getchar()) != EOF ){
    if(c == '\n'){
        retVal = mq_send( (*pMQDes),&EOS,sizeof(char),1);
        if(retVal != 0){
            perror("sending EOS to queue failed \n");
            exit(EXIT_FAILURE);         
        }
        break;
    }       
    retVal = mq_send( (*pMQDes),&EOS,sizeof(char),1);
    if(retVal != 0){
        perror("sending character to queue failed \n");
        break ;         
    }
  }
}

 void* ConsumerThRoutine (void *arg)
 {
   char msg;
   int msg_priority,retVal;

   mqd_t *pMQDes = (mqd_t *) arg;

   while(1){
    printf("\nthe converted string is : ");

retVal = mq_receive (*pMQDes,&msg,sizeof(char),&msg_priority);

    if(retVal == -1){
        perror("mq_receive failed");    
        exit(EXIT_FAILURE); 
    }
    if( msg == '\0')
    {
        break;
    }
    putchar(toupper(msg));
  }
 }

【问题讨论】:

  • 请指出出现该错误的代码行,并复制粘贴错误消息以便我们也能看到。
  • 错误行被higlited

标签: c posix message-queue


【解决方案1】:

我刚刚查看了 mq_receive 的手册页,其中一个错误显示如下:

EMSGSIZE
          msg_len was less than the mq_msgsize attribute of the message queue.

我改变了

retVal = mq_receive (*pMQDes,&msg,sizeof(char),&msg_priority);

retVal = mq_receive (*pMQDes,&msg,1024 * sizeof(char),&msg_priority);

其中 1024 是您设置的 mq_msgsize。然后错误消失。

【讨论】:

  • 请注意,您确实应该传入一个包含 1024 个元素的数组,现在 msg 只是一个字符。
  • 对不起,mq_msgsize 是 10,最好设置为 10。
猜你喜欢
  • 2011-08-03
  • 2020-07-19
  • 2017-04-20
  • 1970-01-01
  • 2011-06-15
  • 2018-12-12
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
相关资源
最近更新 更多