• C++ 共享内存


    共享内存作为进程通讯的一种方式,通过内存映射文件,然后允许不同的进程访问同一块内存实现进程间通讯。

    示例主要分为读和写两个不同的进程。

    ########################################################################################################

    在windows下实现:

    share_write.cpp

    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        unsigned long buff_size = 4096;
        char share_buffer[] = "hello world"; //share_data
    
        // create shared memory file
        HANDLE file_shared_handler = CreateFile("shared_memory", //shared_file_name,
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL,
            OPEN_ALWAYS, // open exist or create new, overwrite file
            FILE_ATTRIBUTE_NORMAL,
            NULL);
    
        if (file_shared_handler == INVALID_HANDLE_VALUE)
            cout << "create file error" << endl;
    
        HANDLE file_mapping_handler = CreateFileMapping(
            file_shared_handler, // Use paging file - shared memory
            NULL,                 // Default security attributes
            PAGE_READWRITE,       // Allow read and write access
            0,                    // High-order DWORD of file mapping max size
            buff_size,            // Low-order DWORD of file mapping max size
            "shared_memory");  // Name of the file mapping object
    
        // map memory file view, get pointer to the shared memory
        LPVOID lp = MapViewOfFile(
            file_mapping_handler,  // Handle of the map object
            FILE_MAP_ALL_ACCESS,  // Read and write access
            0,                    // High-order DWORD of the file offset
            0,                    // Low-order DWORD of the file offset
            buff_size);           // The number of bytes to map to view
    
        // copy data to shared memory
        memcpy(lp, &share_buffer, sizeof(share_buffer));
    
        FlushViewOfFile(lp, buff_size);
    
        // process wait here for other task to read data
        cout << "finish write, wait ..." << endl;
        Sleep(20000);
    
        UnmapViewOfFile(lp);
        CloseHandle(file_mapping_handler);
        CloseHandle(file_shared_handler);
    
        return 0;
    }

    share_read.cpp

    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        HANDLE file_mapping_handler = OpenFileMapping(
            FILE_MAP_ALL_ACCESS,
            NULL,
            "shared_memory");
    
        LPVOID lp_base = MapViewOfFile(
            file_mapping_handler,
            FILE_MAP_ALL_ACCESS,
            0,
            0,
            0);
    
        cout << "read shared memory: " << endl;
        const unsigned long buff_size = 4096;
        char *share_buffer = (char *)lp_base;
    
        cout << share_buffer << endl;
        
        UnmapViewOfFile(lp_base);
        CloseHandle(file_mapping_handler);
    
        return 0;
    }

    输出:

    ##########################################################################################################

    在ubuntu下实现:

    share_write.cpp

    #include <iostream>
    #include <string.h>
    #include <sys/mman.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    using namespace std;
    
    int main()
    {
        char share_buffer[] = "hello world";
        // create mmap file
        int file_handle = open("/home/chain/myproj/shared_memory", O_CREAT | O_RDWR | O_TRUNC, 00777);
        if (file_handle < 0)
            cout << "create file error" << endl;
    
        size_t buffer_size = sizeof(share_buffer);
        ftruncate(file_handle, buffer_size); // extend file size
    
        // map memory to file
        void *p = mmap(NULL, buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, file_handle, 0);
    
        // copy data to shared memory
        memcpy(p, &share_buffer, buffer_size);
        cout << "finish write: "<< share_buffer <<" . wait ..." << endl;
    
        sleep(200000);
    
        // unmap and close
        munmap(p, buffer_size);
        close(file_handle);
    
        return 0;
    }

    share_read.cpp

    #include <iostream>
    #include <sys/mman.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    using namespace std;
    
    int main()
    {
        // open mmap file
        int file_handle = open("/home/chain/myproj/shared_memory", O_RDONLY, 00777);
        if (file_handle < 0)
            cout << "open file error" << endl;
    
        const unsigned long buff_size = 4096;
        size_t read_size = buff_size;
    
        // map file to memory
        void *p = mmap(NULL, read_size, PROT_READ, MAP_SHARED, file_handle, 0);
        char *share_buffer = (char *)p;
        cout << "read shared memory: " << share_buffer << endl;
    
        // unmap and close
        munmap(p, read_size);
        close(file_handle);
    
        return 0;
    }

    输出:

  • 相关阅读:
    Jquery想说爱你不容易
    关于css
    sass相关实例
    web前端学习之HTML
    web前端学习
    软件工程来换网前端设计
    关于前端开发的相关资料及例子
    四则运算
    自我介绍
    关于读完《软件工程》之后不解的问题
  • 原文地址:https://www.cnblogs.com/tyche116/p/12107287.html
Copyright © 2020-2023  润新知