【问题标题】:Message queue - No proper response消息队列 - 没有正确响应
【发布时间】:2018-10-12 04:15:03
【问题描述】:

*我正在使用消息队列来发送和接收消息,但是结构 msqid_ds 中的某些参数没有给出正确的值,为什么会发生这种情况? *最后一条消息发送的时间=1525240214-为什么显示垃圾值?

        struct mesg_q
    {
        char msg_txt[100];
        long msg_typ;
    };

    int main()
    {
        int msgid;
       key_t key;
        char buffer[100]; 

        struct mesg_q msgq;
         struc answerst msqid_ds info;
     //  key = ftok("/home/yash72/krishna/thread_test/msgq.text", 65);
        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);

        if(msgid== -1)
        {    
            printf("msgget failed\n");
            return -1;
        }

        while(1)
        { 
            printf("Text Message\n");
            fgets(msgq.msg_txt,100,stdin);

            if(msgsnd(msgid,&msgq,100,0)==-1)
            {
                printf("Send failed\n");
                return -1;
            }  
            else
            {
                printf("Message send\n");
            }    
         msgctl(msgid, IPC_STAT, &info);
         printf("Time of last message send=%d\n",info.msg_stime);
         printf("uid=%d\n",info.msg_perm.uid);
        }   
    }    

OUTPUT:

 Text Message
qwerty
Message send
Time of last message send=1525240214 // Why this is showing junk?
uid=0
Text Message



Receiver code:
  • 此代码中的垃圾值的原因是什么?

    结构mesg_q { 字符 msg_txt[100]; 长消息类型; };

    int main()
    {
        int msgid;
        char buffer[100];
        long int rec_buff=0;
        key_t  key;
        struct mesg_q msgq;
         struct msqid_ds info;
    
       // key = ftok("/home/yash72/krishna/thread_test/msgq.text", 65);
        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
    
        if(msgid == -1)
        {
            printf("Msgget failed\n");
            return -1;
        }
    
        while(1)
    
        {
            if(msgrcv(msgid,&msgq,100,rec_buff,0)==-1)
            {
                printf("Mesg recv failed\n");
                return -1;
            }
            else
            {
                printf("Mesg Recvd\n");
            }
            printf("Recvd mesg=%s\n",msgq.msg_txt);
    
           msgctl(msgid, IPC_STAT, &info);
    
            printf("Num:of bytes on queue= %d\n", info.msg_cbytes);
            printf("num:of messages on queue=%d\n", info.msg_qnum);
            printf("Max bytes on queue=%d\n", info.msg_qbytes);
            printf("Time of last message recvd=%d\n",info.msg_rtime);
        }     
    }
    

    输出;

    队列上的字节数= 0 队列上的消息数=0 队列上的最大字节数=16384 最后收到消息的时间=1525240214

    从 struct msqid_ds 得到这个不正确值的原因是什么?

【问题讨论】:

  • 你没有指定msg_typ
  • msg_typ 对结构 msqid_ds 做了什么? msg_typ 在结构 mesg_q 中。回答我提出的问题。
  • 您通过调用msgsnd() 第二个参数发送整个msgq。您应该更新同一 msgq 变量中的 msg_typ 值。

标签: c linux message-queue


【解决方案1】:

首先你应该知道为什么要使用message queue IPC,我假设在使用消息队列之前,你已经经历过FIFO。在FIFO 中,进程相互通信的条件是什么?两个进程在通信时都应该活着

为避免FIFO 的上述问题,您正在使用消息队列,因为在 MQ 中,您可以一次放入数据并且可以随时读取,为此您必须指定 mtype 即使用 mtype 处理_1正在发送数据,mtype process_2 正在接收数据。

来自msgsnd的手册页

struct msgbuf {
      long mtype;       /* message type, must be > 0, I'm talking baout this */
      char mtext[1];    /* message data */
};

所以在两个过程中(读者和发件人)指定mtype

msgq.msg_typ = 1; /*specify this in both process */

其次,最后一条消息发送的时间=1525240214-为什么显示垃圾值? => 它不是垃圾数据,它是从 EPOCH 时间开始的秒数,使用 ctime() 打印人类可读的格式。

sender.c

#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include<stdint.h>
struct mesg_q {
        char msg_txt[100];
        long msg_typ;
};
int main(void) {
        int msgid;
        struct mesg_q msgq;
        struct msqid_ds info;
        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
        if(msgid== -1) {
                printf("msgget failed\n");
                return -1;
        }
        while(1) {
                printf("Text Message\n");
                fgets(msgq.msg_txt,sizeof(msgq.msg_txt),stdin);
                /* with what msg_type you are sending, you didn't mention ? mention it */
                msgq.msg_typ = 1; /* I am sending with msg_type 1 */
                if(msgsnd(msgid,&msgq,sizeof(msgq),msgq.msg_typ)==-1) {
                        printf("Send failed\n");
                        return -1;
                }
                else {
                        printf("Message send\n");
                }
                msgctl(msgid, MSG_STAT, &info);
                printf("Time of last message send = %jd\n",(intmax_t)info.msg_stime);/* use the correct format specifier */
                /* what ever time info.msg_stime printing, its not junk, its seconds from EPOCH, use ctime() to print in readable format */
                printf("uid = %d\n",info.msg_perm.uid);
        }
        return 0;
}

receiver.c

struct mesg_q {
        char msg_txt[100];
        long msg_typ;
};
int main(void ){
        int msgid;
        long int rec_buff=0;
        struct mesg_q msgq;
        struct msqid_ds info;
        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
        if(msgid == -1) {
                printf("Msgget failed\n");
                return -1;
        }
        while(1) {
                /* with what msg_typ you are reciving, you have to mention ? u didn't */
                msgq.msg_typ = 1;
                if(msgrcv(msgid,&msgq,sizeof(msgq),rec_buff, msgq.msg_typ)==-1) {
                        printf("Mesg recv failed\n");
                        return -1;
                }
                else{
                        printf("Mesg Recvd\n");
                }
                printf("Recvd mesg=%s\n",msgq.msg_txt);

                msgctl(msgid, MSG_STAT, &info);
                /* enable compiler warning, and use correct format specifier */
                printf("Num:of bytes on queue= %u\n", (int)info.msg_cbytes);
                printf("num:of messages on queue=%d\n", (int)info.msg_qnum);
                printf("Max bytes on queue=%d\n", (int)info.msg_qbytes);
                printf("Time of last message recvd=%jd\n",(intmax_t)info.msg_rtime);
        }
        return 0;
}

【讨论】:

  • 这段代码也给出了相同的输出。那里的 intnax_t 有什么用?我也给出相同的输出
  • 启用警告标志并检查。使用 -Wall -Wstrict-prototypes -Werror 编译您的代码。 这段代码给出相同的输出?你期望什么输出?
  • 使用MSG_STAT 而不是IPC_STAT
  • 阅读msgctl() 的手册页,上面写着msg_cbytes 是非标准的。更好地使用POSIX ipc的
  • 为什么我没有从 struct msqid_ds 的参数中得到正确的值?
猜你喜欢
  • 2013-12-16
  • 2011-05-23
  • 2011-07-21
  • 1970-01-01
  • 2017-09-14
  • 2012-01-07
  • 2013-11-26
  • 2022-10-08
  • 1970-01-01
相关资源
最近更新 更多