C++ - 分叉进程无法从共享内存中读取

C++ - Forked Process Can't Read from Shared Memory

我正在学习进程管理和共享内存,我想知道是否有人可以帮助我。我正在尝试读取一个带有分叉父进程的大型文本文件 (2MB),并将其提供给相应的子进程以进行......很好......处理。但是,我的概念验证小程序无法读取我写入共享内存的数据。调用 'capacity()' 和 'length()' 工作正常,但实际上试图检索存储在那里的数据 returns 一个空字符串。我什至尝试将指针中的内容复制到字符数组中,但仍然没有。有什么想法吗?

程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

using namespace std;

#define SHM_SIZE 2000000    // 2 MB of shared memory

int main(int argc, char* argv[])
{
    string* data = (string*) mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, 
    MAP_SHARED | MAP_ANONYMOUS, -1, 0);

printf("Shared memory capacity before reserve: %ld\n", data->capacity());
data->reserve(SHM_SIZE); // make sure string can use all available space
printf("Shared memory capacity after reserve: %ld\n", data->capacity());

switch(fork())
{
    case -1:
        printf("Something went wrong ... exiting program\n");
        break;
    case 0:
        usleep(1000 * 1000 * 2); // force child to wait for sanity purposes
        printf("\nChild capacity:  %ld\n", data->capacity());
        printf("Child contents:  %s\n", data->c_str());
        break;
    default:
        // Sample input ... file reading logic omitted for brevity
        *data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean leo elit, aliquam "
            "vitae aliquam sed, rhoncus et mi. Aliquam erat volutpat. Phasellus non urna sit amet "
            "enim tincidunt aliquam quis sit amet orci. Maecenas velit turpis, venenatis eu diam "
            "vel, varius volutpat nulla. Nam mi massa, tristique eget imperdiet id, dictum ut urna. "
            "Nulla id massa placerat, finibus purus quis, ornare neque. Vivamus eget varius ante. "
            "Aenean varius ac neque quis ultricies. Donec vitae bibendum dolor, vitae tempor augue.";
        printf("\nParent capacity:  %ld\n", data->capacity());
        printf("Parent contents:  %s\n", data->c_str());

        wait(NULL);
        munmap(data, sizeof *data);
        printf("\nHello world\n");
        break;
}

return 0;
}

输出:

MacBook-Pro:sharedmem-test slehr$ ./sharedmem-mapred 
Shared memory capacity before reserve: 22
Shared memory capacity after reserve: 2000015

Parent capacity:  2000015
Parent contents:  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean leo elit, aliquam vitae aliquam sed, rhoncus et mi. Aliquam erat volutpat. Phasellus non urna sit amet enim tincidunt aliquam quis sit amet orci. Maecenas velit turpis, venenatis eu diam vel, varius volutpat nulla. Nam mi massa, tristique eget imperdiet id, dictum ut urna. Nulla id massa placerat, finibus purus quis, ornare neque. Vivamus eget varius ante. Aenean varius ac neque quis ultricies. Donec vitae bibendum dolor, vitae tempor augue.

Child capacity:  2000015
Child contents:  

Hello world
MacBook-Pro:sharedmem-test slehr$ 

A std::string 可能是一些具有大小、长度、指针(或类似的东西)的数据结构。你不能期望它的内部指针指向共享内存

对于 mmap-ed 内存,您应该使用原始指针,例如

auto data = (char*) mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, 
                         MAP_SHARED | MAP_ANONYMOUS, -1, 0);

当然你应该测试失败:

if (data==(char*)MAP_FAILED) { perror("mmap"); exit(EXIT_FAILURE); };

再读一遍mmap(2)

您可以考虑 shm_overview(7) (and look at sem_oveview(7) 进行同步)

阅读Advanced Linux Programming