系统相关
首页 > 系统相关> > 进程间通信(二)消息队列

进程间通信(二)消息队列

作者:互联网

消息队列用于运行于同一台机器上的进程间通信,它和管道和相似,是一个在系统内核中用来保存消息的队列,它在系统内核中是以消息链表的形式出现。消息链表中节点的结构用msgmsgmsg声明。

消息队列

(1)创建新消息队列或取得已存在消息队列,函数原型:

int msgget(key_t key, int msgflg);

(2)向队列读/写消息,函数原型如下所述:

msgrcvmsgrcvmsgrcv从队列中取用消息:

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

msgsndmsgsndmsgsnd将数据放到消息队列中:

int msgsnd(int msqid, cosnt void *msgp, size_t msgsz, int msgflg);
struct msgstru {
	long mtype;
	char mtext[512];
}

(3)设置消息队列属性,函数原型:

int msgctl(int msgqid, int cmd, struct msqid_ds *buf);

例1:用消息队列来传输数据

//msgreceive.cpp
/*************************************************************************
      > File Name: msgreceive.cpp
      > Author: ersheng
      > Mail: ershengaaa@163.com 
      > Created Time: Fri 01 Mar 2019 08:49:56 PM CST
 ************************************************************************/

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/msg.h>
using namespace std;
#define BUFSIZE 512
struct msg_st {
	long int msg_type;
	char text[BUFSIZE];
};
int main() {
	int running = 1;
	int msgid = -1;
	struct msg_st data;
	long int msgtype = 0;
	//建立消息队列 
	msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
	if (msgid == -1) {
		fprintf(stderr, "msgget failed with errno: %d\n", errno);
		exit(EXIT_FAILURE);
	}
	//从队列中获取消息,直到遇到end消息为止 
	while (running) {
		if (msgrcv(msgid, (void *)&data, BUFSIZE, msgtype, 0) == -1) {
			fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
			exit(EXIT_FAILURE);
		}	
		printf("You wrote: %s\n", data.text);
		//遇到end结束 
		if (strncmp(data.text, "text", 3) == 0) {
			running = 0;
		}
	}
	//删除消息队列 
	if (msgctl(msgid, IPC_RMID, 0) == -1) {
		fprintf(stderr, "msgctl(IPC_RMID) failed\n");
		exit(EXIT_FAILURE);
	}
	exit(EXIT_SUCCESS);
}
//msgsend.cpp
/*************************************************************************
      > File Name: msgsend.cpp
      > Author: ersheng
      > Mail: ershengaaa@163.com 
      > Created Time: Fri 01 Mar 2019 09:01:07 PM CST
 ************************************************************************/

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#include <errno.h>
using namespace std;
#define MAX_TEXT 512
#define BUFSIZE 512
struct msg_st {
	long int msg_type;
	char text[MAX_TEXT];
};
int main() {
	int running = 1;
	struct msg_st data;
	char buffer[BUFSIZE];
	int msgid = -1;
	//建立消息队列 
	msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
	if (msgid == -1) {
		fprintf(stderr, "msgget failed with error: %d\n", errno);
		exit(EXIT_FAILURE);
	}
	//向消息队列中写消息,直到写入end 
	while (running) {
		//输入数据 
		printf("Enter some text: ");
		fgets(buffer, BUFSIZE, stdin);
		data.msg_type = 1;
		strcpy(data.text, buffer);
		//向队列发送数据 
		if (msgsnd(msgid, (void *)&data, MAX_TEXT, 0) == -1) {
			fprintf(stderr, "msgsend failed\n");
			exit(EXIT_FAILURE);
		}
		//输入end结束输入 
		if (strncmp(buffer, "end", 3) == 0) {
			running = 0;
		}
		sleep(1);
	}
	exit(EXIT_SUCCESS);
}

在这里插入图片描述
在这里插入图片描述

消息队列与有名管道

相同点:

不同点:

标签:队列,msgid,间通信,int,消息,进程,msgsnd,include
来源: https://blog.csdn.net/qq_38790716/article/details/88092758