消息队列 - 没有正确的响应

时间:2018-05-02 06:06:44

标签: c linux message-queue

*我使用消息队列发送和接收消息,但结构中的一些参数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:
  • 此代码中垃圾值的原因?

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

    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的错误值的原因是什么?

1 个答案:

答案 0 :(得分:0)

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

为了避免上面FIFO的问题,你正在使用消息队列,就像在MQ中一样,你可以一次放入数据并且可以随时读取,因为你必须指定mtype即是什么mtype process_1正在发送数据,并且mtype进程_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;
}