【ONE·Linux || 进程间通信】

总言

  进程间通信:简述进程间通信,介绍一些通信方式,管道通信(匿名、名命)、共享内存等。

文章目录

  • 总言
  • 1、进程间通信简述
  • 2、管道
    • 2.1、简介
    • 2.2、匿名管道
      • 2.2.1、匿名管道的原理
      • 2.2.2、编码理解:用fork来共享管道
        • 2.2.2.1、准备工作一:创建一个管道pipe
        • 2.2.2.2、准备工作二:创建子进程,对父子进程构建单向通信的信道
        • 2.2.2.3、正式工作:父子进程间通信
        • 2.2.2.4、结束工作:收尾处理(附整体代码)
      • 2.2.3、总结说明(匿名管道特点、读写原则)
      • 2.2.4、扩展应用:进程池的模拟实现
        • 2.2.4.1、processpool.c
        • 2.2.4.2、Task.hpp
    • 2.3、名命管道
      • 2.3.1、是什么
      • 2.3.2、相关使用演示
        • 2.3.2.1、comm.hpp
        • 2.3.2.2、log.hpp
        • 2.3.2.3、server.cxx
        • 2.3.2.4、client.cxx
        • 2.3.2.5、server端加入子进程(多个读端演示)
  • 3、system V 共享内存
    • 3.1、基础理论
    • 3.2、实操环节
      • 3.2.1、相关函数、指令汇总(通讯前后的工作)
        • 3.2.1.1、shmget
        • 3.2.1.2、ftok
        • 3.2.1.3、ipcs、ipcrm
        • 3.2.1.4、shmat、shmdt
        • 3.2.1.5、shmclt
      • 3.2.2、使用演示
        • 3.2.2.1、基本说明
        • 3.2.2.2、comm.hpp
        • 3.2.2.3、log.hpp
        • 3.2.2.4、shmserver.cxx
        • 3.2.2.5、shmclient.cxx
    • 3.3、总结补充
  • 4、system V 信号量
    • 4.1、理解层面

  
  
  

1、进程间通信简述

  1)、为什么要进行进程间通信?

  以下为一些举例:
  数据传输:一个进程需要将它的数据发送给另一个进程。
  资源共享:多个进程之间共享同样的资源。
  通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种事件(如进程终止时要通知父进程)。
  进程控制:有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变。
  
  
  
  2)、要达成上述进程间通信,需要面临哪些问题,为什么?
  说明:进程具有独立性,要实现进程间通信,则需要让通信双方进程能够看到同一份"资源",此处强调"共享"这一特性。
  
  
  
  3)、进程间通信发展简述
在这里插入图片描述

  
  
  
  

2、管道

2.1、简介

  1)、什么是管道
  管道是Unix中最古老的进程间通信的形式,我们把从一个进程连接到另一个进程的一个数据流称为一个“管道”。
  
  特点:单向通信、传送的都是资源(数据是计算机里最重要的资源)
  
  说明:由于进程具有独立性,若让进程来提供管道,其它进程是看不到该管道存在的。因此,计算机里管道的实现,由操作系统提供,由进程来使用。
在这里插入图片描述

  
  
  

2.2、匿名管道

2.2.1、匿名管道的原理

  1)、问题引入:当进程打开一个文件后,创建一个子进程会发生什么?

在这里插入图片描述

  回答:对进程相关数据结构(*filefiles_structfile* fd_array等),子进程会进行写时拷贝;对被打开文件相关的内核数据结构(文件信息所生成的文件结构file),父子进程指向相同。由此诞生了共享文件,即管道。
  
  
  
  
  2)、如何实现一个管道?
  实际上,管道的底层原理就是通过文件实现的。相关步骤说明:
  1、分别以读写方式打开同一个文件
  2、fork()创建子进程(此时父子进程虽然彼此独立,但它们共享同一个文件)
  3、双方进程各自关闭自己不需要的文件描述符(假设父进程写入、子进程读取,则父进程关闭读端,子进程关闭写端。)
  
  PS:进程间通信属于内存级别的通信,大多以临时数据为主,不需要写入磁盘,虽然存在,但一般情况下没必要,且影响传送效率。
  
  

  
  
  

2.2.2、编码理解:用fork来共享管道

2.2.2.1、准备工作一:创建一个管道pipe

  1)、相关函数介绍
  这里我们使用pipe函数,其作用是创建一个匿名管道。头文件为<unistd.h>。(man 2 pipe查看具体信息。)

NAMEpipe, pipe2 - create pipeSYNOPSIS#include <unistd.h>int pipe(int pipefd[2]);

  
  说明:该函数用于获取两个输出型参数(这里返回文件描述符数组)。pipefd类似于基础IO中学习的文件描述标识符,这里pipefd[0]表示管道的读端,pipefd[1]表示管道的写端。注意参数的返回类型是int

 pipe()  creates  a  pipe, a unidirectional data channel that can be used for interprocess communica‐tion.  The array pipefd is used to return two file descriptors referring to the ends  of  the  pipe.pipefd[0]  refers to the read end of the pipe.  pipefd[1] refers to the write end of the pipe.  Datawritten to the write end of the pipe is buffered by the kernel until it is read from the read end ofthe pipe.  For further details, see pipe(7).

  
  返回值:int类型,若成功,则返回0,若失败则返回-1

RETURN VALUEOn success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

  
  
  2)、相关使用如下

    //step1:创建一个管道int pipefd[2] = {0};//文件描述符数组,用于接受pipe读端、写端int ret = pipe(pipefd);assert(ret != -1);//用于检测是否成功创建管道(void)ret;

  说明:
  1、pipe(pipefd):这里我们并未给出管道名称,所以说pipe创建的是一个匿名管道。
  2、(void)retassert只在debug模式下有效,为了避免release模式下,ret变量因无效(未使用)而报警,此处设置(void)ret以证其使用性。
  
  
  
  3)、验证:pipefd表示的含义
在这里插入图片描述

  

    //验证:pipefd[2]具体含义cout << "pipefd[0]" << pipefd[0] << endl;cout << "pipefd[1]" << pipefd[1] << endl;
