<Linux进程通信之共享内存>——《Linux》

目录

一、system V共享机制

1.共享内存示意图

2.共享内存数据结构

3.共享内存函数

3.1shmget函数

3.2 shmat函数

3.3 shmdt函数

3.4 shmctl函数

3.5 实例代码:

3.6 结果演示:

4. 创建共享内存

5. 基于共享内存与管道进行访问控制的共享内存读写

6. system V消息队列 

7. system V信号量 

8. 进程互斥

后记:●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教!

                                                                           ——By 作者:新晓·故知


一、system V共享机制

共享内存区是最快的 IPC 形式。一旦这样的内存映射到共享它的进程的地址空间,这些进程间数据传递不再涉及到内核,换句话说是进程不再通过执行进入内核的系统调用来传递彼此的数据。

1.共享内存示意图

(1)创建共享内存——删除共享内存      ------>OS内部实现

(2)关联共享内存——去关联共享内存   ------->进程实现

2.共享内存数据结构

Linux内核源码:

struct shmid_ds
{
    struct ipc_perm shm_perm;    /* operation perms */
    int shm_segsz;               /* size of segment (bytes) */
    __kernel_time_t shm_atime;   /* last attach time */
    __kernel_time_t shm_dtime;   /* last detach time */
    __kernel_time_t shm_ctime;   /* last change time */
    __kernel_ipc_pid_t shm_cpid; /* pid of creator */
    __kernel_ipc_pid_t shm_lpid; /* pid of last operator */
    unsigned short shm_nattch;   /* no. of current attaches */
    unsigned short shm_unused;   /* compatibility */
    void *shm_unused2;           /* ditto - used by DIPC */
    void *shm_unused3;           /* unused */
};

3.共享内存函数

3.1shmget函数

功能:用来创建共享内存
原型:int shmget(key_t key, size_t size, int shmflg);
参数:
key:这个共享内存段名字
size:共享内存大小
shmflg:由九个权限标志构成,它们的用法和创建文件时使用的mode模式标志是一样的
返回值:成功返回一个非负整数,即该共享内存段的标识码;失败返回-1

 3.2 shmat函数

功能:将共享内存段连接到进程地址空间

原型:
void *shmat(int shmid, const void *shmaddr, int shmflg);
参数:
shmid: 共享内存标识
shmaddr:指定连接的地址
shmflg:它的两个可能取值是SHM_RND和SHM_RDONLY
返回值:成功返回一个指针,指向共享内存第一个节;失败返回-1
说明:
shmaddr为NULL,核心自动选择一个地址
shmaddr不为NULL且shmflg无SHM_RND标记,则以shmaddr为连接地址。
shmaddr不为NULL且shmflg设置了SHM_RND标记,则连接的地址会自动向下调整为SHMLBA的整数倍。公式:
shmaddr - (shmaddr % SHMLBA)
shmflg=SHM_RDONLY,表示连接操作用来只读共享内存

3.3 shmdt函数

功能:将共享内存段与当前进程脱离
原型:
int shmdt(const void *shmaddr);
参数:
shmaddr: 由shmat所返回的指针
返回值: 成功返回0;失败返回-1
注意:将共享内存段与当前进程脱离不等于删除共享内存段

3.4 shmctl函数

功能:用于控制共享内存
原型:
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
参数:
shmid:由shmget返回的共享内存标识码
cmd:将要采取的动作(有三个可取值)
buf:指向一个保存着共享内存的模式状态和访问权限的数据结构
返回值:成功返回0;失败返回-1

3.5 实例代码:

测试代码结构
makefile:
.PHONY:all
all:server client
client:client.c comm.c
 gcc -o $@ $^
server:server.c comm.c
 gcc -o $@ $^
.PHONY:clean
clean:
 rm -f client server

comm.h:

#ifndef _COMM_H_
#define _COMM_H_
# include <stdio.h>
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/shm.h>
# define PATHNAME "."
# define PROJ_ID 0x6666
int createShm(int size);
int destroyShm(int shmid);
int getShm(int size);
# endif

comm.c:

#include "comm.h"

static int commShm(int size, int flags)
{
    key_t key = ftok(PATHNAME, PROJ_ID);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    int shmid = 0;
    if ((shmid = shmget(_key, size, flags)) < 0)
    {
        perror("shmget");
        return -2;
    }
    return shmid;
}
int destroyShm(int shmid)
{
    if (shmctl(shmid, IPC_RMID, NULL) < 0)
    {
        perror("shmctl");
        return -1;
    }
    return 0;
}
int createShm(int size)
{
    return commShm(size, IPC_CREAT | IPC_EXCL | 0666);
}
int getShm(int size)
{
    return commShm(size, IPC_CREAT);
}

