System V|Shared memory basic communication framework construction|【Super detailed code explanation and comments】

55b442c530014f30b6a3b4eda19d23ec.png

 


foreword

那么这里博主先安利一下一些干货满满的专栏啦!

手撕数据结构https://blog.csdn.net/yu_cblog/category_11490888.html?spm=1001.2014.3001.5482这里包含了博主很多的数据结构学习上的总结,每一篇都是超级用心编写的,有兴趣的伙伴们都支持一下吧!
算法专栏https://blog.csdn.net/yu_cblog/category_11464817.html这里是STL源码剖析专栏,这个专栏将会持续更新STL各种容器的模拟实现。

STL源码剖析https://blog.csdn.net/yu_cblog/category_11983210.html?spm=1001.2014.3001.5482


What is System V shared memory

The essence of inter-process communication: let different processes see the same resource

Shared memory areas are the fastest form of IPC. Once such memory is mapped into the address space of the processes sharing it, these inter-process data transfers no longer involve the kernel, in other words processes no longer pass data to each other by executing system calls into the kernel.

When we use pipes for inter-process communication, we know that whether it is in the form of anonymous pipes or named pipes, some space needs to be created on the disk in the end.

The shared memory solution only needs to create a shared space at the memory level!

02e64df334f04d408603eb90060342f2.png

As for the detailed system call interface, the blogger will explain it in the comments of the code.

Today, the blogger leads everyone to set up the framework. Regarding the specific realization of communication, the blogger will explain in detail in the next issue!


Code download address 

Git address https://github.com/Yufccode/BitCode/tree/main/Linux/%E4%BB%A3%E7%A0%81/1126_SystemV%E5%85%B1%E4%BA%AB%E5%86 %85%E5%AD%98%E6%A1%86%E6%9E%B6%E6%90%AD%E5%BB%BA


 Partial realization principle

9a36e3a23e544a32b771e1064b2c6098.png9f07e20119cf4915a225d4b3c39ef59c.png 41d6034a0e6d40828950a47237b57177.png9dee891625e0436090362a22a31233b2.png4566e7cc304045ebb76a56c18087b075.png 


shmServer.cc and shmClient.cc code 

Note that only these two codes are not enough. Partners who need to download the code to learn should download the code from the link provided by the blogger.

Inside is a complete project!

shmServer.cc


#include "comm.hpp"

// 为了让key更好看,和命令行输出的对应起来
// 写一个转换成16进制的函数
std::string TransToHex(key_t k)
{
    char buffer[32];
    snprintf(buffer, sizeof buffer, "0x%x", k);
    return buffer;
}

int main()
{
    // 1.创建公共的key值
    key_t k = ftok(PATH_NAME, PROJ_ID);
    assert(k != -1);
    Log("create key done", Debug) << " server key: " << TransToHex(k) << std::endl;

    // 2.创建共享内存 -- 建议创建一个全新的共享内存 -- Server是通信的发起者
    int shmid = shmget(k, SHM_SIZE, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid == -1)
    {
        // 创建失败
        perror("shmget");
        exit(1);
    }
    Log("create shm done", Debug) << " shmid: " << shmid << std::endl;

    //3.将制定的共享内存,挂接到自己的地址空间
    sleep(3);
    char* shmaddr = (char*)shmat(shmid,nullptr,0);
    Log("attach shm done", Debug) << " shmid: " << shmid << std::endl;
    sleep(3);
    //我们会看到 挂接数会从0变成1!

    // ================= 这里面就是通信的逻辑了!================= //
    //...

    // ================= 这里面就是通信的逻辑了!================= //

    //4.解除挂接
    //  将制定的共享内存,从自己的地址空间中去关联
    int n = shmdt(shmaddr);
    assert(n!=-1);
    (void)n;
    Log("detach shm done", Debug) << " shmid: " << shmid << std::endl;
    sleep(3);


    // last. 删除共享内存
    //IPC_RMID表示,即便是有进程和当下的shm挂接,依旧删除共享内存
    int n = shmctl(shmid, IPC_RMID, nullptr);
    assert(n != -1);
    (void)n;
    Log("delete shm done", Debug) << " shmid: " << shmid << std::endl;
    return 0;
}

 shmClient.cc


#include "comm.hpp"

int main()
{
    key_t k = ftok(PATH_NAME, PROJ_ID);
    if(k<0)
    {
        Log("create key failed", Error) << " client key: " << k << std::endl;
        exit(1);
    }
    Log("create key done", Debug) << " client key: " << k << std::endl;
    //获取共享内存
    int shmid = shmget(k,SHM_SIZE,IPC_CREAT);
    if(shmid<0)
    {
        Log("create shm success", Error) << " client key: " << k << std::endl;
        exit(2);
    }
    Log("create shm failed", Debug) << " client key: " << k << std::endl;
    sleep(5);
    //
    char* shmaddr = (char*)shmat(shmid,nullptr,0);
    if(shmaddr ==nullptr)
    {
        Log("attach shm failed", Error) << " client key: " << k << std::endl;
        exit(3);
    }
    Log("attach shm success", Debug) << " client key: " << k << std::endl;
    sleep(5);


    //使用


    //去关联
    int n = shmdt(shmaddr);
    assert(n!=-1);
    Log("detach shm success", Debug) << " client key: " << k << std::endl;
    sleep(5);

    //client要不要chmctl删除呢?不需要!!!
    
    return 0;
}

end

Seeing this, I believe that everyone has a certain understanding of the basic construction of the System V communication structure! If you think this article is helpful to you, don't be stingy with your likes and collections!

 

Guess you like

Origin blog.csdn.net/Yu_Cblog/article/details/129233517