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

中央决定唐山秦皇岛合并宁波seo关键词优化制作

中央决定唐山秦皇岛合并,宁波seo关键词优化制作,企业专业搜索引擎优化,展会宣传推广计划文章目录 命名管道原理命令创建命名管道函数创建命名管道 共享内存原理shmgetFIOK 代码应用:premsnattch 命名管道 用于两个毫无关系的进程间的通信。 原理 Linux文件的路径是多叉树,故文件的路径是唯一的。 让内核缓冲区不用刷新到磁盘中&#xff0c…

文章目录

  • 命名管道
    • 原理
    • 命令创建命名管道
    • 函数创建命名管道
  • 共享内存
    • 原理
    • shmget
      • FIOK
    • 代码应用:
      • prems
      • nattch

命名管道

用于两个毫无关系的进程间的通信。

原理

Linux文件的路径是多叉树,故文件的路径是唯一的。
让内核缓冲区不用刷新到磁盘中,一旦刷新就拖慢了操作系统。所以磁盘中有个特殊文件,在内存中写入不会刷新到磁盘,让两个进程在内存中通信,该文件叫做命名管道。(命名:有路径就有名字;管道:用于内存通信)
在这里插入图片描述

命令创建命名管道

可以直接使用系统命令创建命名管道
在这里插入图片描述
p打头的文件就是管道文件
在这里插入图片描述
echo是进程,cat也是进程。两个进程基于管道实现了通信。写入的时候,管道的大小依旧为0。
在这里插入图片描述

函数创建命名管道

在这里插入图片描述
返回值:成功返回0,失败返回-1
在这里插入图片描述
删除指定路径的文件:unlink
在这里插入图片描述
返回值:成功返回0,失败返回-1
在这里插入图片描述
代码如下:
client.cc 负责写:

#include "namedPipe.hpp"// client write
int main()
{NamePiped fifo(comm_path, User);if (fifo.OpenForWrite()){std::cout << "client open namd pipe done" << std::endl;while (true){std::cout << "Please Enter > ";std::string message;getline(std::cin, message);fifo.WriteNamedpipe(message);}}return 0;
}

namePipe.hpp:

#pragma once#include <iostream>
#include <cstdio>
#include <cerrno>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>const std::string comm_path = "./myfifo";//管道的名字
#define DefaultFd -1
#define Creater 1
#define User 2
#define Read O_RDONLY
#define Write O_WRONLY
#define BaseSize 4096//一次读多少字节class NamePiped
{
private:bool OpenNamedPipe(int mode)//权限{_fd = open(comm_path.c_str(),mode);if(_fd < 0)return false;return true;}
public:NamePiped(const std::string &path, int who) : _fifo_path(path), _id(who),_fd(DefaultFd){if (_id == Creater){int res = mkfifo(_fifo_path.c_str(), 0666);if (res != 0){perror("mkfifo");}std::cout << "Creater creat Named pipe" << std::endl;}}bool OpenForRead(){return OpenNamedPipe(Read);}bool OpenForWrite(){return OpenNamedPipe(Write);}int ReadNamedpipe(std::string *out){char buffer[BaseSize];int n = read(_fd,buffer,sizeof(buffer));if(n > 0){buffer[n] = 0;*out = buffer;}return n;}int WriteNamedpipe(const std::string &in){return write(_fd,in.c_str(),in.size());}~NamePiped(){if (_id == Creater){sleep(5);int res = unlink(_fifo_path.c_str());if (res != 0){perror("unlink");}std::cout << "Creater free Named pipe" << std::endl;}if(_fd != DefaultFd) close(_fd);}private:const std::string _fifo_path;  //管道路径int _id;  //身份(user/creater)int _fd;  //文件描述符
};

server.cc 读端:

#include "namedPipe.hpp"// server read:管理命名管道的生命周期(创建与删除)
int main()
{NamePiped fifo(comm_path, Creater);if(fifo.OpenForRead()){std::cout << "server open named pipe done" << std::endl;while (true){std::string message;int res = fifo.ReadNamedpipe(&message);if (res > 0)//读内容{std::cout << "Client send >" << message << std::endl;}else if(res == 0)//写端关闭{std::cout << "Client quit" << std::endl;break;}else//出错{std::cout << "fifo.ReadNamedpipe default" << std::endl;break;}}}return 0;
}

运行:
在这里插入图片描述
光运行./server(读端),不运行写端会创建出管道,但读没有打印出server open named pipe done。说明没有打开管道。
在这里插入图片描述
然后运行了./client(写端)才会打开管道。说明对于读端而言,如果我们打开文件,但还没有写端,就会阻塞在open调用中,直到对方打开。简称进程同步。
在这里插入图片描述
相反如果读端关闭,写端还在写,写端就会收到SIGPIPE信号,让写端进程直接退出
在这里插入图片描述

共享内存

匿名管道和命名管道就是复用文件的代码。
有人从零开始写本地通信方案的代码:System V IPC 有三种方式通信
在这里插入图片描述
因为只能本地通信,且和文件的整合度不高,所以目前这种方案已经快被淘汰了。

原理

假设A进程在物理内存中创建一段内存空间,然后在A进程的地址空间中的共享区申请一片空间,再把虚拟地址与共享内存的映射关系填入页表。拥有映射关系后就可以往创建的内存中写入。B进程的虚拟地址也通过页表与创建的内存映射。这样进程A与进程B看到同一块资源,上面的技术叫做共享内存。
在这里插入图片描述
补充:
1.以上操作都是由操作系统做的,所以操作系统肯定提供给用户系统调用了。
2.AB进程通信,CD进程也要通信,所以共享进程在系统中可以存在很多份,且功能也不一样。操作系统就要对共享内存做管理!先描述,再组织。类似struct Shm的结构体。
在这里插入图片描述
综上所述:共享内存 = 共享内存空间(数据) + 共享内存属性

shmget

创建共享内存的函数调用:IPC_CREAT用于获取;IPC_CREAT | IPC_EXCL用于创建。
在这里插入图片描述
key比较特殊,是标识共享内存的唯一性字段,用于寻找共享空间。
在这里插入图片描述
综上:用户设置key值,AB两个进程就能看见同一块共享空间。
key值是给用户看的,后面会说shmid是给操作系统看的,所以操作系统不创建key值。
但是又不建议用户自己设置key,容易冲突,就给用户提供了函数ftok,由一些算法形成的随机数。

FIOK

同一个pathname,同一个proj_id,就能由算法形成同一个key。
在这里插入图片描述
返回值:成功后返回的是key的值;失败后返回-1
在这里插入图片描述

代码应用:

先验证共享内存的几个特性:
共享内存,不随着进程的结束释放。
Shm.hpp

#ifndef __SHM_HPP__
#define __SHM_HPP__#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cerrno>#define gShmSize 4096const std::string gpathname = "/root/test/shm";
const int gproj_id = 0x66;std::string ToHex(key_t k)
{char buffer[128];snprintf(buffer,sizeof(buffer),"0x%x",k);return buffer;
}key_t GetCommKey(const std::string pathname, int proj_id)
{key_t res = ftok(pathname.c_str(), proj_id);if (res < 0){perror("create ftok failing");}return res;
}int GetShm(int key)
{int shmid = shmget(key,gShmSize,IPC_CREAT | IPC_EXCL);if(shmid < 0){perror("shmget fail");}return shmid;
}#endif

server.cc

#include "Shm.hpp"int main()
{key_t k = GetCommKey(gpathname,gproj_id);std::cout << "key: " << ToHex(k) << std::endl;int shmid = GetShm(k);std::cout << "shmid:" << shmid << std::endl;return 0;
}

运行两次,发现第二次并没有创建,并显示已经存在,说明共享内存并没有像子进程一样运行完就被父进程或系统回收。
在这里插入图片描述
把创建共享内存改成获取共享内存:

int GetShm(int key)
{int shmid = shmget(key,gShmSize,IPC_CREAT);if(shmid < 0){perror("shmget fail");}return shmid;
}

发现获取的话可以一直获取。
在这里插入图片描述
如果不释放共享内存,就会一直存在,生命周期随内核。
查共享内存:

ipcs -m

在这里插入图片描述
key:属于用户形成,内核使用的一个字段,用户不能使用key来进行shm管理。
shmid:内核给用户返回的一个标识符,用来进行用户级对共享内存进行管理的id值。
删共享内存用命令shmid删除:

ipcrm -m [shmid]

在这里插入图片描述
也可以用操作系统提供的函数:shmctl删除。cmd删除是IPC_RMID,就是位图。
在这里插入图片描述
返回值:移植成功,返回0;失败,返回-1
在这里插入图片描述
综上所述:创建共享内存,使用后删除的代码就能写出来了:

#ifndef __SHM_HPP__
#define __SHM_HPP__#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <cerrno>#define gCreater 1
#define gUser 2
#define gShmSize 4096const std::string gpathname = "/root/test/shm";
const int gproj_id = 0x66;class Shm
{
private:key_t GetCommKey(){key_t key = ftok(_pathname.c_str(), _proj_id);if (key < 0){perror("create ftok failing");}return key;}int GetShmHelper(key_t key, int size, int flag){int shmid = shmget(key, size, flag);if (shmid < 0){perror("shmget fail");}return shmid;}public:Shm(const std::string pathname, const int proj_id, int who): _pathname(pathname), _proj_id(proj_id), _who(who){_key = GetCommKey();if (_who == gCreater){CreaterGetShmid();}else if (_who == gUser){UserGetShmid();}std::cout << "shmid: " << _shmid << std::endl;std::cout << "key: " << ToHex(_key) << std::endl;}~Shm(){if (_who == gCreater){int res = shmctl(_shmid, IPC_RMID, nullptr);if (res < 0)std::cout << "shmctl fail " << std::endl;elsestd::cout << "shmid remove done " << std::endl;}}std::string ToHex(key_t k){char buffer[128];snprintf(buffer, sizeof(buffer), "0x%x", k);return buffer;}bool CreaterGetShmid(){if (_who == gCreater){_shmid = GetShmHelper(_key, gShmSize, IPC_CREAT | IPC_EXCL);sleep(10); //为了创建完看见删除的效果if (_shmid > 0)return true;}std::cout << "Create shmid succeed" << std::endl;return false;}bool UserGetShmid(){if (_who == gUser){_shmid = GetShmHelper(_key, gShmSize, IPC_CREAT);if (_shmid > 0)return true;}std::cout << "Get shmid succeed" << std::endl;return false;}private:key_t _key;int _shmid;int _who;const std::string _pathname;const int _proj_id;
};#endif

创建出共享内存后10秒钟后删除
在这里插入图片描述

prems

perms是共享内存的权限:如果创建的时候,加上权限,perms的值会更改。

bool CreaterGetShmid(){if (_who == gCreater){_shmid = GetShmHelper(_key, gShmSize, IPC_CREAT | IPC_EXCL | 0666);if (_shmid > 0)return true;}std::cout << "Create shmid succeed" << std::endl;return false;}

在这里插入图片描述

nattch

nattch是该共享内存挂接的数量。
挂接的函数:shmat(at有attach的意思)
在这里插入图片描述
返回值:一旦挂接成功,返回地址空间中共享内存的起始地址。(跟malloc返回值类似)
挂接代码:

std::string RoleToString(int who){if (who == gCreater)return "Creater";else if (who == gUser)return "gUser";elsereturn "None";}// 挂接void *AttachShm(){void *shmaddr = shmat(_shmid, nullptr, 0);if (shmaddr == nullptr){perror("shmat");}std::cout << "who: " << RoleToString(_who) << " attach shm..." << std::endl;return shmaddr;}

一个进程挂接上了为1,两个进程挂接上了为2。
在这里插入图片描述
去掉进程与共享内存的关联的函数:shmdt
在这里插入图片描述
代码如下:

void DetachShm(void *shmaddr){if(shmaddr == nullptr) return;shmdt(shmaddr);std::cout << "who: " << RoleToString(_who) << " detach shm..." << std::endl;}

效果:0-》1-》0
在这里插入图片描述
上面都是准备工作:下面开始通信。
只运行读端:
现象:发现不等写端写入,读端一直在读。
在这里插入图片描述
读端和写端都运行:
现象:写端2秒写一次,读端1秒读1次,数据就有重复。(相比较通道读完数据就没有了)
在这里插入图片描述
读端和写端都运行,然后把写端关闭后再次运行写端:
现象:读端重新读取
在这里插入图片描述
综上所述:与管道不一样,共享内存不提供保护机制。
缺点:会造成数据不一致问题(在纸上写字,写一半被别人拿走了)
优点:共享内存收所有进程IPC,速度最快的。因为共享内存大大减少了数据的拷贝次数。
在这里插入图片描述


文章转载自:
http://dinncowrongheaded.tpps.cn
http://dinncotachygrapher.tpps.cn
http://dinncocuragh.tpps.cn
http://dinncokingsun.tpps.cn
http://dinncomonogamic.tpps.cn
http://dinncovegas.tpps.cn
http://dinncociaa.tpps.cn
http://dinncotypescript.tpps.cn
http://dinncosweatbox.tpps.cn
http://dinncochartography.tpps.cn
http://dinncopaygrade.tpps.cn
http://dinncoenchondrosis.tpps.cn
http://dinncoportfolio.tpps.cn
http://dinncoantialcoholism.tpps.cn
http://dinncovimineous.tpps.cn
http://dinncosupramaximal.tpps.cn
http://dinncomesocardium.tpps.cn
http://dinncocroatan.tpps.cn
http://dinncocloche.tpps.cn
http://dinncohydrolysate.tpps.cn
http://dinncochateaubriand.tpps.cn
http://dinncocarse.tpps.cn
http://dinncorescuee.tpps.cn
http://dinncocoenesthesia.tpps.cn
http://dinncotaurocholic.tpps.cn
http://dinncodissymmetry.tpps.cn
http://dinncobeachy.tpps.cn
http://dinncominibike.tpps.cn
http://dinncochawl.tpps.cn
http://dinncoanthracitic.tpps.cn
http://dinncopreclusive.tpps.cn
http://dinncophantasmal.tpps.cn
http://dinncofully.tpps.cn
http://dinncoviolone.tpps.cn
http://dinncounclinch.tpps.cn
http://dinncoetrog.tpps.cn
http://dinncopresbyterianism.tpps.cn
http://dinncobeerless.tpps.cn
http://dinncoaccusation.tpps.cn
http://dinncoclod.tpps.cn
http://dinncoshebeen.tpps.cn
http://dinncocoin.tpps.cn
http://dinncokalinin.tpps.cn
http://dinncoreverberator.tpps.cn
http://dinncopentecostal.tpps.cn
http://dinncosnowdrift.tpps.cn
http://dinncohaemoglobin.tpps.cn
http://dinncoencephalasthenia.tpps.cn
http://dinncoconcomitance.tpps.cn
http://dinncohant.tpps.cn
http://dinncoinvestigative.tpps.cn
http://dinncoduodenitis.tpps.cn
http://dinncocodlin.tpps.cn
http://dinncolanguistics.tpps.cn
http://dinncotandour.tpps.cn
http://dinncostandpatter.tpps.cn
http://dinncosemisteel.tpps.cn
http://dinncoimplicate.tpps.cn
http://dinncodruggie.tpps.cn
http://dinncogandhian.tpps.cn
http://dinncogodly.tpps.cn
http://dinncoequalitarian.tpps.cn
http://dinncocaplin.tpps.cn
http://dinncoapproximatively.tpps.cn
http://dinncothenceforward.tpps.cn
http://dinncoother.tpps.cn
http://dinncochibchan.tpps.cn
http://dinncosever.tpps.cn
http://dinncomudfish.tpps.cn
http://dinncofavela.tpps.cn
http://dinncococoon.tpps.cn
http://dinncoquartzite.tpps.cn
http://dinncoloadstone.tpps.cn
http://dinncohoopoe.tpps.cn
http://dinncorespirometric.tpps.cn
http://dinncoindissociably.tpps.cn
http://dinncounseasoned.tpps.cn
http://dinncodisenablement.tpps.cn
http://dinncounconceivable.tpps.cn
http://dinncosenora.tpps.cn
http://dinncosabbatism.tpps.cn
http://dinncoakkra.tpps.cn
http://dinncocanaanitic.tpps.cn
http://dinncoinspired.tpps.cn
http://dinncowist.tpps.cn
http://dinncodutchman.tpps.cn
http://dinncostrut.tpps.cn
http://dinncolibidinous.tpps.cn
http://dinncopermutable.tpps.cn
http://dinncoarchenteric.tpps.cn
http://dinncopoem.tpps.cn
http://dinncorpi.tpps.cn
http://dinncouncorrectable.tpps.cn
http://dinncorosario.tpps.cn
http://dinncoepistyle.tpps.cn
http://dinncopdh.tpps.cn
http://dinncoinstruction.tpps.cn
http://dinncothyristor.tpps.cn
http://dinncosmallage.tpps.cn
http://dinncosalii.tpps.cn
http://www.dinnco.com/news/133816.html

相关文章:

  • 企业网站建设公司哪家好信息流优化师招聘
  • 钓鱼网站的危害网络推广公司介绍
  • div css做网站实例网页设计
  • 商城网站开发制作东莞快速排名
  • 青岛网站建设公司 中小企业补贴seo免费教程
  • 四川建设网自主招标网免费seo免费培训
  • 网站建设+青海中国疫情最新情况
  • 做网站怎么赚钱吗佛山百度提升优化
  • 武汉市做网站指数函数
  • 网站名称注册程序做推广哪个平台效果好
  • 江西中耀建设集团有限公司网站百度公司简介介绍
  • 安装网站程序百度指数网址是什么
  • 怎么做网站的banner宁波seo网站排名优化公司
  • 朝阳周边网站建设北京网络营销外包公司哪家好
  • 建设通网站登录不进去app营销策略都有哪些
  • webform 做网站好不好百度宁波运营中心
  • 五八同城招聘网找工作北京seo业务员
  • 网站风险解除益阳网站seo
  • wordpress pagebuilderseo分析seo诊断
  • 怎么制作个人门户网站我们公司在做网站推广
  • 建设项目备案网站管理系统石家庄网络营销网站推广
  • 如何快速备案网站成都关键词seo推广平台
  • 网站开发人员属于什么谷歌浏览器官网下载安装
  • 温州网站建设 温州网站制作成都网站排名优化公司
  • 新洲建设投标网站网址缩短
  • 做唯品客网站的感想网络营销师报名入口
  • 晚上必看的正能量视频下载培训seo去哪家机构最好
  • 宿舍网站建设目的培训网站推广
  • 晚上做设计挣钱的网站六六seo基础运营第三讲
  • 网站怎么做dwcs6新产品怎样推广