server.c:

#include "comm.h"
int main()
{
    int shmid = createShm(4096);
    char *addr = shmat(shmid, NULL, 0);
    sleep(2);
    int i = 0;
    while (i++ < 26)
    {
        printf("client# %s\n", addr);
        sleep(1);
    }
    shmdt(addr);
    sleep(2);
    destroyShm(shmid);
    return 0;
}

client.c:

#include "comm.h"
int main()
{
    int shmid = getShm(4096);
    sleep(1);
    char *addr = shmat(shmid, NULL, 0);
    sleep(2);
    int i = 0;
    while (i < 26)
    {
        addr[i] = 'A' + i;
        i++;
        addr[i] = 0;
        sleep(1);
    }
    shmdt(addr);
    sleep(2);
    return 0;
}

3.6 结果演示:

 ctrl+c终止进程,再次重启

# ./server
shmget: File exists
# ipcs -m
------ Shared Memory Segments --------
key          shmid    owner   perms  bytes  nattch status
0x66026a25   688145   root    666    4096   0
       
# ipcrm -m 688145 #删除shm ipc资源,注意,不是必须通过手动来删除,这里只为演示相关指令,删除IPC资源是进程该做的事情
注意:共享内存没有进行同步与互斥!

4. 创建共享内存

 

 获取同一key值:

 现象:

  • 当我们运行完毕创建全新的共享内存的代码后(进程退出),但是之后在第二次、第三次、第n次的时候,再去申请就显示file存在。

这是因为systemV下的共享内存,它得生命周期是跟随内核的!

  • 释放共享内存的解决方法:

(1)显式删除

(2)重启kernel(OS)

  • 如何知道有哪些是IPC资源?

        通过指令查看:

        ipcs -m:查看shm list

        ipcs -q

        ipcs -s等

  • 如何显式删除?

      (1) ipcrm -m (shmid号)

         例如:ipcrm -m 5

     (2)通过系统接口

shell脚本:

while :; do ipcs -m; sleep 1;done

 

 

共享内存实际上是映射到了进程地址空间的用户区(堆、栈之间)!对于每一个进程而言,挂接到自己的上下文中的共享内存,属于自己的空间,类似于堆空间或者栈空间,可以被用户直接使用。

IpcShmCli.cc:

#include "Comm.hpp"
#include "Log.hpp"
using namespace std;

// 充当使用内存的角色
int main()
{

    //1.创建相同的key值
    key_t key = CreateKey();
    //cout << "key(hex): 0x" << hex << key << endl;
    Log() << "key(hex): 0x" << hex << key << endl;

    //2.获取共享内存
    int shmid = shmget(key,MEM_SIZE,IPC_CREAT);
    if(shmid < 0)
    {
        Log() << "shmget: " << strerror(errno) << "\n";
        return 2;
    }

    //3.挂接(关联)
    char *str = (char*)shmat(shmid,nullptr,0);
    
    //4.使用
    // sleep(6);  //仅用于观察
    
    //测试1
    int cnt = 0;      //未使用任何系统调用接口,这是因为共享内存实际上是映射到了进程地址空间的用户区(堆、栈之间)
    while(cnt <= 26)           
    {
        str[cnt] = 'A' +cnt;
        ++cnt;
        str[cnt] = '\0';
        sleep(1);
    }

    //5.去关联
    shmdt(str);
    return 0;
}

IpcShmSer.cc:

#include "Comm.hpp"
#include "Log.hpp"
using namespace std;

// 充当创建内存的角色

// 创建全新的共享内存
const int flags = IPC_CREAT | IPC_EXCL;
int main()
{
    key_t key = CreateKey();
    Log() << "key(hex): 0x" << hex << key << endl;
    Log() << "Begin create share memory!\n";
    int shmid = shmget(key, MEM_SIZE, flags | 0666); //"| 0666"权限控制
    if (shmid < 0)
    {
        Log() << "shmget: " << strerror(errno) << "\n";
        return 2;
    }
    //sleep(3);
    Log() << "Create shm success! shmid: " << shmid << "\n";
    // 使用
    // 1.将共享内存和自己的进程产生关联attach
    char *str = (char *)shmat(shmid, nullptr, 0);
    Log() << "Attach shm: " << shmid << " success!\n";
    //sleep(3);

    //测试共享内存通信
    while(true)
    {
        printf(".%s\n",str);
        sleep(1);
    }

    // 2.去关联
    shmdt(str);
    Log() << "Dttach shm: " << shmid << " success!\n";
    //sleep(3);

    // 删除
    shmctl(shmid, IPC_RMID, nullptr);
    Log() << "Delete shm,shmid: " << shmid << " success!\n";

    // sleep(3);
    return 0;
}

