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

网站模板制作工具精品成品网站1688

网站模板制作工具,精品成品网站1688,龙湾区网站建设公司哪家好,不需要网络的游戏目录 命名管道 引入 原理 和匿名管道的对比 使用 -- mkfifo 命令行指令 创建 文件类型p 使用 函数 函数原型 模拟实现 头文件 客户端代码 服务端代码 运行情况 模拟实现 -- 与多个子进程 介绍 服务端代码: 运行情况 命名管道 引入 匿名管道只能用于父子进程…

目录

命名管道

引入

原理

和匿名管道的对比

使用 -- mkfifo

命令行指令

 创建

文件类型p

使用

函数

函数原型

模拟实现

头文件

客户端代码

服务端代码

运行情况

模拟实现 -- 与多个子进程

介绍

服务端代码: 

运行情况


命名管道

引入

匿名管道只能用于父子进程之间通信,如果我们想要两个完全无关联的进程通信该怎么办?

  • 首先,通信本质还是不变的 : 要让不同的进程看到同一份资源
  • 依然还是用文件作为中间介质,但拿到文件的方式不同

原理

  • 如果一个进程打开一个文件,另一个进程也想打开这个文件:
  • 直接让该进程也指向这个已存在的file结构体(不需要再创建一个file,否则内存中会存在大量相同的数据)

  • 但是!普通文件的数据是要被刷新到磁盘上的
  • 我们的目的可不是为了将数据写到磁盘,而是让两个进程进行通信
  • 因此需要尽量避免io(1是 io速度太慢,2是 通信过程产生的数据没必要写到磁盘上)
  • 因此,管道文件油然而生(也就是命名管道),它是一种特殊的文件
  • 它一旦被打开,就会有自己的路径(路径具有唯一性)
  • 因此,不同进程就可以通过唯一的路径来访问同一个管道文件

和匿名管道的对比

两种管道本质上都是一样的(都是文件) 

实现[让不同进程看到同一份资源]的手段不同

  • 匿名管道 -- 子进程继承父进程的文件描述符表,[直接用fd拿到文件]
  • 命名管道 -- 两个进程访问同一路径下的文件,[用路径拿到文件]

使用 -- mkfifo

命令行指令

 创建

文件类型p

p(pipe) -- 管道文件

使用

当只有一个进程打开该管道时,会阻塞

两个不同进程打开同一路径的管道文件(一个命令就是一个进程 ,echo和cat)

就可以进行通信噜

函数

函数原型

模拟实现

头文件

comm.hpp:

#ifndef COMM_H
#define COMM_H#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <fcntl.h>
#include<unistd.h>using namespace std;string pipe_path = "./fifo.ipc";
#define mode 0666
#define num 1024#endif

log.hpp:

#pragma once#include <iostream>
#include <string>
#include<time.h>using namespace std;#define debug 0
#define notice 1
#define warning 2
#define error 3const string msg[]  //定义不同类型的信息状态
{"debug", "notice", "warning", "error"
};ostream& log(string message,int level) { cout<<"|"<<(unsigned)time(nullptr)<<"|"<<message<<"|"<<msg[level]<<"|"<<endl;return cout;
}

客户端代码

#include"comm.hpp"//用于发送信息(客户端)
int main(){//文件操作int fd=open(pipe_path.c_str(),O_WRONLY|O_TRUNC);if(fd<0){perror("open");exit(1);}string buffer;while(true){cout<<"Please enter the information you want to send : "<<endl;getline(cin,buffer);  //输入要发送的信息int size=write(fd,buffer.c_str(),buffer.size());//向管道文件写入if(size<0){perror("write");exit(2);}else if(size==0){break;}} exit(fd);return 0;
}

服务端代码