mypipe.out:mypipe.ccg++ -o $@ $^ -std=c++11 -DDEBUG //debug模式

  
  
  
  
  

2.2.2.2、准备工作二:创建子进程,对父子进程构建单向通信的信道

  说明:上述步骤,我们只是创建出管道,未曾实际使用它。此处我们以fork的形式创建父子进程,让其使用管道进行通信。根据之前描述的如何实现一个管道,相关示意图如下:
在这里插入图片描述

  
  相关代码如下:

    //step2:创建子进程,对父子进程构建单向通信的信道pid_t id = fork();assert(id != -1);//fork失败if(id = 1)//子进程:此处设子进程读取{close(pipefd[1]);//对子进程,保留读端,关闭写端//相关读取操作//……exit(0);}//父进程:此处设父进程写入close(pipefd[0]);//对父进程,保留写端,关闭读端//相关写入操作//……

  
  man 2 fork

SYNOPSIS#include <unistd.h>pid_t fork(void);RETURN VALUEOn  success, the PID of the child process is returned in the parent, and 0 is returned in the child.On failure, -1 is returned in the parent, no child process is created, and errno  is  set  appropri‐ately.

  
  

2.2.2.3、正式工作:父子进程间通信

  说明:在上述准备工作中,所创建的匿名管道能够让父子进程单向通信,只用在读端、写端分别编写相关代码操作即可。以下为一个演示案例(该部分内容不固定,仅做演示理解):

  对子进程,读取管道中的数据:使用read函数读取管道文件,并将读取到的数据存储在re_buffer中。

    if(0 == id){close(pipefd[1]);//子进程保留读端//……相关读取操作char re_buffer[1024*8]={0};//子进程缓冲区:用于接收读端读取到的数据while(true){//从管道中读取数据存储入缓冲区ssize_t re = read(pipefd[0], re_buffer, sizeof(re_buffer)-1);if(re > 0){re_buffer[re]= 0;cout << "【childpid-" << getpid() << ", get a message 】:" << re_buffer << endl;}else if(re == 0)//end of file,读取到0,文件结尾。{cout << "child: end of file, quit." << endl;break;//退出循环}}exit(0);//终止子进程}

  
  man 2 read
在这里插入图片描述
  
  
  对父进程,写入数据:这里设置写10次结束。

    //父进程close(pipefd[0]);//父进程保留写端//……相关写入操作char wr_buffer[1024*8]={0};//写端缓冲区,用于存储发送的数据string message = "父进程写入数据";//提示文字1int count=0;//提示文字2,用于记录写入次数while(true){//向wr_buffer中写入相关信息snprintf(wr_buffer,sizeof(wr_buffer),"fatherid-%d, %s, 第%d写入.",getpid(),message.c_str(),1+count++);//将写端缓冲区数据写入管道文件中write(pipefd[1],wr_buffer,strlen(wr_buffer));sleep(1);//便于观察输出结果而设//结束写端if(10 == count){cout<<"father quit!\n"<<endl;break;}}

  
  
  

2.2.2.4、结束工作:收尾处理(附整体代码)

  结束后,需要将通信管道关闭。

    //step3:执行完毕close(pipefd[1]);pid_t ret2 = waitpid(id, nullptr, 0);printf("wait:%d, childpid:%d, count:%d\n",ret2,id,count);assert(ret2 > 0);(void)ret2;

  演示结果:
在这里插入图片描述

  
  
  
  整体代码如下:

#include<iostream>
#include<unistd.h>
#include<assert.h>
#include<string>
#include<cstring>
#include<cstdio>
#include<sys/types.h>
#include<sys/wait.h>using namespace std;//演示以fork父子进程的方式创建匿名管道
int main()
{//step1:创建一个管道int pipefd[2] = {0};//文件描述符数组,用于接受pipe读端、写端int ret = pipe(pipefd);assert(ret != -1);//用于检测是否成功创建管道(void)ret;//assert只在debug模式下有效,为了避免release模式下ret变量未被使用而报警设置。//验证:pipefd[2]具体含义#ifdef DEBUGcout << "pipefd[0]" << pipefd[0] << endl;cout << "pipefd[1]" << pipefd[1] << endl;cout<<endl;#endif//step2:创建子进程,对父子进程构建单向通信的信道pid_t id = fork();assert(id != -1);//fork失败if(0 == id)//子进程:此处设子进程读取{close(pipefd[1]);//对子进程,保留读端,关闭写端//……相关读取操作char re_buffer[1024*8]={0};//子进程缓冲区:用于接收读端读取到的数据while(true){//从管道中读取数据存储入缓冲区ssize_t re = read(pipefd[0], re_buffer, sizeof(re_buffer)-1);if(re > 0){re_buffer[re]= 0;cout << "【childpid-" << getpid() << ", get a message 】:" << re_buffer << endl;}else if(re == 0)//end of file{cout << "child: end of file, quit." << endl;break;//退出循环}}exit(0);//终止子进程}//父进程:此处设父进程写入close(pipefd[0]);//对父进程,保留写端,关闭读端//……相关写入操作char wr_buffer[1024*8]={0};//写端缓冲区,用于存储发送的数据string message = "父进程写入数据";//提示文字1int count=0;//提示文字2while(true){//向wr_buffer中写入相关信息,并将其显示snprintf(wr_buffer,sizeof(wr_buffer),"fatherid-%d, %s, 第%d写入.",getpid(),message.c_str(),1+count++);//将写端缓冲区数据传入管道write(pipefd[1],wr_buffer,strlen(wr_buffer));//结束写端sleep(1);if(10 == count){cout<<"father quit!\n"<<endl;break;}}//step3:执行完毕close(pipefd[1]);pid_t ret2 = waitpid(id, nullptr, 0);printf("wait:%d, childpid:%d, count:%d\n",ret2,id,count);assert(ret2 > 0);(void)ret2;return 0;
}

  
  
  

2.2.3、总结说明(匿名管道特点、读写原则)

  1)、管道特点
  1、匿名管道只能用于具有亲缘关系的进程之间进行通信。通常运用于父子进程(一个管道由一个进程创建,然后该进程调用fork,此后父、子进程之间就可应用该管道。假如不断在fork,那么就能实现父子、兄弟之间共用同一管道)。

  2、管道通过让进程间协同,提供了访问控制。(根据之前fork的学习,创建子进程后,父子进程执行是没有先后顺序的,即缺乏访问控制)。

  3、管道提供流式服务。(即面向字节流,后续需要通过订制协议来进行区分)。

  4、管道是基于文件的,文件的生命周期随进程,故管道的生命周期是随进程的。(一般而言,进程退出,管道释放)。

  5、管道是半双工的,数据只能向一个方向流动。(需要双方通信时,需要建立起两个管道。)
  
  
  2)、管道读写原则
  两种访问方式和两种退出方式:
  1、若写端快,读端慢,管道写满了就不再写入;
  2、若写端慢,读端快,管道内没有数据时,读端必须等待;
  3、若写端关闭,读端最终会读取到0,标志到了文件结尾。
  4、若读端关闭,OS会终止写进行。
  
  
  
  