Comm.hpp:

#pragma once

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#include <cassert>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "Log.hpp"

#define PATH_NAME "/home/study/lesson/lesson26"
#define PROJ_ID 0x14
#define MEM_SIZE 4096 // 单位是字节

key_t CreateKey()
{
    key_t key = ftok(PATH_NAME, PROJ_ID);
    if (key < 0)
    {
        std::cerr << "ftok:" << strerror(errno) << std::endl;
        exit(1);
    }
    return key;
}

Log.hpp:

#pragma once

#include <iostream>
#include <ctime>

std::ostream &Log()
{
    std::cout << "For Debug | "
              << "timestamp: " << (uint64_t)time(nullptr) << " | ";
    return std::cout;
}

makefile:

.PHONY:all
all:IpcShmCli IpcShmSer

IpcShmCli:IpcShmCli.cc
	g++ -o $@ $^ -std=c++11
IpcShmSer:IpcShmSer.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f IpcShmCli IpcShmSer

共享内存,因为其自身的特性,使得它没有访问控制!
共享内存被双方直接看到,属于双方的用户空间,可以直接通信,但是不安全!
共享内存,是所有进程间通信,速度最快的!

 

5. 基于共享内存与管道进行访问控制的共享内存读写

将命名管道和共享内存创建好,把缓冲区数据写好,再通知读取。建立命名管道进行通信控制,管道具有同步访问策略,借此控制共享内存的访问。

IpcShmCli.cc:

#include "Comm.hpp"
#include "Log.hpp"
using namespace std;

// 充当使用内存的角色
int main()
{
    //打开以管道通信方式文件
    int fd = Open(FIFO_FILE,WRITER);

    //1.创建相同的key值
    key_t key = CreateKey();
    //cout << "key(hex): 0x" << hex << key << endl;
    Log() << "key(hex): 0x" << hex << key << endl;

    //2.获取共享内存
    int shmid = shmget(key,MEM_SIZE,IPC_CREAT);
    if(shmid < 0)
    {
        Log() << "shmget: " << strerror(errno) << "\n";
        return 2;
    }

    //3.挂接(关联)
    char *str = (char*)shmat(shmid,nullptr,0);
    
    //4.使用
    // sleep(6);  //仅用于观察
    
    // //测试1
    // int cnt = 0;      //未使用任何系统调用接口,这是因为共享内存实际上是映射到了进程地址空间的用户区(堆、栈之间)
    // while(cnt <= 26)           
    // {
    //     str[cnt] = 'A' +cnt;
    //     ++cnt;
    //     str[cnt] = '\0';
    //     sleep(1);
    // }

    // //测试2
    // while(true)
    // {
    //     cout<<"Please Enter# ";
    //     fflush(stdout);
    //     ssize_t s= read(0,str,MEM_SIZE);
    //     if(s > 0)
    //     {
    //         str[s] = '\0';
    //     }
    // }

    //测试管道通信
    while(true)
    {
        printf("Please Enter# ");
        fflush(stdout);
        ssize_t s = read(0, str, MEM_SIZE);
        if(s > 0)
        {
            str[s] = '\0';
        }
        Signal(fd);
    }

    //5.去关联
    shmdt(str);

    return 0;
}

 IpcShmSer.cc:

#include "Comm.hpp"
#include "Log.hpp"
using namespace std;

// 充当创建内存的角色

