簡體   English   中英

IPC在C中使用消息隊列:接收錯誤

[英]IPC using message queue in C: Error in receiving

我正在C linux中實現消息隊列。 我正在發送integer = 17和接收integer = 0 請參閱下文,讓我知道我的msgsndmsgrcv函數出了什么問題。 請注意這一點: rbuf將數據存儲在rbuf->m->msglenrbuf->mtype

在發送過程中

msgsnd(msqid, sbuf,sizeof(int), 0);
printf("\nmsglen = %d",rbuf->m->msglen);  // 17

在接收過程中。 兩者具有相同的msqid。 我已經驗證了

msgrcv(msqid, rbuf, sizeof(int), 1, 0);
printf("\nmsglen = %d",rbuf->m->msglen); // 0

//msqid=98305, some valid id

這是我在另一個文件中定義的結構定義。

typedef struct message1
{
    int msglen;
    unsigned char *cp;
}msg1;

typedef struct msgbuf
{
    long    mtype;
    msg1    *m;
} message_buf;

您正在發送一條包含指向message1結構的指針的消息。 接收過程取消了對該指針的引用,但是在該過程中它沒有指向同一對象。 實際上,我很驚訝您沒有遇到段錯誤。

您應該這樣定義msgbuf:

typedef struct msgbuf
{
    long    mtype;
    msg1    m;
} message_buf;

因此,msg1結構包含在msgbuf中,而不是由其指向。

另外,您需要指定的大小是sizeof(message_buf),而不是sizeof(int)。

//header files
#include"msgbuf.h"  //where I have put my structures
#define AUTOMATIC 1
#define USER_DATA 2

int main()
{
    int msgflg = IPC_CREAT | 0666,ch,len=0;
    size_t msgsize = 0;
    int msqid;
    key_t key;
    message_buf *sbuf;
    char ans;
    char *data;
    key = ftok("/home/user",15);

    printf("Do you want to send messages\t");
    scanf("%c",&ans);
    getchar();
    if (((ans=='y' || ans=='Y') && (msqid = msgget(key, msgflg )) ==-1))
    {       perror("msgget");
            exit(1);
    }
    else
    fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);

    while(ans=='y' || ans=='Y')
    {
            printf("\n1. Automatic data\n2. Enter data\n3. Exit\nEnter your choice\t");
            scanf("%d",&ch);
            getchar();
            switch(ch)
            {
                    case AUTOMATIC: len=strlen("Did you get this?");

                                    sbuf=malloc(len+sizeof(int));
                                    memset(sbuf, 0, sizeof(message_buf));
                                    sbuf->m=malloc(len+sizeof(int));
                                    memset(sbuf->m, 0, sizeof(msg1));

                                    sbuf->m->msglen=len;
                                    sbuf->m->cp=malloc(sizeof(len));
                                    strncpy(sbuf->m->cp, "Did you get this?",strlen("Did you get this?"));
                                    sbuf->m->cp[strlen(sbuf->m->cp)]='\0';
                                    break;
                    case USER_DATA: printf("\nEnter data\t");
                                    fflush(stdout);
                                    len = getline(&data, &msgsize, stdin);

                                    sbuf=malloc(len+sizeof(int));
                                    memset(sbuf, 0, sizeof(message_buf));
                                    sbuf->m=malloc(len+sizeof(int));
                                    memset(sbuf->m, 0, sizeof(msg1));

                                    strcpy(sbuf->m->cp, data);
                                    sbuf->m->cp[strlen(sbuf->m->cp)]='\0';
                                    sbuf->m->msglen=len;
                                    break;
                    case 3: msgctl(msqid, IPC_RMID, NULL);
                            printf("\nQueue with q id = %d is removed\n",msqid);
                            exit(1);
                    default:printf("\nTRY AGAIN\t");
                            scanf("%c",&ans);
                            getchar();
            }
            printf("\nmsglen = %d\nmsgcp= %s",sbuf->m->msglen,sbuf->m->cp);
             /* Send a message */
            sbuf->mtype=1;
            if (msgsnd(msqid, sbuf,2*sizeof(int), 0) < 0)
            {
        printf("Msg q id= %d\nMsg type= %d\nMsg Text %s\nMsg Len= %d\n", msqid, sbuf->mtype, sbuf->m->cp,sbuf->m->msglen);
                    perror("msgsnd");
                    exit(1);
            }
            else
            printf("\nMessage: %s\n Sent\n", sbuf->m->cp);
    }
    msgctl(msqid, IPC_RMID, NULL);
    printf("\nQueue with q id = %d is removed\n",msqid);
    return 0;
}

現在正在接收代碼

//header files
#include"msgbuf.h"
int main()
{
    int msqid;
    key_t key;
    int msgflg = 0666;
    message_buf  *rbuf;
    int msg_len_rcvd=0;

    rbuf=malloc(150);
    rbuf->m=malloc(100);
    key = ftok("/home/user",15);
    if ((msqid = msgget(key, msgflg)) ==-1)
    {
            perror("msgget");
            exit(1);
    }
    printf("\n\n%d\n",msqid);
    /* Receive an answer of message type 1.   */
    while(1)
    {
            if ( (msg_len_rcvd=msgrcv(msqid, rbuf, 2*sizeof(int), 1, 0)) < 0)
            {
                    perror("msgrcv");
                    exit(1);
            }
            else
            {
                    printf("\n Number of bytes received:: %d", msg_len_rcvd);
                    printf("\nmtype1 = %d",rbuf->mtype);
                    printf("\nmsglen= %d",rbuf->m->msglen);
            }
            break;
    }
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM