gpt4 book ai didi

c++ - 以链表指针为全局变量的多进程c++(11)

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:08 26 4
gpt4 key购买 nike

我有给定的经典问题 here , herehere还有here ,

但是,我想要一个子进程在双向链表的末尾插入一个元素。指向列表第一个元素的点是全局的,我想从主进程访问所有列表元素,而且下次我使用 fork 分支主进程时,我想访问所有元素,并更新它们,并插入更多元素,进而主进程再次能够访问修改后的列表。

每个进程都通过 execvp 的系统调用退出(我需要它们能够使用不同数量的参数调用东西)。

我可能问的问题太笼统了,但我个人除了分支和在列表末尾插入一个元素之外没有得到任何进一步的结果。因此,我实际上没有一行代码可以带我去我想去的地方。我不知道如何在这种情况下使用 shm()。

请帮忙。

最佳答案

你可以试试这个..我只是从头开始写的..它是跨平台的,所以这总是一个加号。分配器和池可以重新用于任何东西。例如,您可以将任何 STL 容器分配到堆栈上或任何您想要的地方..

共享内存.hpp:

#ifndef SHAREDMEMORY_HPP_INCLUDED
#define SHAREDMEMORY_HPP_INCLUDED

#if defined _WIN32 || defined _WIN64
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/mman.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#endif

#include <string>
#include <cstdint>

class SharedMemory
{
private:
std::string name;
std::size_t size;
void* data;
void* hFileMap;

public:
SharedMemory(std::string name, std::size_t size) : name(name), size(size), data(nullptr), hFileMap(nullptr) {};
~SharedMemory();

bool Open();
bool Create();

std::size_t GetSize() const {return this->size;}
void* GetPointer() const {return this->data;}
};

#endif // SHAREDMEMORY_HPP_INCLUDED

共享内存.cpp:

#include "SharedMemory.hpp"

SharedMemory::~SharedMemory()
{
if (data)
{
#if defined _WIN32 || defined _WIN64
UnmapViewOfFile(data);
data = nullptr;

if (hFileMap)
{
if (CloseHandle(hFileMap))
{
hFileMap = nullptr;
}
}

#else

if (data)
{
munmap(data, size);
data = nullptr;
}

if (hFileMap)
{
if (!close(hFileMap))
{
hFileMap = nullptr;
}
}
#endif
}
}

bool SharedMemory::Open()
{
#if defined _WIN32 || defined _WIN64
if ((hFileMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, name.c_str())) == nullptr)
{
return false;
}

if ((data = MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, size)) == nullptr)
{
CloseHandle(hFileMap);
return false;
}
#else

if ((hFileMap = open(MapName.c_str(), O_RDWR | O_CREAT, 438)) == -1)
{
return false;
}

if ((data = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, hFileMap, 0)) == MAP_FAILED)
{
close(hFileMap);
return false;
}
#endif
return true;
}

bool SharedMemory::Create()
{
#if defined _WIN32 || defined _WIN64
if ((hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, size, name.c_str())) == nullptr)
{
return false;
}

if ((data = MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, size)) == nullptr)
{
CloseHandle(hFileMap);
return false;
}

#else

if ((hFileMap = open(MapName.c_str(), O_RDWR | O_CREAT, 438)) == -1)
{
return false;
}

if ((data = mmap(nullptr, Size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, hFileMap, 0)) == MAP_FAILED)
{
close(hFileMap);
return false;
}
#endif
return true;
}

池.hpp:

#ifndef POOLS_HPP_INCLUDED
#define POOLS_HPP_INCLUDED

#include <stdexcept>
#include <cstdint>
#include "SharedMemory.hpp"

template<typename T>
class SharedPool
{
private:
T* data;
SharedMemory* shm;
std::size_t size;

public:
SharedPool(SharedMemory* shm) : data(reinterpret_cast<T*>(shm->GetPointer())), shm(shm), size(shm->GetSize()) {};

template<typename U = T>
void* allocate(std::size_t n, const void* hint = 0) {return &data[0];}

template<typename U = T>
void deallocate(U* ptr, std::size_t n) {}

template<typename U = T>
std::size_t max_size() const {return size;}
};

#endif // POOLS_HPP_INCLUDED

main.cpp(从进程一向共享内存添加值):

#include "SharedMemory.hpp"
#include "Allocators.hpp"
#include "Pools.hpp"

#include <vector>
#include <iostream>

int main()
{
SharedMemory mem = SharedMemory("Local\\Test_Shared_Memory", 1024);
if (!mem.Open() && !mem.Create())
{
throw std::runtime_error("Error Mapping Shared Memory!");
}

auto pool = PoolAllocator<int, SharedPool<int>>(SharedPool<int>(&mem));
std::vector<int, decltype(pool)> v(pool);

int* ptr = reinterpret_cast<int*>(mem.GetPointer());
std::cout<<"Pushing 3 values to: "<<ptr<<"\n";

v.push_back(100);
v.push_back(200);
v.push_back(700);

std::cin.get();
}

main.cpp(从共享内存读取值进程二):

#include "SharedMemory.hpp"
#include "Allocators.hpp"
#include "Pools.hpp"

#include <vector>
#include <iostream>

int main()
{
SharedMemory mem = SharedMemory("Local\\Test_Shared_Memory", 1024);
if (!mem.Open() && !mem.Create())
{
throw std::runtime_error("Error Mapping Shared Memory!");
}

auto pool = PoolAllocator<int, SharedPool<int>>(SharedPool<int>(&mem));
std::vector<int, decltype(pool)> v(pool);

int* ptr = reinterpret_cast<int*>(mem.GetPointer());
std::cout<<"Reading 3 values from: "<<ptr<<"\n";

v.reserve(3);
std::cout<<v[0]<<"\n";
std::cout<<v[1]<<"\n";
std::cout<<v[2]<<"\n";

std::cin.get();
}

关于c++ - 以链表指针为全局变量的多进程c++(11),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21062929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com