2.2.4、扩展应用:进程池的模拟实现

  上述匿名管道,可运用于创建进程池。如下图,当有多个子进程时,父子之间建立起管道通信。当父进程发送指令(command code),可派遣任务让随机一个子进程执行。
  在这里插入图片描述
  
  PS:该部分相关代码如下,可结合思维导图理解。相关演示结果如下:
在这里插入图片描述
在这里插入图片描述

  
  

2.2.4.1、processpool.c

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include"task.hpp"#define PROCESS_NUM 5 //子进程数目
using namespace std;//子进程使用:用于等待接受父进程传递的任务编号指令,以便后续执行
int waitCommand(int pipefd, bool& quit)
{//读取:从对应的pipefd中读取到相关的指令,将其返回。uint32_t command = 0;int ret = read(pipefd, &command, sizeof(command));if(ret==0)//读取到文件末尾{quit=true;return -1;}assert(ret==sizeof(uint32_t));//用于检测读取到的指令是否合法return command;
}//父进程使用:用于委派任务给子进程
void sentAndWakeup(pid_t id, int pipefd, uint32_t command)
{//写入:将对应的command传递到子进程对应的通信管道中write(pipefd,&command,sizeof(command));cout<< "【father process-" << getpid() <<"】: call child process-" << id << ", execute task-" << desc[command] << ", child pipefd-" << pipefd <<"\n" <<endl;
}int main()
{//1、准备工作Load();//加载任务vector<pair<pid_t,int>> slots;//用于记录创建出的子进程,以及该进程对应的通信管道//2、创建子进程,建立单向通信管道for(int i = 0; i < PROCESS_NUM; ++i){//建立管道int pipefd[2];int n = pipe(pipefd);assert(n == 0);(void)n;//创建子进程pid_t id = fork();assert(id!=-1);if(0==id)//对子进程{close(pipefd[1]);//3、子进程开始工作while(true){// 等待父进程命令bool quit = false;int command = waitCommand(pipefd[0], quit);if(quit)//在管道中读取到文件尾break;// 执行对应的命令:此处对command做了检测if(command >= 0 && command <handlerSize()){calltasks[command];}else{cout<<"command error: "<< command << endl;}}exit(1);//子进程结束,退出}//2、对父进程close(pipefd[0]);//对管道:关闭读端slots.push_back({id,pipefd[1]});//对进程:记录当前创建的子进程id及其通信管道fd}//3、父进程开始工作:派遣任务srand((unsigned long)time(nullptr)*getpid());//随机数方式的负载均衡,这里为了尽量随机可做一些处理while(true){int command = rand() % handlerSize();// 选择一个任务:获取任务编号int choosechild = rand() % slots.size();// 选择一个子进程:获取到子进程id,通信管道fdsentAndWakeup(slots[choosechild].first, slots[choosechild].second, command); // 将任务派遣给子进程:根据选择的进程choosechild,将command任务编号传入该进程对应的通信管道。sleep(1);}//4、父进程结束工作:收尾处理for(const auto &it : slots)//关闭所有通信管道{close(it.second);}for(const auto &it : slots)//回收所有的子进程信息{waitpid(it.first,nullptr,0);}return 0;
}

  
  
  
  
  

2.2.4.2、Task.hpp
#pragma once
#include<iostream>
#include<vector>
#include<unordered_map>
#include<functional>
#include<unistd.h>using func = std::function<void()>;
std::vector<func> calltasks;//任务表:存储各函数接口std::unordered_map<int,std::string> desc;//任务编号及其文字描述//任务:(各函数实现)
void readMySQL()
{std::cout << "【child process-" << getpid() << " 】 :执行访问数据库的任务\n" << std::endl;
}void execuleUrl()
{std::cout << "【child process-" << getpid() << " 】 :执行url解析\n" << std::endl;
}void cal()
{std::cout << "【child process-" << getpid() << " 】 :执行加密任务\n" << std::endl;
}void save()
{std::cout << "【child process-" << getpid() << " 】 执行数据持久化任务\n" << std::endl;
}//任务汇总
void Load()
{desc.insert({calltasks.size(),"readMySQL:读取数据库"});calltasks.push_back(readMySQL);desc.insert({calltasks.size(),"execuleUrl:进行url解析"});calltasks.push_back(execuleUrl);desc.insert({calltasks.size(),"cal:进行加密"});calltasks.push_back(cal);desc.insert({calltasks.size(),"saveL:进行数据持久化"});calltasks.push_back(save);}void showHandler()//用于展示目前所具有的任务信息及其编号
{for(auto &it: desc){std::cout << it.first << "\t" << it.second << std::endl;}
}int handlerSize()//用于获取函数数组的大小,即任务总数
{return calltasks.size();
}

  
  
  
  
  

2.3、名命管道

2.3.1、是什么

  1)、问题引入:对于非血缘关系的进程,如何让二者看到同一份资源?
  
  回答:在磁盘上建立名命管道文件(FIFO文件),可以被双方进程打开,但不会将内存数据刷新到磁盘上。(PS:命名管道是一种特殊类型的文件,其在磁盘中大小为0)。
  
  
  
  2)、如何创建一份名命管道文件?
  相关函数指令:mkfifo,(man 3 mkfifo 可在Linux中查看)。
  

