• [ Linux ] 进程间通信介绍 管道


    目录

    0.进程间通信介绍

    0.1通信背景

    0.2进程间通信目的

    1.管道

    1.1 管道是什么

    1.2 匿名管道

    1.2.1管道通信的特点

    1.2.2 匿名管道编码

    父进程控制子进程的行为

    进程池 -- 池化概念

    1.3管道的特征总结

    1.4命名管道

    1.4.1创建一个命名管道

    1.4.2 命名管道编码


    0.进程间通信介绍

    0.1通信背景

    在之前我们学习进程时知道进程具有独立性,所以进程间交互数据的成本就变得非常高。进程之间为什么也进行进程间通信,这就需要谈谈进程间通信的目的了。但是进程具有独立性不是彻底独立,只管自己不理任何人,当然不是这样的。进程之间也是存在通信的。那么进程间通信的方式也有很多种,包括管道,System V IPC,POSIX IPC........那么今天我们先来看看进程间如何通过管道来进行通信。

    0.2进程间通信目的

    • 数据传输:一个进程需要将它的数据发送给另一个进程。
    • 资源共享:多个进程之间共享同样的资源。
    • 通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种时间(如进程终止时要通知父进程)。
    • 进程控制:有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变。

    1.管道

    1.1 管道是什么

    管道是Unix中最古老的进程间通信的形式。我们把从一个进程连接到另一个进程的一个数据流称为一个“管道”。在Linux中管道分两种:匿名管道和命名管道。

    1.2 匿名管道

    假设现在在内存中有两个独立的进程,如果要想这两个进程之间通信,那么进程1可以先把数据拷贝到磁盘上,然后进程2再去读这个数据【如下图所示】....首先不考虑这种方法是否可行,但是比较好的让我们理解了一个道理:进程在通信之前,必须让不同的进程能够看到同一份资源(文件,内存块.....)

    因此在通信之前,如何解决让进程先看到同一份资源呢?并且资源的不同决定了不同种类的通信方式!!!

    因此我们正在学习的管道是提供共享资源的一种手段。

    我们知道文件在内存和磁盘之间来回切换是非常耗费时间的,因此进程间通信大多都是内存级别的。意思就是在内存内部重建一块区域进行通信。

    那么什么是管道呢?

    在计算机通信中,我们把文件不再是一个磁盘文件,通过特定的接口表征自己的身份,说明他和磁盘脱离,自己读写数据是就在文件的内存缓冲区,完成数据交互,我们把这个文件叫做管道。因此我们说Linux下一切皆文件,管道也是文件。管道就是一个内存级文件。内容不需要刷新到磁盘中。

    1.2.1管道通信的特点

    在我们生活中遇到的管道有什么特点呢?那首先问那些都属于管道呢?天然气管道,水龙头管道等等.....那么这些管道大多数情况下都是单向的,并且这些管道都是传输资源的,在计算机中最重要的资源就是数据。

    1. 单向的
    2. 传输数据

    那么我们如何来保证单向性呢?

    我们来看下图,父进程和子进程通过管道完成进程通信如何保证单向性呢,我们刚刚提到了管道是一个文件,是数据的缓冲区,因此当父进程把需要通信的数据通过写的方式写入管道内时,子进程通过读的方式拿到这些资源即可完成父子间的通信。为了保证单向的,我们需要关闭父进程的读端,让父进程只能写,关闭子进程的写段,让子进程只能读。通过这样的方式我们就可以保证父进程只能写数据子进程只能读数据的单向性。

    父进程必须以读写方式打开,这是因为子进程会继承下去,这样子进程就不用再打开了。那么谁决定父子关闭什么读写?这不是由管道决定的,这是由我们的需求所决定的。

    那么我们如何来打开管道呢?难道要调用两次open()吗?当然不是了,因此操作系统提供了pipe()接口

    1.2.2 匿名管道编码

    认识pipe()接口,当我们调用piep时,底层会自动帮助我们把文件以读方式和写方式打开,而且我们会的到两个文件描述符,这两个文件描述符会写进pipefd数组内,因此这个数组是一个输出型参数。并且pipe是一个系统调用。返回0表示成功,返回-1表示失败。

    接下来我们进行管道的代码:

    下面这段代码是管道的创建和验证输出的数组是否使我们所想的两个文件描述符

    1. #include <iostream>
    2. #include <cstdio>
    3. #include <unistd.h>
    4. using namespace std;
    5. int main()
    6. {
    7. int pipefd[2] = {0};
    8. if(pipe(pipefd) != 0)
    9. {
    10. cerr<<"pipe error"<<endl;
    11. return 1;
    12. }
    13. cout<<"fd[0]:"<<pipefd[0]<<endl;
    14. cout<<"fd[1]:"<<pipefd[1]<<endl;
    15. return 0;
    16. }

    匿名管道代码演示

    1. #include <iostream>
    2. #include <cstdio>
    3. #include <cstring>
    4. #include <sys/wait.h>
    5. #include <sys/types.h>
    6. #include <unistd.h>
    7. using namespace std;
    8. //演示pipe管道通信的基本过程 -- 匿名管道
    9. int main()
    10. {
    11. //1.创建管道
    12. int pipefd[2] = {0};
    13. if(pipe(pipefd) != 0)
    14. {
    15. cerr<<"pipe error"<<endl;
    16. return 1;
    17. }
    18. //2.创建子进程
    19. pid_t id = fork();
    20. if(id<0)
    21. {
    22. cerr<<"fork error"<<endl;
    23. return 2;
    24. }
    25. else if(id == 0)
    26. {
    27. //child 来进行读取
    28. close(pipefd[1]);
    29. char buffer[1024];
    30. while(true)
    31. {
    32. memset(buffer,0,sizeof(buffer));
    33. ssize_t s = read(pipefd[0],buffer,sizeof(buffer) - 1 );
    34. if(s>0)
    35. {
    36. //读取成功
    37. buffer[s] = '\0';
    38. cout<<"子进程收到消息,消息的内容是:"<< buffer <<endl;
    39. }
    40. else if(s == 0)
    41. {
    42. cout<<"父进程写完了,我也退出了"<<endl;
    43. break;
    44. }
    45. else
    46. {
    47. //do nothing
    48. }
    49. }
    50. close(pipefd[0]);
    51. exit(0);
    52. }
    53. else
    54. {
    55. //parent 来进行写入
    56. close(pipefd[0]);
    57. string msg = "你好子进程,我是父进程!";
    58. int cnt = 0;
    59. while(cnt<5)
    60. {
    61. write(pipefd[1],msg.c_str(),msg.size());
    62. sleep(1);
    63. cnt++;
    64. }
    65. close(pipefd[1]);
    66. cout<<"父进程的工作结束了 退出"<<endl;
    67. }
    68. pid_t res = waitpid(id,nullptr,0);
    69. if(res > 0 )
    70. {
    71. cout<<"父进程等待成功"<<endl;
    72. }
    73. //pipefd[0] 是读
    74. //pipefd[1] 是写
    75. // cout<<"fd[0]:"<<pipefd[0]<<endl;
    76. // cout<<"fd[1]:"<<pipefd[1]<<endl;
    77. return 0;
    78. }

    至此父进程的数据就发给了子进程,子进程也能够接受父进程发送来的数据。在上述代码中,我们在父进程中带了sleep(1),让父进程每间1秒向管道内写入一个数据,那么子进程没有带sleep(1),为什么子进程也会随之休眠一秒呢?为了更好的看到这个现象,我们在父子进程里面带上时间戳,写写日志。

    我们通过测试观察到子进程代码没有任何的休眠,子进程会随着父进程的节奏读取,那么我们可以得出结论:当父进程没有写入数据的时候,子进程在等!所以,父进程写入之后,子进程才能read(会返回)到数据,子进程打印读取数据要以父进程的节奏为主!

    那么,父进程和子进程读写的时候是有一定的顺序性的!当父进程向管道写入的时候,子进程才可以读!

    • 管道内部,没有数据,reader就必须阻塞等待(read时等待)
    • 管道内部,如果数据写满,writer就必须阻塞等到(writer时等待)

    因此pipe内部是自带访问控制机制的以及存在同步和互斥机制的。

    所谓的阻塞等待的本质是将当前的tast_struct 放入等待队列中,将PCB的状态由R->S/D/T

    进程控制子进程的行为

    假设父进程想让我的子进程做父进程想让子进程做的行为,以及父进程想控制一批(多个)子进程.....该如何来写呢???

    我们先写一段父进程控制子进程的行为的代码

    1. #include <iostream>
    2. #include <vector>
    3. #include <unordered_map>
    4. #include <cstdio>
    5. #include <cstring>
    6. #include <cstdlib>
    7. #include <ctime>
    8. #include <sys/wait.h>
    9. #include <sys/types.h>
    10. #include <unistd.h>
    11. #include <cassert>
    12. using namespace std;
    13. // 定义一个函数指针
    14. typedef void (*functor)();
    15. vector<functor> functors; //方法的集合
    16. // for debug
    17. unordered_map<uint32_t, string> info;
    18. void f1() { cout << "这是一个处理日志的任务,执行的进程id: [" << getpid() << "]"
    19. << "执行的时间是:[" << time(nullptr) << "]" << endl; }
    20. void f2() { cout << "这是一个处理数据备份的任务,执行的进程id: [" << getpid() << "]"
    21. << "执行的时间是:[" << time(nullptr) << "]" << endl; }
    22. void f3() { cout << "这是一个处理网络连接的任务,执行的进程id: [" << getpid() << "]"
    23. << "执行的时间是:[" << time(nullptr) << "]" << endl; }
    24. void loadFunctor()
    25. {
    26. info.insert({functors.size(), "处理日志任务"});
    27. functors.push_back(f1);
    28. info.insert({functors.size(), "数据备份任务"});
    29. functors.push_back(f2);
    30. info.insert({functors.size(), "处理网络连接任务"});
    31. functors.push_back(f3);
    32. }
    33. int main()
    34. {
    35. // 0. 加在任务列表
    36. loadFunctor();
    37. // 1.创建管道
    38. int pipefd[2] = {0};
    39. if (pipe(pipefd) != 0)
    40. {
    41. cerr << "pipe error" << endl;
    42. return 1;
    43. }
    44. // 2.创建子进程
    45. pid_t id = fork();
    46. if (id < 0)
    47. {
    48. cerr << " fork error" << endl;
    49. return 2;
    50. }
    51. else if (id == 0)
    52. {
    53. // child read
    54. // 关闭不需要的fd
    55. close(pipefd[1]);
    56. while (true)
    57. {
    58. uint32_t operatorType = 0;
    59. //如果有数据就读取 如果没有数据就阻塞等待
    60. ssize_t s = read(pipefd[0], &operatorType, sizeof(uint32_t));
    61. if(s == 0)
    62. {
    63. cout << "我是子进程,我要退出了" <<endl;
    64. break;
    65. }
    66. assert(s == sizeof(uint32_t));
    67. // assert断言 是编译有效的 debug模式
    68. //但是如果是release模式下 断言就没有了
    69. //一旦断言没有了,s变量就是只被定义没有被使用,一次你release模式下 可能有warning
    70. (void)s;
    71. if (operatorType < functors.size())
    72. {
    73. functors[operatorType]();
    74. }
    75. else
    76. {
    77. cerr << "bug? operatorTypr: " << operatorType << cout;
    78. }
    79. }
    80. close(pipefd[0]);
    81. exit(0);
    82. }
    83. else
    84. {
    85. srand((long long)time(nullptr));
    86. close(pipefd[0]);
    87. int num = functors.size();
    88. int cnt = 10;
    89. while (cnt--)
    90. {
    91. //形成任务码
    92. uint32_t commandCode = rand() % num;
    93. cout<< "父进程指派任务完成,任务是: " <<info[commandCode] <<
    94. "任务的编号是:" << cnt << endl;
    95. //想指定进程下答操作的任务
    96. write(pipefd[1], &commandCode, sizeof(uint32_t));
    97. sleep(1);
    98. }
    99. close(pipefd[1]);
    100. pid_t res = waitpid(id, nullptr, 0);
    101. if (res)
    102. cout << " wait sucess " << endl;
    103. }
    104. return 0;
    105. }

    通过这份代码父进程控制了子进程的行为,往后我们只需要修改functors里面的方法,就可以让子进程执行指定的任务。

    进程池 -- 池化概念

    那么如果是一个父进程想要控制一批子进程呢??? 父进程怎么样把一批任务交给子进程呢?


    因此,我们有多少个进程,我们就创建多少个管道,父进程可以通过对指定管道写入特定的任务,让指定的子进程做对应的事情,这样我们就引入了一个池化的概念!那么我怎么如何书写对应的代码呢?

    1. int processNum = 5;
    2. int main()
    3. {
    4. for(int i = 0;i<processNum;++i)
    5. {
    6. //定义保存管道fd的对象
    7. int pipefd[2] = {0};
    8. //创建管道
    9. pipe(pipefd);
    10. pid_t id = fork();
    11. if(id == 0 )
    12. {
    13. //子进程执行
    14. exit(0);
    15. }
    16. //父进程做得事情
    17. }
    18. return 0;
    19. }

    上面这份代码就成功的将每一次创建的子进程和父进程都独立的进行了控制,因此父进程在不断的循环,给子进程指派任务的时候需要知道给哪一个进程指派,指派什么任务,通过什么指派呢? 因此我们接下来需要做的就是解决这些问题。因此我们需要一节pair结构

    1. #include <iostream>
    2. #include <vector>
    3. #include <unordered_map>
    4. #include <cstdio>
    5. #include <cstring>
    6. #include <cstdlib>
    7. #include <ctime>
    8. #include <sys/wait.h>
    9. #include <sys/types.h>
    10. #include <unistd.h>
    11. #include <cassert>
    12. using namespace std;
    13. // 定义一个函数指针
    14. typedef void (*functor)();
    15. vector<functor> functors; //方法的集合
    16. // for debug
    17. unordered_map<uint32_t, string> info;
    18. void f1() { cout << "这是一个处理日志的任务,执行的进程id: [" << getpid() << "]"
    19. << "执行的时间是:[" << time(nullptr) << "]\n\n" << endl; }
    20. void f2() { cout << "这是一个处理数据备份的任务,执行的进程id: [" << getpid() << "]"
    21. << "执行的时间是:[" << time(nullptr) << "]\n\n" << endl; }
    22. void f3() { cout << "这是一个处理网络连接的任务,执行的进程id: [" << getpid() << "]"
    23. << "执行的时间是:[" << time(nullptr) << "]\n\n" << endl; }
    24. void loadFunctor()
    25. {
    26. info.insert({functors.size(), "处理日志任务"});
    27. functors.push_back(f1);
    28. info.insert({functors.size(), "数据备份任务"});
    29. functors.push_back(f2);
    30. info.insert({functors.size(), "处理网络连接任务"});
    31. functors.push_back(f3);
    32. }
    33. //第一个int32_t:进程pid
    34. //第二个int32_t:该进程对应的管道写端fd
    35. typedef pair<int32_t, int32_t> elem;
    36. int processNum = 5;
    37. void work(int blockFd)
    38. {
    39. cout << "进程 [" << getpid() << "] 开始工作" << endl;
    40. //进行
    41. while (true)
    42. {
    43. //阻塞等待 获取任务信息
    44. uint32_t operatorCode = 0;
    45. ssize_t s = read(blockFd, &operatorCode, sizeof(uint32_t));
    46. if (s == 0)
    47. break;
    48. assert(s == sizeof(uint32_t));
    49. (void)s;
    50. //处理任务
    51. if (operatorCode < functors.size())
    52. {
    53. functors[operatorCode]();
    54. }
    55. }
    56. cout << "进程 [" << getpid() << "] 结束工作" << endl;
    57. }
    58. // [子进程的pid,子进程的管道fd]
    59. void sendTask(const vector<elem> & processFds)
    60. {
    61. srand((long long)time(nullptr));
    62. while (true)
    63. {
    64. sleep(1);
    65. //选择一个进程 选择进程是随机的 没有压着一个进程给任务 -- 随机的
    66. //较为均匀的将任务给所有的子进程 --- 负载均衡
    67. uint32_t pick = rand() % processFds.size();
    68. // 选择任务
    69. uint32_t task = rand() % functors.size();
    70. //把任务给一个指定的进程
    71. write(processFds[pick].second, &task, sizeof(task));
    72. //打印对应的提示信息
    73. cout << "父进程指派任务 --> " << info[task] << "给进程:" << processFds[pick].first
    74. << "编号:" << pick << endl;
    75. }
    76. }
    77. int main()
    78. {
    79. loadFunctor();
    80. vector<elem> assignMap;
    81. for (int i = 0; i < processNum; ++i)
    82. {
    83. //定义保存管道fd的对象
    84. int pipefd[2] = {0};
    85. //创建管道
    86. pipe(pipefd);
    87. pid_t id = fork();
    88. if (id == 0)
    89. {
    90. //子进程读取
    91. close(pipefd[1]);
    92. //子进程执行
    93. work(pipefd[0]);
    94. close(pipefd[0]);
    95. exit(0);
    96. }
    97. //父进程做得事情
    98. close(pipefd[0]);
    99. elem e(id, pipefd[1]);
    100. assignMap.push_back(e);
    101. }
    102. cout << "create all process success !" << endl;
    103. //父进程,派发任务
    104. sendTask(assignMap);
    105. // 回收资源
    106. for (int i = 0; i < processNum; ++i)
    107. {
    108. if (waitpid(assignMap[i].first, nullptr, 0) > 0)
    109. cout << "wait for : pid = " << assignMap[i].first << " wait success"
    110. << "number " << i << endl;
    111. close(assignMap[i].second);
    112. }
    113. return 0;
    114. }

    我们发现此时父进程5个子进程随机的派发不同的任务,这就是一种进程池。至此匿名管道全部写完。

    其中,我们在shell命令行中写的 | 就是匿名管道

    1.3管道的特征总结

    1. 管道只能用来进行具有血缘关系的进程之间,进行进程间通信,常用语父子通信。
    2. 管道只能单向通信(由内核设计实现)半双工的一种有特殊情况
    3. 管道自带同步机制(pipe满,write等;pipe空,read满) -- 自带访问控制
    4. 管道是面向字节流的 -- 先写的字符 一定是先被读取的 没有格式边界 需要用户来自定义区分内容的边界 [sizeof(uint32_t)]
    5. 管道的生命周期随进程 -- 管道是文件 -- 进程退出了 曾经打开的文件也会退出

    1.4命名管道

    我们刚刚提到的都是父子间(血缘)通信,如果我们想要两个毫不相干的两个进程之间通信,应该怎么办。因此我们接下来要讲的就是命名管道。命名管道和之前的匿名管道最大的区别就是可以让任意两个进程之间通信。

    1.4.1创建一个命名管道

    • 创建一个命名管道可以在命令上创建 使用如下这个命令

    mkfifo filename

    这个myfifo就是一个管道文件,前面以p开头。以p开头就是管道,假如我们现在要在左侧想管道内部写入一些东西,在右侧实时查看,当我们左侧回车按下时候,右侧立马出现了“aaaaaa”

    但是这样还是不能很好的观察现象,我们在右侧写一个实时的查看脚本,让一直想管道文件内部写入 bbbbb

    while :; do echo "bbbbb" ; sleep 1; done >> myfifo

    • 命名管道也可以在程序中创建,相关函数

    int mkfifo(const char* filename,mode_t mode);

    我们发现命名管道是带路径的,这有什么作用呢? 其实命名管道是通过一个fifo文件,由于这个文件存在路径,我们都知道路径具有唯一性,因此通过路径我们进程都可以看到这一份资源。

    1.4.2 命名管道编码

    由于我们知道命名管道可以实现两个不想关的进程完成通信,因此我们接下来将写两个文件(进程),让这两个文件进行通信。我们想让clientFifo.cpp这个进程和severFifo.cpp这个进程通过命名管道通信,该怎么写呢?

    1. //comm.h
    2. #pragma once
    3. #include <iostream>
    4. #include <cstdio>
    5. #include <string>
    6. #include <cstring>
    7. #include <cerrno>
    8. #include <sys/types.h>
    9. #include <sys/stat.h>
    10. #include <unistd.h>
    11. #include <fcntl.h>
    12. #define IPC_PATH "./.fifo"
    13. // makefile
    14. .PHONY:all
    15. all: clientFifo severFifo
    16. clientFifo:clientFifo.cpp
    17. g++ -Wall -o $@ $^ -std=c++11
    18. severFifo:severFifo.cpp
    19. g++ -Wall -o $@ $^ -std=c++11
    20. .PHONY:clean
    21. clean:
    22. rm -f clientFifo severFifo .fifo
    23. //clientFifo.cpp
    24. //写入
    25. #include "comm.h"
    26. using namespace std;
    27. int main()
    28. {
    29. int pipeFd = open(IPC_PATH,O_WRONLY);
    30. if(pipeFd < 0)
    31. {
    32. cerr<<"Open : " << strerror(errno) <<endl;
    33. return 1;
    34. }
    35. #define NUM 1024
    36. char line[NUM];
    37. //通信
    38. while(true)
    39. {
    40. printf("请输入你的消息# ");
    41. fflush(stdout);
    42. memset(line,0,sizeof(line));
    43. //从键盘按行为单位读取
    44. if(fgets(line,sizeof(line),stdin) != nullptr )
    45. {
    46. line[strlen(line) - 1] = '\0';
    47. write(pipeFd,line,strlen(line));
    48. }
    49. else
    50. {
    51. break;
    52. }
    53. }
    54. close(pipeFd);
    55. cout<< "客户端退出啦"<<endl;
    56. return 0;
    57. }
    58. //severFifo.cpp
    59. //让severFifo 来读取
    60. #include "comm.h"
    61. using namespace std;
    62. int main()
    63. {
    64. umask(0);
    65. if(mkfifo(IPC_PATH,0600) != 0)
    66. {
    67. cerr<<"mkfifo client" <<endl;
    68. return 1;
    69. }
    70. int pipeFd = open(IPC_PATH,O_RDONLY);
    71. if(pipeFd < 0)
    72. {
    73. cerr << "open fifo error" << endl;
    74. return 2;
    75. }
    76. //正常通信
    77. char buffer[1024];
    78. while(true)
    79. {
    80. ssize_t s = read(pipeFd,buffer,sizeof(buffer) - 1);
    81. if(s > 0 )
    82. {
    83. buffer[s] = '\0';
    84. cout<<" 客户端-->服务器# " << buffer << endl;
    85. }
    86. else if(s == 0)
    87. {
    88. cout<< "客户端退出了,我也退出了";
    89. break;
    90. }
    91. else
    92. {
    93. //do nothing
    94. cout << "read error" << strerror(errno) <<endl;
    95. break;
    96. }
    97. }
    98. close(pipeFd);
    99. cout<< "服务端退出啦"<<endl;
    100. unlink(IPC_PATH);
    101. return 0;
    102. }

    通过这个代码我们成功的在两个毫无相关的进程之间完成了管道通信。

    (本篇完)

  • 相关阅读:
    CloudCompare 二次开发(10)——点云投影到平面
    虚拟机上为AzureDevOps Server 创建用户
    栈、队列、矩阵的总结
    servlet的简单使用总结
    Swift学习笔记四(function 篇)
    23种设计模式中之中介者模式(Mediator Pattern)
    pyqt5使用经验总结
    ThreadLocal的原理
    【无标题】
    项目实战:中央控制器实现(2)-优化Controller,将共性动作抽取到中央控制器
  • 原文地址:https://blog.csdn.net/qq_58325487/article/details/127991318