关于消息队列基础内容:
https://blog.csdn.net/qq_42418668/article/details/95189251
进程A给消息队列添加消息,进程B获取消息:
maina.c:
/*================================================================
* Copyright (C) 2019 . All rights reserved.
*
* 文件名称:maina.c
* 创 建 者:gaojie
* 创建日期:2019年07月08日
* 描 述:
*
================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/msg.h>
typedef struct data
{
long type;
char text[128];
}MsgStruct;
int main(int argc,char *argv[])
{
if(argc<3)
{
printf("error\n");
exit(0);
}
MsgStruct val;
memset(&val,0,sizeof(val));
sscanf(argv[1],"%d",&val.type);
strcpy(val.text,argv[2]);
int msgid = msgget((key_t)1234,IPC_CREAT|0664);
assert(msgid != -1);
msgsnd(msgid,&val,strlen(val.text),0);
}
mainb.c
/*================================================================
* Copyright (C) 2019 . All rights reserved.
*
* 文件名称:mainb.c
* 创 建 者:gaojie
* 创建日期:2019年07月08日
* 描 述:
*
================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/msg.h>
typedef struct data
{
long type;
char text[128];
}MsgStruct;
int main(int argc,char *argv[])
{
if(argc<2)
{
printf("error\n");
exit(0);
}
long type = 0;
sscanf(argv[1],"%d",&type);
int msgid = msgget((key_t)1234,IPC_CREAT | 0664);
assert(msgid != -1);
MsgStruct val;
memset(&val,0,sizeof(val));
msgrcv(msgid,&val,sizeof(val.text)-1,type,0);
printf("type:%d\n",val.type);
printf("data:%s\n",val.text);
}
进程A给消息队列添加消息:
进程B获取消息队列的消息: