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;
}

输出:

C++ 共享内存

 

C++ 共享内存

 

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

在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;
}

输出:

C++ 共享内存

C++ 共享内存

原文链接: https://www.cnblogs.com/tyche116/p/12107287.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 共享内存

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/308537

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午6:06
下一篇 2023年2月16日 上午6:08

相关推荐