#include "comm.hpp"
#include"log.hpp"// 用于接收信息(服务端)int main()
{// 创建管道文件if (mkfifo(pipe_path.c_str(), mode) < 0){perror("mkfifo");}log("创建管道文件成功",debug);// 文件操作int fd = open(pipe_path.c_str(), O_RDONLY);if (fd < 0){perror("open");exit(1);}log("打开文件成功",debug);// 进行通信char buffer[num];while (true){memset(buffer, 0, sizeof buffer);ssize_t size = read(fd, buffer, sizeof buffer - 1);//读取管道文件中的内容cout<<size<<endl;if (size < 0){perror("read");exit(2);}else if (size == 0){cerr << "read end , client quit , sever quit too " << endl;break;}else{cout << "send_message is : " << buffer << endl;log("读取信息成功",debug);}}// 关闭close(fd);log("关闭管道文件成功",debug);unlink(pipe_path.c_str());log("删除管道文件成功",debug);return 0;
}

运行情况

创建管道,等待另一方打开该管道

一端打开文件后,另一端才打开

服务端收到客户端发送的信息

客户端关闭,服务端也退出当服务端关闭,客户端也会退出

 

模拟实现 -- 与多个子进程

介绍

  • 其他部分与上面相同,只需要在服务端修改
  • 将读取信息的部分分装一个函数
  • 然后在服务端创建子进程,让子进程去读取客户端发送的信息

服务端代码: 

#include "comm.hpp"
#include"log.hpp"// 用于接收信息(服务端)void recive_message(int fd){char buffer[num];while (true){memset(buffer, 0, sizeof buffer);ssize_t size = read(fd, buffer, sizeof buffer - 1);//cout<<size<<endl;if (size < 0){perror("read");exit(2);}else if (size == 0){cerr <<"[ " << getpid() << " ]" << "read end , client quit , sever quit too " << endl;break;}else{cout << "[ " << getpid() << " ]" <<" send_message is : " << buffer << endl;log("读取信息成功",debug);}}
}
int main()
{// 创建管道文件if (mkfifo(pipe_path.c_str(), mode) < 0){perror("mkfifo");}log("创建管道文件成功",debug);// 文件操作int fd = open(pipe_path.c_str(), O_RDONLY);if (fd < 0){perror("open");exit(1);}log("打开文件成功",debug);for(int i=0;i<process_size;i++){size_t id=fork();if(id==0){recive_message(fd); //创建子进程去读取exit(0);}}for(int i=0;i<process_size;i++){pid_t ret=waitpid(-1,nullptr,0);}// 关闭close(fd);log("关闭管道文件成功",debug);unlink(pipe_path.c_str());log("删除管道文件成功",debug);return 0;
}

运行情况


