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

标签 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:
  • 此代码中出现垃圾值的原因是什么?

    结构 mesg_q { char 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 中得到这个错误值的原因是什么?

最佳答案

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

为避免上述FIFO问题,您正在使用消息队列,因为在MQ中您可以一次放入数据并可以随时读取,为此您必须指定mtypemtype process_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;
}

关于c - 消息队列 - 没有正确的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50128097/

相关文章:

c++ - 将帧保存到磁盘时防止丢帧

linux - POSIX 消息队列 linux

c - memcpy 是否会正确复制使用 mkl_malloc 分配的数组?

linux - QProcess 调用带有参数的 gksudo 用于调用脚本的个性化消息

linux - Bash-检查目录中存在什么类型的文件

c - 在列表名称中搜索请求名称

amqp 上的 C 二进制消息

c++ - 如何忽略使用 buildrpm 自动添加的编译器标志

c - 如何在没有 clock_t 或定时器的情况下在 C 中执行时间循环?

c - C中的数组求和,应该在(???)中插入什么来完成该功能?