在这里插入图片描述函数介绍

  头文件和对应函数声明:

NAMEmkfifo - make a FIFO special file (a named pipe)SYNOPSIS#include <sys/types.h>#include <sys/stat.h>int mkfifo(const char *pathname, mode_t mode);

  具体介绍:

DESCRIPTIONmkfifo()  makes  a FIFO special file with name pathname.  mode specifies the FIFO's permissions.It is modified by the process's umask in the usual way: the permissions of the created file  are(mode & ~umask).A FIFO special file is similar to a pipe, except that it is created in a different way.  Insteadof being an anonymous communications channel, a FIFO special file is entered into the file  sys‐tem by calling mkfifo().Once  you  have  created a FIFO special file in this way, any process can open it for reading orwriting, in the same way as an ordinary file.  However, it has to be open at both ends  simulta‐neously  before  you can proceed to do any input or output operations on it.  Opening a FIFO forreading normally blocks until some other process opens the  same  FIFO  for  writing,  and  viceversa.  See fifo(7) for nonblocking handling of FIFO special files.

  返回值:

RETURN VALUEOn success mkfifo() returns 0.  In the case of an error, -1 is returned (in which case, errno isset appropriately).

  
  
  

在这里插入图片描述使用演示

  mkfifo named_pipe:mkfifo可在命令行上使用。

在这里插入图片描述

  

[wj@VM-4-3-centos T0917]$ mkfifo named_pipe
[wj@VM-4-3-centos T0917]$ ll
total 0
prw-rw-r-- 1 wj wj 0 Sep 16 20:48 named_pipe //可以看到其在磁盘上大小为0
[wj@VM-4-3-centos T0917]$ echo "hello" > named_pipe [wj@VM-4-3-centos T0917]$ cat < named_pipe 
hello

  
  以脚本演示:while :; do echo "hello pipe"; sleep 1 ; done > named_pipe
在这里插入图片描述
  
  
  
  
  

2.3.2、相关使用演示

  整体实现框架如下:
在这里插入图片描述
  演示结果:

在这里插入图片描述
  
  

2.3.2.1、comm.hpp
#ifndef _COMM_H_
#define _COMM_H_#include<iostream>
#include<string>
#include<cstring>
#include<assert.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include "log.hpp"using namespace std;#define MODE 0666 //mkfifo中第二参数mode
#define SIZE 1024 //读写端缓冲区大小//命名管道路径、名称:此处创建在当前路径下
string ipcPath="./fifo.ipc";#endif

  
  
  
  

2.3.2.2、log.hpp
#ifndef _LOG_H
#define _LOG_H#include<iostream>
#include<ctime>#define Debug 0
#define Notice 1
#define Warning 2
#define Error 3const std::string msg[]={"Debug","Notice","Warning","Error"
};std::ostream &Log(std::string masseage, int label)
{std::cout << "[" << (unsigned long)time(nullptr) << "] :" << msg[label]<< ", "<< masseage << std::endl;return std::cout;
}#endif

  
  
  
  

2.3.2.3、server.cxx

#include "comm.hpp"static void getMessage(int fd)
{char buffer[SIZE]={0};//读端缓冲区while(true){//void * memset ( void * ptr, int value, size_t num );memset(buffer,'\0',sizeof(buffer));//为了每次接受信息前,清屏处理上此遗留的无效数据int rd = read(fd, buffer, sizeof(buffer) - 1);if (rd > 0) // 读取到数据{cout << "masseage from client:> " << buffer << endl;}else if (rd == 0) // 读取到文件尾end of file{cout << "end of the file, quit!" << endl;break;}else{perror("read"); // errorbreak;}}
}int main()//server:读取端,用于接受client发送的通信
{//1、创建管道文件int ret = mkfifo(ipcPath.c_str(), MODE);assert(0 == ret);(void)ret;Log("server:创建管道文件",Debug) << endl;//2、正常文件操作:接受通信、显示通信//2.1、打开名命管道int fd = open(ipcPath.c_str(), O_RDONLY);if(fd < 0){perror("open");exit(2);//处理assert,上述mkfifo也可以用perror检测}Log("server:打开名命管道",Debug)<< endl;//2.2、进行通信getMessage(fd);//2.3、通信结束,关闭文件close(fd);Log("server:通信结束,关闭文件",Debug)<< endl;//3、删除管道文件unlink(ipcPath.c_str());Log("server:删除管道文件",Debug)<< endl;return 0;
}

  
  
  
  

2.3.2.4、client.cxx

#include "comm.hpp"int main()//client:写入端,用于发送通信到管道中
{//1、获取管道文件int fd = open(ipcPath.c_str(), O_WRONLY);if(fd < 0){perror("open");exit(1);}Log("client: 获取管道文件",Debug)<< endl;//2、ipc通信过程string buffer;while(true){cout<< "client sent massage:> ";std::getline(std::cin, buffer);//获取数据到缓冲区write(fd,buffer.c_str(),buffer.size());//将缓冲区数据写入通信管道中}//3、关闭管道文件close(fd);Log("client: 关闭管道文件",Debug)<< endl;return 0;
}

  
  
  

2.3.2.5、server端加入子进程(多个读端演示)

  相关演示结果:
在这里插入图片描述
  
  只需要在server端做出小改动就行:

static void getMessage(int fd)
{char buffer[SIZE]={0};//读端缓冲区while(true){//void * memset ( void * ptr, int value, size_t num );memset(buffer,'\0',sizeof(buffer));//为了每次接受信息前,清屏处理上此遗留的无效数据int rd = read(fd, buffer, sizeof(buffer) - 1);if (rd > 0) // 读取到数据{cout <<"child: " << getpid() <<" | masseage from client:> " << buffer << endl;}else if (rd == 0) // 读取到文件尾end of file{cout << "end of the file, quit!" << endl;break;}else{perror("read"); // errorbreak;}}
}
    // //2.2、进行通信// getMessage(fd);//2.2、写法二:创建子进程,进行多项读端通信int nums=3;//进程数目for(int i = 0; i < nums; ++i){int id = fork();assert(id >= 0);if(id == 0)//子进程:执行读取任务{getMessage(fd);exit(1);//子进程结束,退出进程}}for(int i = 0; i < nums; ++i)//父进程:等待子进程结束{waitpid(-1,nullptr,0);}

  
  
  
  
  
  
  

