做推送网站,做视频网站 许可,h5制作软件免费手机版下载,深圳莲花大厦住房和建设局网站C进程间通信 消息队列 消息队列概述消息队列代码示例1. 创建和发送消息的程序#xff08;sender.cpp#xff09;2. 接收消息的程序#xff08;receiver.cpp#xff09; 代码解释运行步骤运行结果 消息队列概述
消息队列是一种进程间通信机制#xff0c;允许一个或多个进程… C进程间通信 消息队列 消息队列概述消息队列代码示例1. 创建和发送消息的程序sender.cpp2. 接收消息的程序receiver.cpp 代码解释运行步骤运行结果 消息队列概述
消息队列是一种进程间通信机制允许一个或多个进程通过消息的形式进行通信。消息队列可以在内核中维护提供了一个先进先出的队列支持消息的优先级排序。
消息队列代码示例
1. 创建和发送消息的程序sender.cpp
#include sys/types.h
#include sys/ipc.h
#include sys/msg.h
#include iostream
#include cstring
#include cstdlib#define MSGQ_KEY 1234
#define MSG_SIZE 128struct message
{long msg_type;char msg_text[MSG_SIZE];
};int main()
{int msgid msgget(MSGQ_KEY, 0666 | IPC_CREAT); // 创建消息队列if (msgid -1) {perror(msgget);exit(EXIT_FAILURE);}message msg;msg.msg_type 1; // 消息类型strcpy(msg.msg_text, Hello from sender process!); // 消息内容if (msgsnd(msgid, msg, sizeof(msg.msg_text), 0) -1) { // 发送消息perror(msgsnd);exit(EXIT_FAILURE);}std::cout Message sent: msg.msg_text std::endl;return 0;
}2. 接收消息的程序receiver.cpp
#include sys/types.h
#include sys/ipc.h
#include sys/msg.h
#include iostream
#include cstring
#include cstdlib#define MSGQ_KEY 1234
#define MSG_SIZE 128struct message
{long msg_type;char msg_text[MSG_SIZE];
};int main()
{int msgid msgget(MSGQ_KEY, 0666 | IPC_CREAT); // 获取消息队列if (msgid -1) {perror(msgget);exit(EXIT_FAILURE);}message msg;if (msgrcv(msgid, msg, sizeof(msg.msg_text), 1, 0) -1) { // 接收消息perror(msgrcv);exit(EXIT_FAILURE);}std::cout Message received: msg.msg_text std::endl;if (msgctl(msgid, IPC_RMID, nullptr) -1) { // 删除消息队列perror(msgctl);exit(EXIT_FAILURE);}return 0;
}代码解释 创建和发送消息的程序sender.cpp msgget(MSGQ_KEY, 0666 | IPC_CREAT): 创建一个消息队列如果消息队列不存在则创建否则获取现有的消息队列。定义一个message结构包含消息类型和消息文本。设定消息类型并赋值消息文本。msgsnd(msgid, msg, sizeof(msg.msg_text), 0): 发送消息到消息队列。打印发送的消息。 接收消息的程序receiver.cpp msgget(MSGQ_KEY, 0666 | IPC_CREAT): 获取消息队列。定义一个message结构包含消息类型和消息文本。msgrcv(msgid, msg, sizeof(msg.msg_text), 1, 0): 从消息队列接收类型为1的消息。打印接收到的消息。msgctl(msgid, IPC_RMID, nullptr): 删除消息队列。
运行步骤
分别编译sender.cpp和receiver.cppg sender.cpp -o sender
g receiver.cpp -o receiver打开两个终端在第一个终端运行接收程序./receiver在第二个终端运行发送程序./sender运行结果
第一个终端运行接收程序输出
Message received: Hello from sender process!第二个终端运行发送程序输出
Message sent: Hello from sender process!
文章转载自: http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.nwnbq.cn.gov.cn.nwnbq.cn http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn http://www.morning.hpmzs.cn.gov.cn.hpmzs.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.qqxmj.cn.gov.cn.qqxmj.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.rnds.cn.gov.cn.rnds.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.lbhck.cn.gov.cn.lbhck.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.gzttoyp.com.gov.cn.gzttoyp.com http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.rtmqy.cn.gov.cn.rtmqy.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.prprz.cn.gov.cn.prprz.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.xwgbr.cn.gov.cn.xwgbr.cn http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.fxzgw.com.gov.cn.fxzgw.com http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn