资阳网站设计陕西省咸阳市建设银行网站
web/
2025/10/6 19:16:18/
文章来源:
资阳网站设计,陕西省咸阳市建设银行网站,easy ssl wordpress,青岛seo优化介绍
进程间通信的方式 1.Linux原生支持的管道----匿名和命名管道 2.System V-----共享内存、消息队列、信号量 3.Posix------多线程、网路通信 进程间通信目的 数据传输#xff1a;一个进程需要将它的数据发送给另一个进程 资源共享#xff1a;多个进程之间共享同样的资源。…介绍
进程间通信的方式 1.Linux原生支持的管道----匿名和命名管道 2.System V-----共享内存、消息队列、信号量 3.Posix------多线程、网路通信 进程间通信目的 数据传输一个进程需要将它的数据发送给另一个进程 资源共享多个进程之间共享同样的资源。 通知事件一个进程需要向另一个或一组进程发送消息通知它它们发生了某种事件如进程终止时要通知父进程。 进程控制有些进程希望完全控制另一个进程的执行如Debug进程此时控制进程希望能够拦截另一个进程的所有陷入和异常并能够及时知道它的状态改变 管道
原理
管道是单向通信的常用于有亲缘关系的父子间通信父进程调用pipe打开管道文件操作系统创建struct file结构体存有inode等文件信息父进程fd指向该文件父进程fork出子进程子进程也拷贝父进程的代码和fd同时fd指向管道分别关闭读端和写端从而实现单向通信。
创建管道 调用成功返回0失败返回-1。pipefd是输出型参数pipefd【0】为3pipefd【1】为4.
构建单向通信的读端子进程关闭写端
//2.create child processpid_t idfork();assert(id!-1);if(id0){//child process//3构建单向通行的信道pipe[0]:read,pipe[1]:write//3.1关闭子进程不需要的fdclose(pipefd[1]);char buffer[1025];while(true){ssize_t sread(pipefd[0],buffer,sizeof(buffer)-1);if(s0){buffer[s]0;coutchild get a message[getpid()]fatherbufferendl;}}exit(0);}写端父进程关闭读端 close(pipefd[0]);string message我是父进程我正在给你发消息;int count0;char send_buffer[1024];while(true){//3.2构建一个变化的字符串snprintf(send_buffer,sizeof(send_buffer),%s:%d,message.c_str(),count);//3.3写入write(pipefd[1],send_buffer,strlen(send_buffer));//3.4故意睡一会sleep(1);}pid_t retwaitpid(id,nullptr,0);assert(ret0);(void)ret;close(pipefd[1]);管道特点 1.常用于亲缘关系的父子间通信 2.提供访问控制例如管道无数据时读端就必须等数据写入 3.管道本质是内核中的一块缓冲区多个进程通过访问同一块缓冲区实现通信。 4. 管道提供的是面向流式的通信服务面向字节流需要定制协议来进行数据区分。 5.管道是基于文件的文件的生命周期是随进程的那么管道的生命周期也是随进程的。 6.管道是单向通信的就是半双工通信的一种特殊情况数据只能向一个方向流动。需要双方通信时需要建立起两个管道。半双工通信就是要么在收数据要么在发数据不能同时在收数据和发数据比如两个人在交流时一个人在说另一个人在听而全双工通信是同时进行收数据和发数据比如两个人吵架的时候相互问候对方一个人既在问候对方又在听对方的问候。 实现一个简易进程池
process.cc
#includeiostream
#includeunistd.h
#includestdlib.h
#includesys/wait.h
#includesys/types.h
#includeassert.h
#includevector
#includecstdlib
#includetime.h
#includeTask.hpp#define PROCESS_NUM 5using namespace std;int waitCommand(int waitFd,bool quit)
{uint32_t command0;ssize_t sread(waitFd,command,sizeof(command));if(s0){quittrue;return -1;}assert(ssizeof(uint32_t));return command;
}//给哪一个进程通过什么文件描述符发送什么命令
void sendAndWakeUp(pid_t who,int fd,uint32_t command)
{write(fd,command,sizeof(command));coutcall processwhoexecutedesc[command]throughfdendl;
}int main()
{Load();vectorpairpid_t,int slots;//先创建多个进程for(int i0;iPROCESS_NUM;i){int pipefd[2]{0};int npipe(pipefd);assert(n0);(void)n;pid_t idfork();assert(id!-1);if(id0){//child读取关闭写端close(pipefd[1]);while(true){bool quitfalse;//pipefd[0]int commandwaitCommand(pipefd[0],quit);//如果不发则阻塞if(quit) break;if(command0commandhandlerSize()){callbacks[command]();}else{cout非法commandendl;}}exit(1);}close(pipefd[0]);slots.push_back(pairpid_t,int(id,pipefd[1]));}//开始任务srand((unsigned long)time(nullptr));while(true){int select,command;cout##############endl;cout1.show functionsendl;cout2.send commandendl;coutPlease select;cinselect;if(1select){showHandler();}else if(select2){coutEnter Your Command:;cincommand;//选择进程int choice_procIDrand()%slots.size();//布置任务给指定进程sendAndWakeUp(slots[choice_procID].first,slots[choice_procID].second,command);}}//关闭fd所有的子进程都会退出for(const auto slot:slots){close(slot.second);}//回收所有的子进程信息for(const auto slot: slots){waitpid(slot.first,nullptr,0);}return 0;
}task.hpp
#pragma once#includeiostream
#includestring
#includeunistd.h
#includefunctional
#includevector
#includeunordered_maptypedef std::functionvoid() func;std::vectorfunc callbacks;
std::unordered_mapint,std::string desc;void readMySQL()
{std::coutprocess[getpid()]执行访问数据库的任务std::endl;
}void execuleURL()
{std::coutprocess[getpid()]执行url解析std::endl;
}void cal()
{std::coutprocess[getpid()]执行加密任务std::endl;
}void save()
{std::coutprocessgetpid()执行数据持久化任务std::endl;
}void Load()
{desc.insert({callbacks.size(),readMySQL});callbacks.push_back(readMySQL);desc.insert({callbacks.size(),execul});callbacks.push_back(execuleURL);desc.insert({callbacks.size(),cal});callbacks.push_back(cal);desc.insert({callbacks.size(),save});callbacks.push_back(save);}void showHandler()
{for(const auto iter:desc){std::coutiter.first\titer.secondstd::endl;}
}int handlerSize()
{return callbacks.size();
}命名管道
引入
管道应用的一个限制就是只能在具有共同祖先具有亲缘关系的进程间通信。 如果我们想在不相关的进程之间交换数据可以使用FIFO文件来做这项工作它经常被称为命名管道。命名管道是一种特殊类型的文件。
区别打开方式是否存在于文件系统血缘
匿名管道不属于文件系统是一种特殊的文件只存在于内存中只能进行血缘关系间的通信。命名管道可用于无关联的进程间通信mkfifo函数调用后需用open打开因为它以FIFO文件的形式存在于文件系统中。
实现一个简易的客户端和服务端
//comm.hpp
#ifndef _COMM_H
#define _COMM_H#includeiostream
#includestring
#includesys/types.h
#includesys/stat.h
#includefcntl.h
#includeunistd.h
#includecstdio
#includecstring
#includelog.hppusing namespace std;string ipcPath./fifo.ipc;
const int MODE0666;
const int SIZE128;#endif//server.cc
#includecomm.hppint main()
{//1.创建命名管道if(mkfifo(ipcPath.c_str(),MODE)0){perror(mkfifo);exit(1);}Log(创建管道文件成功,Debug)step1endl;//2.打开命名管道int fdopen(ipcPath.c_str(),O_RDONLY);if(fd0){perror(open);exit(2);}Log(打开成功,Debug)step2endl;//3.读取数据char buf[SIZE];while(true){memset(buf,\0,sizeof(buf));ssize_t sread(fd,buf,sizeof(buf)-1);//\0不读if(s0){coutclient say:bufendl;}else if(s0){//EOFcerrclient quit!endl;break;}else{//errorperror(read);break;}}//4.关闭文件close(fd);unlink(ipcPath.c_str());Log(关闭成功,Debug)step 3endl;return 0;
}//client.cc
#includecomm.hpp
int main()
{//获取管道文件int fdopen(ipcPath.c_str(),O_WRONLY);if(fd0){perror(open);exit(1);}//ipc通信过程string buffer;while(true){coutplease input:endl;getline(cin,buffer);write(fd,buffer.c_str(),buffer.size());}close(fd);return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88070.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!