3、system V 共享内存

3.1、基础理论

  1)、共享内存的原理简述
在这里插入图片描述

  问题1、共享内存是谁提供的?
  回答:操作系统。共享内存是OS专门为了进程通信而设定的,进程只是使用它,而非拥有者。
  
  
  问题2、操作系统要不要管理共享内存?
  回答:需要。实际上共享内存=共享内存块+对应共享内存的内核数据结构。(PS:对于管道文件,OS也要管理,只不过其包含在文件管理中,不需要单独拎出处理)。
  
  
  

3.2、实操环节

3.2.1、相关函数、指令汇总(通讯前后的工作)

在这里插入图片描述

3.2.1.1、shmget

  1)、基础介绍
  shmget功能:用来创建共享内存。查看方式:man shmget

NAMEshmget - allocates a System V shared memory segmentSYNOPSIS#include <sys/ipc.h>#include <sys/shm.h>int shmget(key_t key, size_t size, int shmflg);

  参数介绍:
  key:key值用于保证通信双方看到的都是同一个共享内存。通信双方(client&server)能够匹配同一个key值,则能看到同一个共享内存。其在OS中具有唯一性,其数值本身不重要。
  使用方法:使用同样的算法规则,使得通信双方获取同一个key值(唯一)。比如:ftok函数(下述介绍)。
  
  size:共享内存大小。
  size = 4096/4097:共享内存的大小最好是页(PAGE,4096,4kb)的整数倍。(PS:4097会创建两个页,但OS实际只给4097个bit)
  
  
  shmflg:由九个权限标志构成,它们的用法和创建文件时使用的mode模式标志是一样的。
在这里插入图片描述

       The value shmflg is composed of:IPC_CREAT   to create a new segment.  If this flag is not used, then shmget() will find the segment  asso‐ciated with key and check to see if the user has permission to access the segment.IPC_EXCL    used with IPC_CREAT to ensure failure if the segment already exists.

  
  返回值: 若创建成功,shmget的返回值为共享内存的用户层标识符,类似fd文件标识符。

RETURN VALUEOn success, a valid shared memory identifier is returned.  On errir, -1 is returned, and errno is  set  toindicate the error.

  相关使用举例见下述ipcs、ipcrm小节。
  
  
  
  

3.2.1.2、ftok

  1)、基础介绍
  功能:用于创建共享内存时,形成相同的key值。查看方式,man ftok

NAMEftok - convert a pathname and a project identifier to a System V IPC keySYNOPSIS#include <sys/types.h>#include <sys/ipc.h>key_t ftok(const char *pathname, int proj_id);

  

DESCRIPTIONThe  ftok()  function  uses  the  identity of the file named by the given pathname (which must refer to anexisting, accessible file) and the least significant 8 bits of proj_id (which must be nonzero) to generatea key_t type System V IPC key, suitable for use with msgget(2), semget(2), or shmget(2).The  resulting value is the same for all pathnames that name the same file, when the same value of proj_idis used.  The value returned should be different when the (simultaneously existing) files or  the  projectIDs differ.

  
  返回值:

RETURN VALUEOn  success,  the generated key_t value is returned.  On failure -1 is returned, with errno indicating theerror as for the stat(2) system call.

  
  
  2)、使用演示

在这里插入图片描述

  
  相关代码:

[wj@VM-4-3-centos shm]$ ls
comm.hpp  log.hpp  makefile  shmclient.cxx  shmserver.cxx
[wj@VM-4-3-centos shm]$ make
g++ -o shmclient.out shmclient.cxx -std=c++11
g++ -o shmserver.out shmserver.cxx -std=c++11[wj@VM-4-3-centos shm]$ ls
comm.hpp  log.hpp  makefile  shmclient.cxx  shmclient.out  shmserver.cxx  shmserver.out[wj@VM-4-3-centos shm]$ ./shmserver.out 
[1694951640] :Debug, server:ftok successserver key: 1140916349[wj@VM-4-3-centos shm]$ ./shmclient.out 
[1694951646] :Debug, client:ftok successclinet key: 1140916349
[wj@VM-4-3-centos shm]$ 

  
  
  
  

3.2.1.3、ipcs、ipcrm

  1)、基础介绍
  ipcs

NAMEipcs - provide information on IPC facilitiesDESCRIPTIONipcs  provides information on the inter-process communication facilities for which the calling process hasread access.

  ipcs -m:可用于查看共享内存。

       -m, --shmemsWrite information about active shared memory segments.

  
  
  ipcrm

NAMEipcrm - remove a message queue, semaphore set or shared memory idSYNOPSISipcrm [options]

  ipcrm -m shmid:删除共享内存(注意此处需要填shmid值)

OPTIONS-M, --shmem-key shmkeyremoves the shared memorysegment created with shmkey after the last detach is performed.-m, --shmem-id shmidremoves the shared memory segment identified by shmid after the last detach is performed.

  
  
  
  2)、使用演示
  int shmget(key_t key, size_t size, int shmflg); shmlg带上权限时:

在这里插入图片描述

  使用ipcs 查看时各项含义简介:
在这里插入图片描述

  若使用shmget,参数shmlg中不加权限,结果如下:
在这里插入图片描述

  
  
  
  

3.2.1.4、shmat、shmdt

  1)、基础介绍
  man shmatman shmdt,可查看相关信息:
  shmat将共享内存段连接到进程地址空间;shmdt:将共享内存段与当前进程脱离。

NAMEshmat, shmdt - System V shared memory operationsSYNOPSIS#include <sys/types.h>#include <sys/shm.h>void *shmat(int shmid, const void *shmaddr, int shmflg);int shmdt(const void *shmaddr);

  相关参数:
   shmid:共享内存标识
   shmaddr:指定连接的虚拟地址(一般设置为nullptr即可,由操作系统自行配置)
   shmflg:它的两个可能取值是SHM_RND和SHM_RDONLY
  
  返回值: 对shmat,成功返回一个指针,指向共享内存第一个节;失败返回-1。对shmdt,成功返回0,失败返回-1。

