Linux Message Queue在两个Process之间接收特定mtype失败

时间:2017-01-18 06:02:51

标签: linux message-queue

send.c创建一个消息队列并发送带有mtype 5的消息

send.c

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
struct msgbuf
    {
    long mtype;
    char mtext[40];
    };
void main()
    {
    int key,id;
    struct msgbuf vara; 
    key = ftok("/usr", 65);
    id = msgget(key, IPC_CREAT|0666);
    vara.mtype=5;
    strcpy(vara.mtext,"hi");
    msgsnd(id,&vara, strlen(vara.mtext)+1, 0);
    perror("status:");
    printf("msg sent\n");
    }

receive.c正在尝试从mtype 5的消息队列接收消息

recieve.c

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#define MAXSIZE 128
struct msgbuf
    {
    long mtype;
    char mtext[40];
    };
void main()
    {
    int key;    int id;
    struct msgbuf vars;
    key = ftok("/usr", 65);
    id = msgget(key, IPC_CREAT|0666);
    msgrcv(id,&vars,MAXSIZE,5,0);
    perror("status ");
    printf("msg rcvd --> %s\n", vars.mtext);
    }

我无法接收带有在receive.c文件中的msgrcv函数中提到的特定mtype的消息但是如果mtype设置为0我能够收到消息

0 个答案:

没有答案