进程间通信概述
进程间通信的目的是为了实现以下几个主要功能:
数据传输:一个进程需要将其数据发送给另一个进程。资源共享:多个进程之间共享相同的资源。通知事件:一个进程需要向另一个或一组进程发送消息,通知它们发生了某种事件(例如,进程终止时需要通知父进程)。进程控制:某些进程希望完全控制另一个进程的执行(例如,Debug进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能及时了解其状态变化。
进程间通信的发展包括:
管道System V进程间通信POSIX进程间通信
进程间通信的分类包括:
管道:匿名管道(pipe)命名管道System V IPC:消息队列共享内存信号量POSIX IPC:消息队列共享内存信号量互斥量条件变量读写锁
管道
管道是一种最基本的进程间通信机制,主要用于具有亲缘关系的进程之间的通信,例如父子进程。
匿名管道
匿名管道用于父子进程之间的通信。以下是匿名管道的使用示例:




以下是使用管道进行通信的示例代码:


完整代码如下:
#include #include #include // errno.h#include // string.h#include #include #include const int size = 1024;std::string getOtherMessage(){ static int cnt = 0; std::string messageid = std::to_string(cnt); cnt++; pid_t self_id = getpid(); std::string stringpid = std::to_string(self_id); std::string message = "messageid: "; message += messageid; message += " my pid is : "; message += stringpid; return message;}// 子进程进行写入void SubProcessWrite(int wfd){ int pipesize = 0; std::string message = "father, I am your son prcess!"; char c = 'A'; while (true) { std::cerr 0) { inbuffer[n] = 0; // == ' ' std::cout 0) exit(0); SubProcessWrite(pipefd[1]); close(pipefd[1]); exit(0); } std::cout 0) { std::cout >8)&0xFF)
管道的读取和写入行为如下:
WiseHome家政预约小程序
家政服务平台系统包含家用电器安装清洗、搬家、家电维修、管道疏通、月嫂保姆、育儿陪护、上门开锁等多种服务项目,用户可以直接通过家政小程序咨询,在线预约服务类型,同时还设置有知识科普,给用户科普一些清洁保养小技巧,让用户能够足不出户就可以直接预约服务,方便又快捷。本项目使用微信小程序平台进行开发。使用腾讯专门的小程序云开发技术,云资源包含云函数,数据库,带宽,存储空间,定时器等,资源配额价格低廉,无需
0 查看详情
如果管道内部为空且写端未关闭,读进程将被阻塞,直到写端写入数据。如果管道已满且读端未读取且未关闭,写进程将被阻塞,直到数据被读取。如果管道一直被读且写端关闭,读端的read返回值将为0,表示读到文件结尾。如果读端关闭,写端一直在写入,写端进程将被操作系统用13号信号终止,相当于进程出现异常。
管道的特征
匿名管道:仅用于具有亲缘关系的进程之间的通信,常用于父子进程之间。生命周期:随进程而存在。同步机制:管道内部自带进程间同步机制,具有明显的顺序性。字节流:管道文件在通信时是面向字节流的,写的次数和读取的次数不一定一一匹配。通信模式:管道的通信模式是一种特殊的半双工模式,数据只能向一个方向流动;需要双向通信时,需要建立两个管道。

当要写入的数据量不大于PIPE_BUF时,Linux将保证写入的原子性;当要写入的数据量大于PIPE_BUF时,Linux不再保证写入的原子性。原子操作意味着写入操作不会被中断,读方要么读不到数据,要么读到完整的数据。


进程池实现
进程池的实现可以利用管道进行进程间通信。以下是进程池实现的示例代码:

ProcessPool.cc:
#include #include #include #include #include #include #include "Task.hpp"// void work(int rfd)// {// while (true)// {// int command = 0;// int n = read(rfd, &command, sizeof(command));// if (n == sizeof(int))// {// std::cout 0)// {// std::cout *channels, task_t task){ for (int i = 0; i empty()) { // 第二次之后,开始创建的管道要关闭继承下来的写端 for(auto &channel : *channels) channel.CloseChannel(); } // child - read close(pipefd[1]); dup2(pipefd[0], 0); // 将管道的读端,重定向到标准输入 task(); close(pipefd[0]); exit(0); } // 3.构建一个channel名称 std::string channel_name = "Channel-" + std::to_string(i); // 父进程 close(pipefd[0]); // a. 子进程的pid b. 父进程关心的管道的w端 channels->push_back(Channel(pipefd[1], id, channel_name)); } }}// 0 1 2 3 4 channelnumint NextChannel(int channelnum){ static int next = 0; int channel = next; next++; next %= channelnum; return channel;}void SendTaskCommand(Channel &channel, int taskcommand){ write(channel.GetWfd(), &taskcommand, sizeof(taskcommand));}void ctrlProcessOnce(std::vector &channels){ sleep(1); // a. 选择一个任务 int taskcommand = SelectTask(); // b. 选择一个信道和进程 int channel_index = NextChannel(channels.size()); // c. 发送任务 SendTaskCommand(channels[channel_index], taskcommand); std::cout &channels, int times = -1){ if (times > 0) { while (times--) { ctrlProcessOnce(channels); } } else { while (true) { ctrlProcessOnce(channels); } }}void CleanUpChannel(std::vector &channels){ // int num = channels.size() -1; // while(num >= 0) // { // channels[num].CloseChannel(); // channels[num--].Wait(); // } for (auto &channel : channels) { channel.CloseChannel(); channel.Wait(); } // // 注意 // for (auto &channel : channels) // { // channel.Wait(); // }}// ./processpool 5int main(int argc, char *argv[]){ if (argc != 2) { std::cerr channels; // 1. 创建信道和子进程 CreateChannelAndSub(num, &channels, work1); // 2. 通过channel控制子进程 ctrlProcess(channels, 5); // 3. 回收管道和子进程. a. 关闭所有的写端 b. 回收子进程 CleanUpChannel(channels); // sleep(100); return 0;}



Task.hpp:
#pragma once#include #include #include #include #include #define TaskNum 3typedef void (*task_t)(); // task_t 函数指针类型void Print(){ std::cout 2) return; tasks[number]();}int SelectTask(){ return rand() % TaskNum;}void work(){ while (true) { int command = 0; int n = read(0, &command, sizeof(command)); if (n == sizeof(int)) { std::cout
以上就是【Linux】进程间通信(匿名管道)的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/820413.html
微信扫一扫
支付宝扫一扫