RETURN VALUEOn success shmat() returns the address of the attached shared memory  segment;  on  error  (void *) -1  isreturned, and errno is set to indicate the cause of the error.On  success  shmdt()  returns  0;  on  error -1 is returned, and errno is set to indicate the cause of theerror.

  相关使用演示见下述shmclt.
  
  
  
  
  

3.2.1.5、shmclt

  1)、基础介绍
  man shmclt,可查看相关信息:

NAMEshmctl - System V shared memory controlSYNOPSIS#include <sys/ipc.h>#include <sys/shm.h>int shmctl(int shmid, int cmd, struct shmid_ds *buf);DESCRIPTIONshmctl() performs the control operation specified by cmd on the System V shared memory segment whose iden‐tifier is given in shmid.The buf argument is a pointer to a shmid_ds structure,

  相关参数:
  cmd:该参数可针对shmid对应的共享内存进行各项选项操作(下述演示中只使用到IPC_RMID:即便是有进程和当下的shm挂接,依旧删除共享内存。)
  buf:为共享内存属性而准备,若不使用可设置为空nullptr。若想自行设置或做其它处理,可使用到该参数。
  
  返回值: 成功返回0;失败返回-1。

		Other operations return 0 on success.On error, -1 is returned, and errno is set appropriately.

  
  2)、使用演示
  用于观察的脚本:while :; do ipcs -m; sleep 1; done

在这里插入图片描述

  
  
  
  

3.2.2、使用演示

3.2.2.1、基本说明

  此部分整体介绍框架如下:
在这里插入图片描述
  
  须知:
  1、共享空间创建在虚拟地址中的共享区,该部分属于用户空间,不用经过系统调用就可以直接访问。即:通讯进程双方可以直接进行内核级别的读写。
  2、若创建共享内存后,写端没有写入任何消息数据,不妨碍读端读取数据(读取为空,共享内存被创建后默认清除所有数据)。
  
  使用时注意事项:
  1、使用共享内存,通信双方只要一方直接向共享内存中写入数据,另一方就可以立马看到对方写入的数据。
  2、共享内存缺乏访问控制!会带来并发问题。(如:写端数据信息尚未完全录入,读端就把数据读取出去。由于数据的不完整性,导致该数据无效或错误。)
  
  
  PS:实际进程间通信部分可以根据自己的需求设置,这里主要演示使用共享内存通讯时如何让其一定程度上得到访问控制。以下为整体代码展示。
在这里插入图片描述

  
  
  

3.2.2.2、comm.hpp
#pragma once#include<iostream>
#include<string>
#include<cstring>
#include<assert.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<fcntl.h>
#include "log.hpp"using namespace std;#define SHM_SIZE 4096 //shmget参数:定义shm的大小,最好是页(PAGE,4096,4kb)的整数倍
#define PROJ_ID 0x44//ftok参数:值可随意,the least significant 8 bits of proj_id (which must be nonzero)
#define PATH_NAME "/home/wj"//路径正确即可。/
//用于让共享内存达到访问控制的一些设置:借助管道实现#define FIFO_NAME "./fifo" //将名命管道创建在当前目录下,名称为fifo#define READ O_RDONLY //以读的方式打开管道文件
#define WRITE O_WRONLY //以写的方式打开管道文件//类:用于让进程运行、关闭时,自动创建、销毁管道文件
class Init
{
public:Init()//默认构造{umask(0);int fifoid = mkfifo(FIFO_NAME,0666);assert(fifoid != -1);(void)fifoid;Log("FIFO pipe created!", Notice);}~Init()//默认析构{unlink(FIFO_NAME);Log("remove FIFO pipe!", Notice);}
};//函数:主要目的是为读写两端以不同方式打开管道文件
int OpenFIFO(std::string pathname, int flags)
{int fd = open(pathname.c_str(),flags);assert(fd > 0);(void)fd;return fd;
}//函数:等待。让读端处于等待中,直到从管道文件中接受到指令。
void Wait(int fd)//此处fd是管道文件
{uint32_t command = 0;//指令:对于输入内容不重要,只是为了控制ssize_t n = read(fd, &command, sizeof(uint32_t));assert(n == sizeof(uint32_t));(void)n;
}//函数:唤醒。当写端数据输入完成后,输入指令唤醒读端。
void Signal(int fd)//此处fd是管道文件
{uint32_t command = 0;ssize_t n = write(fd, &command, sizeof(uint32_t));assert(n == sizeof(uint32_t));(void)n;
}//函数:关闭管道文件.
void closeFIFO(int fd)
{close(fd);
}
/

  
  
  
  

3.2.2.3、log.hpp
#ifndef _LOG_H
#define _LOG_H#include<iostream>
#include<ctime>#define Debug 0
#define Notice 1
#define Warning 2
#define Error 3const std::string msg[]={"Debug","Notice","Warning","Error"
};std::ostream &Log(std::string masseage, int label)
{std::cout << "[" << (unsigned long)time(nullptr) << "] :" << msg[label]<< ", "<< masseage << std::endl;return std::cout;
}#endif

  
  
  
  

3.2.2.4、shmserver.cxx

