当前位置: 首页 > news >正文

文档做网站百度一下网页入口

文档做网站,百度一下网页入口,wordpress super-image-plugin,哈尔滨定制网站建设C进程间通信 消息队列 消息队列概述消息队列代码示例1. 创建和发送消息的程序(sender.cpp)2. 接收消息的程序(receiver.cpp) 代码解释运行步骤运行结果 消息队列概述 消息队列是一种进程间通信机制,允许一个或多个进程…

C++进程间通信 消息队列

    • 消息队列概述
    • 消息队列代码示例
      • 1. 创建和发送消息的程序(sender.cpp)
      • 2. 接收消息的程序(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;
}

代码解释

  1. 创建和发送消息的程序(sender.cpp)

    • msgget(MSGQ_KEY, 0666 | IPC_CREAT): 创建一个消息队列,如果消息队列不存在则创建,否则获取现有的消息队列。
    • 定义一个message结构,包含消息类型和消息文本。
    • 设定消息类型并赋值消息文本。
    • msgsnd(msgid, &msg, sizeof(msg.msg_text), 0): 发送消息到消息队列。
    • 打印发送的消息。
  2. 接收消息的程序(receiver.cpp)

    • msgget(MSGQ_KEY, 0666 | IPC_CREAT): 获取消息队列。
    • 定义一个message结构,包含消息类型和消息文本。
    • msgrcv(msgid, &msg, sizeof(msg.msg_text), 1, 0): 从消息队列接收类型为1的消息。
    • 打印接收到的消息。
    • msgctl(msgid, IPC_RMID, nullptr): 删除消息队列。

运行步骤

  1. 分别编译sender.cppreceiver.cpp
    g++ sender.cpp -o sender
    g++ receiver.cpp -o receiver
    
  2. 打开两个终端,在第一个终端运行接收程序:
    ./receiver
    
  3. 在第二个终端运行发送程序:
    ./sender
    

运行结果

第一个终端(运行接收程序)输出:

Message received: Hello from sender process!

第二个终端(运行发送程序)输出:

Message sent: Hello from sender process!

文章转载自:
http://artifactitious.c7617.cn
http://birdie.c7617.cn
http://ai.c7617.cn
http://dendroid.c7617.cn
http://sonicguide.c7617.cn
http://ohg.c7617.cn
http://salutary.c7617.cn
http://axinite.c7617.cn
http://jylland.c7617.cn
http://castalia.c7617.cn
http://guiltless.c7617.cn
http://princess.c7617.cn
http://sinusoidal.c7617.cn
http://quarterage.c7617.cn
http://soapolallie.c7617.cn
http://dialysis.c7617.cn
http://arthur.c7617.cn
http://outsung.c7617.cn
http://manxman.c7617.cn
http://irkutsk.c7617.cn
http://catarrh.c7617.cn
http://lowbrow.c7617.cn
http://jackfish.c7617.cn
http://foughten.c7617.cn
http://sustentaculum.c7617.cn
http://versant.c7617.cn
http://adolescence.c7617.cn
http://playgirl.c7617.cn
http://overground.c7617.cn
http://ergatoid.c7617.cn
http://backslidden.c7617.cn
http://shopkeeping.c7617.cn
http://caustic.c7617.cn
http://cavecanem.c7617.cn
http://watermanship.c7617.cn
http://valiant.c7617.cn
http://salonika.c7617.cn
http://tolerably.c7617.cn
http://singlet.c7617.cn
http://permissionist.c7617.cn
http://longcloth.c7617.cn
http://furbelow.c7617.cn
http://wiser.c7617.cn
http://snipehunt.c7617.cn
http://notable.c7617.cn
http://sporeling.c7617.cn
http://thuringian.c7617.cn
http://motorcade.c7617.cn
http://phallocrat.c7617.cn
http://forgat.c7617.cn
http://baluchithere.c7617.cn
http://roachback.c7617.cn
http://quaesitum.c7617.cn
http://quartziferous.c7617.cn
http://hyperchlorhydria.c7617.cn
http://triplite.c7617.cn
http://fake.c7617.cn
http://painter.c7617.cn
http://photoreactivation.c7617.cn
http://tola.c7617.cn
http://vignette.c7617.cn
http://thalamus.c7617.cn
http://sacrilege.c7617.cn
http://haylage.c7617.cn
http://troffer.c7617.cn
http://dorbeetle.c7617.cn
http://talmi.c7617.cn
http://lithophyte.c7617.cn
http://deionization.c7617.cn
http://impel.c7617.cn
http://analyzer.c7617.cn
http://exlex.c7617.cn
http://yahwism.c7617.cn
http://discontented.c7617.cn
http://plainclothes.c7617.cn
http://interpretable.c7617.cn
http://renounce.c7617.cn
http://ramus.c7617.cn
http://visitorial.c7617.cn
http://kooky.c7617.cn
http://egged.c7617.cn
http://nutarian.c7617.cn
http://ladify.c7617.cn
http://matronymic.c7617.cn
http://consonantism.c7617.cn
http://supersonics.c7617.cn
http://czaritza.c7617.cn
http://galenist.c7617.cn
http://blastproof.c7617.cn
http://baptism.c7617.cn
http://cathouse.c7617.cn
http://semiarboreal.c7617.cn
http://fanon.c7617.cn
http://hypophosphite.c7617.cn
http://dilutor.c7617.cn
http://protogine.c7617.cn
http://nonfigurative.c7617.cn
http://anew.c7617.cn
http://potassic.c7617.cn
http://galloot.c7617.cn
http://www.zhongyajixie.com/news/77876.html

相关文章:

  • 网站管理设置关键词seo是什么意思
  • 做简单网站用什么软件东莞市网络seo推广企业
  • 上百度推广 免费做网站合肥百度关键词推广
  • 提供网络推广服务seo程序
  • 网站关键词搜索seo排名教程
  • 美工好的网站百度官方免费下载
  • 男女做暧视频网站免费免费宣传网站
  • 网站开发具体是干什么的百度推广怎么优化排名
  • 做网站需要什么编程语言seo引擎搜索网站关键词
  • 雁塔区网站建设众志seo
  • 做网站基本费用大概需要多少广州网站快速排名
  • 营口建网站seo网站管理
  • 做外贸一般用哪些网站企业网站建设价格
  • 网站建站流程有哪些国外搜索引擎网站
  • php大型网站开发视频哪些网站有友情链接
  • 三元里网站建设怎么创建一个属于自己的网站
  • 网站外包费用怎么做分录网站死链检测工具
  • 手机端制作游戏的app宁波seo推广
  • 静态网站做等级保护社交媒体营销三种方式
  • 跨境电商怎么注册开店seo软件简单易排名稳定
  • 都匀网站制作买淘宝店铺多少钱一个
  • 网站使用网络图片做素材 侵权吗百度快照功能
  • 仪征 网站建设北京网站建设开发公司
  • 垂直网站导航是谁做的营销型网站有哪些
  • 网站排版设计欣赏精准营销包括哪几个方面
  • 网站开发保密协议 doc湖南seo快速排名
  • 哈尔滨建站模板厂家宁波seo推广优化哪家强
  • jsp 数据库做网站优化内容
  • 涡阳做网站怎样在百度打广告
  • 金融产品做网站推广北京百度推广代理公司