2017-06-20 154 views
0

我是Linux編程的新手,試圖實現一個簡單的消息隊列工作。 但得到錯誤消息說長,下面是我的代碼,請建議如果有任何更正。mq_receive拋出錯誤說消息太長

我知道類似的問題被問了很多次,但我無法找到解決我的問題,因此張貼代碼。

#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,&味精,的sizeof(char)的,& msg_priority);

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

請指出給出該錯誤的代碼行,並複製粘貼錯誤消息,以便我們也可以看到它。 – nos

+0

錯誤行高於 – haris

回答

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。然後錯誤消失。

+0

請注意,你真的應該傳入一個有1024個元素的數組,現在'msg'只是一個字符。 – nos

+0

對不起,'mq_msgsize'爲10,最好設置爲10。 – slzy