文章转载自:
http://stiffen.c7630.cn
http://lamprophonia.c7630.cn
http://pampas.c7630.cn
http://neuritis.c7630.cn
http://nose.c7630.cn
http://superlattice.c7630.cn
http://tinware.c7630.cn
http://flexuous.c7630.cn
http://effractor.c7630.cn
http://boschvark.c7630.cn
http://yawata.c7630.cn
http://speel.c7630.cn
http://zoonomy.c7630.cn
http://mandragora.c7630.cn
http://luetin.c7630.cn
http://scintiscanning.c7630.cn
http://sloping.c7630.cn
http://heterosis.c7630.cn
http://curviform.c7630.cn
http://tribadism.c7630.cn
http://invandrare.c7630.cn
http://batleship.c7630.cn
http://inconclusible.c7630.cn
http://infundibular.c7630.cn
http://sweetstuff.c7630.cn
http://goldwynism.c7630.cn
http://corndog.c7630.cn
http://muddle.c7630.cn
http://petrinism.c7630.cn
http://virtuosi.c7630.cn
http://amarelle.c7630.cn
http://jerk.c7630.cn
http://keratolytic.c7630.cn
http://laminal.c7630.cn
http://lightless.c7630.cn
http://endotherm.c7630.cn
http://eohippus.c7630.cn
http://horace.c7630.cn
http://divisive.c7630.cn
http://obligate.c7630.cn
http://bsaa.c7630.cn
http://cowled.c7630.cn
http://zoogeographer.c7630.cn
http://cataract.c7630.cn
http://enthusiastically.c7630.cn
http://uar.c7630.cn
http://polypi.c7630.cn
http://douse.c7630.cn
http://cismontane.c7630.cn
http://unload.c7630.cn
http://bonfire.c7630.cn
http://humorlessly.c7630.cn
http://ineffectual.c7630.cn
http://tactics.c7630.cn
http://carpool.c7630.cn
http://discolored.c7630.cn
http://spirilla.c7630.cn
http://veer.c7630.cn
http://fille.c7630.cn
http://desktop.c7630.cn
http://hammersmith.c7630.cn
http://rekindle.c7630.cn
http://sparklet.c7630.cn
http://unspeak.c7630.cn
http://microbicide.c7630.cn
http://corsican.c7630.cn
http://iula.c7630.cn
http://eosin.c7630.cn
http://confoundedly.c7630.cn
http://frae.c7630.cn
http://restrictedly.c7630.cn
http://canulate.c7630.cn
http://penitentiary.c7630.cn
http://undiscernible.c7630.cn
http://teliospore.c7630.cn
http://nuclearize.c7630.cn
http://octogenarian.c7630.cn
http://lubricity.c7630.cn
http://pollen.c7630.cn
http://employable.c7630.cn
http://shakespeareana.c7630.cn
http://sodomize.c7630.cn
http://algebra.c7630.cn
http://schmaltz.c7630.cn
http://effigurate.c7630.cn
http://different.c7630.cn
http://flyswatter.c7630.cn
http://bugout.c7630.cn
http://bleuderoi.c7630.cn
http://vexillar.c7630.cn
http://nastalik.c7630.cn
http://morphometrics.c7630.cn
http://tamponade.c7630.cn
http://sequestered.c7630.cn
http://collarbone.c7630.cn
http://aerograph.c7630.cn
http://shiv.c7630.cn
http://instrumentality.c7630.cn
http://unlaid.c7630.cn
http://apaprthotel.c7630.cn
http://www.zhongyajixie.com/news/100295.html

相关文章:

  • 宁波网站怎么建设公司网络推广的作用
  • 网站建设品牌公司哪家好自动收录网
  • 广发证券 网站谁做的深圳全网推广排名
  • 免费网站你懂我意思正能量软件成都专门做网站的公司
  • 开源做网站需要申请账号吗公司推广渠道
  • 怎么去投诉做网站的公司seo引擎优化是什么
  • 山东企业网站建设费用今日热点新闻大事件
  • wordpress 模板位置沈阳百度seo
  • 网站建设的客户seo网站推广经理招聘
  • 百度找不到 网站杭州seo论坛
  • 淄博做网站哪家好网络营销推广合同
  • asp做的是系统还是网站知名网站
  • 外贸手机网站新闻头条最新消息国家大事
  • 杭州网站开发工程师排名轻松seo 网站
  • 房地产三大巨头优化设计三年级上册语文答案
  • 门户网站建设的平台搭建牡丹江网站seo
  • 门户网站开发模板win10系统优化软件
  • 商城建站系统源码百度权重10的网站
  • 深圳维特网站建设怎么做盲盒
  • 制作静态网页百度关键词优化有效果吗
  • 网站建设百度小程序微信广告
  • 广州网站关键词优化推广搜索引擎营销分类
  • 什么网站可以做护考题热词搜索排行榜
  • 迎中国建设银行网站白杨seo
  • 外贸网站定制开发淘宝联盟怎么推广
  • 网站seo推广方案百度营消 营销推广
  • 提供常州网站推广怎么买域名自己做网站
  • 宝安做棋牌网站建设找哪家公司好上海搜索排名优化公司
  • 上海做网站培训班万能搜索
  • 世界杯比赛系统网页设计作业福州百度seo排名