#include "comm.hpp"int main()
{// 1、创建公共的key值key_t k = ftok(PATH_NAME, PROJ_ID);if (k < 0){Log("client:ftok failure!", Error)<< " clinet key: " << k << endl;exit(1);}Log("client:ftok success", Debug) << " clinet key: " << k << endl;//2、获取共享内存int shmid = shmget(k, SHM_SIZE, IPC_CREAT);//若创建的共享内存在底层存在,则获取并返回if( shmid < 0){Log("client: get shm failure!", Error) << " the shmid:" << shmid << endl;exit(2);}Log("client: get shm success", Debug) << " the shmid:" << shmid << endl;//3、建立映射(关联)char* shmaddr = (char*)shmat(shmid, nullptr, 0);Log("client: attach shm success", Debug) << " the shmaddr: "<< shmaddr << endl;//4、进行通信:写端,输入数据//4.1、以写的方式打开管道文件int fd = OpenFIFO(FIFO_NAME,WRITE);//4.2、通讯:将数据写入共享内存while(true){int n = read(0, shmaddr, SHM_SIZE-1);if( n > 0){shmaddr[n-1] = 0;//键盘读取数据输入共享内存,最后一位为\n//4.3、写端:向名命管道中发送指令(内容随机),告知读端数据输入完毕Signal(fd);Log("写端:数据输入完成:>", Notice);if(strcmp(shmaddr,"quit")==0) break; //用于结束写端}}//4.3、完成通讯,关闭名命管道(见下述)//5、取消映射(去关联)int ret = shmdt(shmaddr);assert( ret != -1);Log("client: detach shm success", Debug) << " the ret: " << ret << endl;//6、server中做了处理,此处不用删除共享内存closeFIFO(fd);//关闭名命管道return 0;
}

  
  
  
  

3.2.2.5、shmclient.cxx

#include "comm.hpp"int main()
{// 1、创建公共的key值key_t k = ftok(PATH_NAME, PROJ_ID);if (k < 0){Log("client:ftok failure!", Error)<< " clinet key: " << k << endl;exit(1);}Log("client:ftok success", Debug) << " clinet key: " << k << endl;//2、获取共享内存int shmid = shmget(k, SHM_SIZE, IPC_CREAT);//若创建的共享内存在底层存在,则获取并返回if( shmid < 0){Log("client: get shm failure!", Error) << " the shmid:" << shmid << endl;exit(2);}Log("client: get shm success", Debug) << " the shmid:" << shmid << endl;//3、建立映射(关联)char* shmaddr = (char*)shmat(shmid, nullptr, 0);Log("client: attach shm success", Debug) << " the shmaddr: "<< shmaddr << endl;//4、进行通信:写端,输入数据//4.1、以写的方式打开管道文件int fd = OpenFIFO(FIFO_NAME,WRITE);//4.2、通讯:将数据写入共享内存while(true){int n = read(0, shmaddr, SHM_SIZE-1);if( n > 0){shmaddr[n-1] = 0;//键盘读取数据输入共享内存,最后一位为\n//4.3、写端:向名命管道中发送指令(内容随机),告知读端数据输入完毕Signal(fd);Log("写端:数据输入完成:>", Notice);if(strcmp(shmaddr,"quit")==0) break; //用于结束写端}}//4.3、完成通讯,关闭名命管道(见下述)//5、取消映射(去关联)int ret = shmdt(shmaddr);assert( ret != -1);Log("client: detach shm success", Debug) << " the ret: " << ret << endl;//6、server中做了处理,此处不用删除共享内存closeFIFO(fd);//关闭名命管道return 0;
}

  
  
  
  

3.3、总结补充

  
  1、我们把多个进程(执行流)看到的同一份公共资源,称之为临界资源。
  2、进程中,用于访问临界资源的代码称之为临界区。
  
  根据上述共享内存学习,当多个执行流不加保护地访问相同的临界资源时,会存在运行时互相干扰的情况,带来时序问题。
  
  3、为了更好的进行临界区的保护,可以让多个执行流在任何时刻,都只有一个进程进入临界区。将这种操作称之为互斥。
  
  4、原子性。
  
  
  
  

4、system V 信号量

4.1、理解层面

  
  为了合理保护和使用临界资源,当进程想要访问临界资源时,需要先申请信号量。信号量本质是一种计数器,当进程申请信号量成功,计数器 -1,临界资源内部会给进程预留下空间。进程执行自己的临界区代码,访问临界资源,结束后释放信号量,进而计数器+1。
  
  如果某一项操作只有一行汇编,该操作就是原子的。
   信号量计数器是对临界资源的预定机制,要保证其本身也是原子的。那么申请信号量时,计数器--(P操作),释放信号量,计数器++(V操作),二者都必须是原子的。
  
  
  问题:能否让多个进程看到同一个全局变量,将其作为信号量?
  回答:不可以。根据之前所学,CPU在执行进程时有调度,因执行流及其上下文数据的切换,此时会造成该临时变量具有时序问题。
  
  
  
  
  
  
  
  
  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/142474.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Linux 系统移植(二)--系统调试

文章目录 一、 编译文件系统1.1 下载资源安装包1.2 配置模板ARM64目标平台1.3 配置交叉编译器1.4 配置登录用户名和密码1.5 配置Linux 控制台1.6 配置文件系统格式1.7 编译buildroot文件系统 二、编译ARM64 Linux三、启动 Qemu Linux系统参考链接&#xff1a; 一、 编译文件系统…

9.基于粤嵌gec6818开发板小游戏2048的算法实现

2048源码&#xff1a; 感兴趣的可以去了解一下2048优化算法&#xff1a; 基于蒙特卡罗树搜索的_2048_游戏优化算法_刘子正 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/mman.h> #incl…

机器学习笔记:adaBoost

1 介绍 AdaBoost&#xff08;Adaptive Boosting&#xff09;是一种集成学习方法&#xff0c;它的目标是将多个弱分类器组合成一个强分类器 通过反复修改训练数据的权重&#xff0c;使得之前分类错误的样本在后续的分类器中得到更多的关注每一轮中&#xff0c;都会增加一个新的…

大模型时代,如何找准AI发展新方向?TVP读书会与你共探

引言 随着 ChatGPT 掀起人工智能的发展狂潮&#xff0c;大模型时代悄然来临。大模型的出现让人们看到了人工智能在某些方面几乎接近人类智能的“威力”&#xff0c;也由此使人们产生了关于 AGI 的期盼与隐忧。其中&#xff0c;关于大模型是否是人工智能发展的拐点、科技从业者又…

数据结构--堆排序

目录 堆的定义 建立初始化堆的步骤 建立大根堆的代码 大根堆排序的代码 算法效率分析 稳定性 堆的定义 回忆 基于选择排序的特性&#xff1a;选取关键字最小&#xff08;或者最大&#xff09;的元素放入到序列里面&#xff0c;知道了大堆和小堆概念&#xff0c;所以将…

