Tars框架TC_Mmap类的使用--进程间通信之内存映射

版权声明:个人学习之路,若有误,欢迎指正。 https://blog.csdn.net/y396397735/article/details/87932885

本例定义一个类,创建,写入,读取操作函数,直接上代码吧,具体行为注释写清楚了:

	class TestMMap
    {
    public:
        
        /************************************!
         * @brief  // 创建
         * @param  size_t n 文件长度
         * @return void
         ************************************/
        void create(size_t n)
        {
            TC_Mmap mmap;
            cout << "create mmap" << endl;
            mmap.mmap("mmap.dat", n);
            mmap.munmap();
            cout << "create mmap OK" << endl;
        }

        /************************************!
         * @brief  向这块内存写数据
         * @param  const string & s 写入的字符串
         * @return void
         ************************************/
        void write(const string &s)
        {
            TC_Mmap mmap;
            cout << "write mmap" << endl;
            mmap.mmap("mmap.dat", 1000);
            memcpy(mmap.getPointer(), s.c_str(), s.length());

            sleep(100); // 多等会便于比较测试结果

            mmap.munmap();
        }
        
        /************************************!
         * @brief  // 读取文件中内容
         * @return void
         ************************************/
        void read()
        {
            TC_Mmap mmap;
            cout << "read mmap" << endl;
            mmap.mmap("mmap.dat", 1000);

            string s;
            s.assign((char*)mmap.getPointer(), mmap.getSize());
            mmap.munmap();

            cout << s << endl;
        }


        // 静态测试函数
        static void test(int argc, char *argv[])
        {
            try
            {
                TC_Option option; //命令行解析对象
                TestMMap testMMap{};
                // 解析命令输入
                option.decode(argc, argv); 

                int pagesize = sysconf(_SC_PAGESIZE);

                cout << "pagesize:" << pagesize << endl;

                // 命令行输入--test=create 表示创建这么个文件
                if (option.getValue("test") == "create") 
                {
                    size_t n = 50;
                    testMMap.create(n);
                }
                // 命令行输入--test=write --c=2019年2月24日20:26:52 表示写入c对应数据 即"2019年2月24日20:26:52"
                else if (option.getValue("test") == "write")
                {
                    testMMap.write(option.getValue("c"));
                }
                // 命令行输入--test=read 表示读取共享内存中的数据
                else if (option.getValue("test") == "read") 
                {
                    testMMap.read();
                }
            }
            catch (exception &ex)
            {
                cout << ex.what() << endl;
            }
        }
    };

测试程序结果:
首先开一个终端窗口命令输入:

yu@ubuntu:~/projects/FishTars/bin/x64/Debug$ ./FishTars.out --test=create

执行结果:

pagesize:4096
create mmap
create mmap OK

继续输入:

 ./FishTars.out --test=write --c=2019年2月24日20:26:52

执行结果:

pagesize:4096
write mmap

新开一个终端输入:

yu@ubuntu:~/projects/FishTars/bin/x64/Debug$ ./FishTars.out --test=read

执行结果:

pagesize:4096
read mmap
2019年2月24日20:26:52

即两个终端进程通过mmap进行进程间通信。


实践环境:vs2017+ubuntu14.04
代码路径:
https://github.com/lesliefish/FishTars/blob/master/code/FishTars/test/utiltest/test_mmap.h

猜你喜欢

转载自blog.csdn.net/y396397735/article/details/87932885