// 创建全新的共享内存
const int flags = IPC_CREAT | IPC_EXCL;
int main()
{
    // 测试管道通信
    CreateFifo();
    int fd = Open(FIFO_FILE, READER);
    assert(fd >= 0);

    key_t key = CreateKey();
    Log() << "key(hex): 0x" << hex << key << endl;
    Log() << "Begin create share memory!\n";
    int shmid = shmget(key, MEM_SIZE, flags | 0666); //"| 0666"权限控制
    if (shmid < 0)
    {
        Log() << "shmget: " << strerror(errno) << "\n";
        return 2;
    }
    //sleep(3);
    Log() << "Create shm success! shmid: " << shmid << "\n";
    // 使用
    // 1.将共享内存和自己的进程产生关联attach
    char *str = (char *)shmat(shmid, nullptr, 0);
    Log() << "Attach shm: " << shmid << " success!\n";
    //sleep(3);

    // //测试共享内存通信
    // while(true)
    // {
    //     printf(".%s\n",str);
    //     sleep(3);
    // }

    // 测试管道通信
   while(true)
    {
        // 让读端进行等待
        if(Wait(fd) <= 0) break; 
        printf(".%s\n", str);
        sleep(1);
    }
    // 2.去关联
    shmdt(str);
    Log() << "Dttach shm: " << shmid << " success!\n";
    //sleep(3);

    // 删除
    shmctl(shmid, IPC_RMID, nullptr);
    Log() << "Delete shm,shmid: " << shmid << " success!\n";

    Close(fd, FIFO_FILE);
    // sleep(3);
    return 0;
}

Comm.hpp:

#pragma once

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#include <cassert>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "Log.hpp"

#define PATH_NAME "/home/study/lesson/lesson26"
#define PROJ_ID 0x14
#define MEM_SIZE 4096 // 单位是字节
#define FIFO_FILE ".fifo"  //建立命名管道进行通信控制,管道具有同步访问策略,借此控制共享内存的访问



key_t CreateKey()
{
    key_t key = ftok(PATH_NAME, PROJ_ID);
    if (key < 0)
    {
        std::cerr << "ftok:" << strerror(errno) << std::endl;
        exit(1);
    }
    return key;
}

//下面测试通过管道进行通信
void CreateFifo()
{
    umask(0);
    if(mkfifo(FIFO_FILE,0666) < 0)
    {
        Log() << strerror(errno) << "\n";
        exit(2);
    }
}
//打开文件为了读、写
#define READER O_RDONLY
#define WRITER O_WRONLY
int Open(const std::string &filename, int flags)
{
    return open(filename.c_str(), flags);
}

int Wait(int fd)
{
    uint32_t values = 0;
    ssize_t s = read(fd, &values, sizeof(values));
     //assert(s == sizeof(values));
    //(void)s;  //消除告警
    return s;   //让另一方去判断警告
}

int Signal(int fd)
{
    uint32_t cmd = 1;
    write(fd, &cmd, sizeof(cmd));
}

int Close(int fd, const std::string filename)
{
    close(fd);
    unlink(filename.c_str());
}

Log.hpp:

#pragma once

#include <iostream>
#include <ctime>

std::ostream &Log()
{
    std::cout << "For Debug | "
              << "timestamp: " << (uint64_t)time(nullptr) << " | ";
    return std::cout;
}

makefile:

.PHONY:all
all:IpcShmCli IpcShmSer

IpcShmCli:IpcShmCli.cc
	g++ -o $@ $^ -std=c++11
IpcShmSer:IpcShmSer.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f IpcShmCli IpcShmSer

共享内存不作访问控制,可以通过信号量进行对资源保护!

6. system V消息队列 

消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法
每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值
特性方面
IPC 资源必须删除,否则不会自动清除,除非重启,所以 system V IPC 资源的生命周期随内核

 

 

 

7. system V信号量 

信号量主要用于同步和互斥的,下面先来看看什么是同步和互斥。

 

 

 

 以上的生命周期全部是跟随OS内核,有各自的系统调用接口。

总结:

8. 进程互斥

由于各进程要求共享资源,而且有些资源需要互斥使用,因此各进程间竞争使用这些资源,进程的这种关系为进程的互斥
系统中某些资源一次只允许一个进程使用,称这样的资源为临界资源或互斥资源。
在进程中涉及到互斥资源的程序段叫临界区
特性方面
*IPC 资源必须删除,否则不会自动清除,除非重启,所以 system V IPC 资源的生命周期随内核
扩展:
mmap

查看系统接口函数我们发现:每一个函数都包含有struct ipc_perm{ }结构。

每一种IPC资源描述结构体第一个都是一样的。

 

以上就是Linux内核对IPC资源的管理机制。不断地继承,用父类指针指向子类,需要使用哪种就强转获取即可(类似于C++中的多态或C++切片) 。这种方式的进程间通信使用不太多。设计思想很好,但实现和现代主流先进的操作系统编码不匹配。

后记:
●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教!

                                                                           ——By 作者:新晓·故知

猜你喜欢

转载自blog.csdn.net/m0_57859086/article/details/128353523
今日推荐