尚硅谷大数据项目《在线教育之实时数仓》笔记002

视频地址&#xff1a;尚硅谷大数据项目《在线教育之实时数仓》_哔哩哔哩_bilibili 目录 第06章 数据仓库环境准备 P006 P007 P008 P009 P010 P011 P012 P013 P014 第06章 数据仓库环境准备 P006 P007 P008 http://node001:16010/master-status [atguigunode001 ~]$ …

Qt --- Day03

<?xml version"1.0" encoding"UTF-8"?> <ui version"4.0"><class>Widget</class><widget class"QWidget" name"Widget"><property name"geometry"><rect><x>0…

Java 调用 GitLabAPI 获取仓库里的文件件 提交记录

1. 需求 项目组 需要做统计&#xff0c;获取每个开发人员的代码提交次数&#xff0c;提交时间&#xff0c;提交人等等&#xff0c;因代码在GitLab上管理&#xff0c;所以需要调用GitLabAPI来获取。 2. 开发 API官网&#xff1a;https://docs.gitlab.com/ee/api/ 2.1 创建自…

3D科研绘图与学术图表绘制:从入门到精通

&#x1f482; 个人网站:【工具大全】【游戏大全】【神级源码资源网】&#x1f91f; 前端学习课程&#xff1a;&#x1f449;【28个案例趣学前端】【400个JS面试题】&#x1f485; 寻找学习交流、摸鱼划水的小伙伴&#xff0c;请点击【摸鱼学习交流群】 3D科研绘图和学术图表绘…

interview6-jvm篇

JVM(Java Virtual Machine)Java程序的运行环境&#xff08;java二进制字节码的运行环境&#xff09; 在JVM中共有四大部分&#xff0c;分别是ClassLoader&#xff08;类加载器&#xff09;、Runtime DataArea&#xff08;运行时数据区&#xff0c;内存分区&#xff09;、Execu…

分类预测 | MATLAB实现WOA-CNN-BiGRU-Attention数据分类预测(SE注意力机制)

分类预测 | MATLAB实现WOA-CNN-BiGRU-Attention数据分类预测&#xff08;SE注意力机制&#xff09; 目录 分类预测 | MATLAB实现WOA-CNN-BiGRU-Attention数据分类预测&#xff08;SE注意力机制&#xff09;分类效果基本描述模型描述程序设计参考资料 分类效果 基本描述 1.MATLA…

【Synapse数据集】Synapse数据集介绍和预处理,数据集下载网盘链接

【Segment Anything Model】做分割的专栏链接&#xff0c;欢迎来学习。 【博主微信】cvxiaoyixiao 本专栏为公开数据集的介绍和预处理&#xff0c;持续更新中。 文章目录 1️⃣Synapse数据集介绍文件结构源文件样图文件内容 2️⃣Synapse数据集百度网盘下载链接官网下载登录下…

TCPIP状态转换

一个TCP连接在其生命周期中经过了一系列的状态跃迁。一个TCP连接的状态包括&#xff1a; LISTEN &#xff1a;表示正在等待来自任何远程TCP和端口的连接请求&#xff0c;调用listen后套接字出于监听状态SYN_SENT : 表示在发送了连接请求后&#xff0c;正在等待匹配的连接请求…

【Linux is not Unix】Linux前言

目录 二战军工的产物——第一台现代电子数字计算机ENIAC&#xff08;埃尼阿克&#xff09; Unix Linux Linux企业应用现状 如今计算机已经应用在我们生活的各个层面&#xff0c;像我们日常使用的笔记本是计算机的一类&#xff0c;可以解决我们生活中遇到的很多问题&#xff…

嵌入式MCU都有什么高级用法?

嵌入式MCU都有什么高级用法&#xff1f; 您举的几个例子&#xff0c;确实是MCU外设的一些高端玩法。只是不知道您是否想过&#xff0c;既然这些机制是被 人设计出来的&#xff0c;那它就是种标准用法。从微控制器的发展历程来看&#xff0c;许多硬件机制都是有了实际 需求后才…

字节8年经验之谈 —— 10大自动化测试框架总结!

软件行业正迈向自主、快速、高效的未来。为了跟上这个高速前进的生态系统的步伐&#xff0c;必须加快应用程序的交付时间&#xff0c;但不能以牺牲质量为代价。快速实现质量是必要的&#xff0c;因此质量保证得到了很多关注。为了满足卓越的质量和更快的上市时间的需求&#xf…

大屏大概是怎么个开发法(前端)

写在前面&#xff0c;博主是个在北京打拼的码农&#xff0c;从事前端工作5年了&#xff0c;做过十多个大大小小不同类型的项目&#xff0c;最近心血来潮在这儿写点东西&#xff0c;欢迎大家多多指教。 对于文章中出现的任何错误请大家批评指出&#xff0c;一定及时修改。有任何…

产品经理如何科学的进行需求调研?

导语&#xff1a;作为产品经理&#xff0c;需求调研是开展工作的重要环节之一。科学、有效地进行需求调研不仅可以帮助产品经理更好地了解用户需求&#xff0c;还能指导产品设计和功能开发&#xff0c;提升产品的竞争力。本文将介绍几种科学的方法和技巧&#xff0c;帮助产品经…

Powershell 实现禁用密码复杂性,空密码

前提条件 开启wmi,配置网卡,参考 实现一键关闭密码策略和远程空密码登录 最近客户需要的一个无法理解的需求,需要远程登录不输入密码,安全性没有了还要实现,没办法客户是上帝,客户怎么开心怎么来都行,安全性问题告知不重视,实际环境不建议一下操作,只要联网你被黑的哦…

L1-033 出生年 c++解法

一、题目再现 以上是新浪微博中一奇葩贴&#xff1a;“我出生于1988年&#xff0c;直到25岁才遇到4个数字都不相同的年份。”也就是说&#xff0c;直到2013年才达到“4个数字都不相同”的要求。本题请你根据要求&#xff0c;自动填充“我出生于y年&#xff0c;直到x岁才遇到n个…