小编Wow*_*owy的帖子

共享内存的std :: string给出了分段错误(linux)

我目前正在尝试在Linux上的2个进程之间的共享内存中放置结构.我没有问题共享bool或int但是当尝试共享字符串时,std :: string或char我有一个分段错误错误.

现在我的代码是:

#include <iostream>
#include <sys/types.h> //shmat
#include <sys/shm.h>
#include <sys/stat.h> //open
#include <fcntl.h>
#include <unistd.h> //close

using namespace std;

struct Prises{

int numero;
int transactionId;
bool reservation;
bool charge;
bool disponibilite;
bool defaut;
bool verrouillage;
bool trappe;
int LEDverte;
int LEDrouge;
std::string carte;
std::string etat;

};

int main()
{
const char *keyFile = "/tmp/key.dat";
/* Make sure the file exists. */
int descriptor = open(keyFile, O_CREAT | O_RDWR, S_IRWXU);

/* Only wanted to make sure that the …
Run Code Online (Sandbox Code Playgroud)

c++ linux stdstring shared-memory

1
推荐指数
1
解决办法
426
查看次数

在重启的情况下保持共享内存数据?

我目前正在使用shmget和shmat在两个进程之间创建共享内存.当进程死亡时,共享内存仍处于活动状态并重新启动进程意味着我们可以从以前开始.但如果机器关闭然后打开,我们就会丢失数据.

我想知道在shmget/shmat或其他方法中是否有一个选项可以在进程之间创建共享内存,以便即使在重新启动的情况下也能保持数据存活.

现在我正在做这样的事情:

const char *ZoneFile = "/home/Zone.dat";'
key_t sharedKeyZone;
int sharedSpaceIdZone;

int descriptor = open(ZoneFile, O_CREAT | O_RDWR, S_IRWXU);
close(descriptor);
sharedKeyZone = ftok(ZoneFile, 1);
sharedSpaceIdZone = shmget(sharedKeyZone, 1 * sizeof(Zone_t), IPC_CREAT);
ZoneArray = (Zone_t *) shmat(sharedSpaceIdZone, NULL, 0);
Run Code Online (Sandbox Code Playgroud)

Zone_t是一种结构类型,我可以毫无问题地从我的2进程访问ZoneArray [0]中的每个数据.

现在,我能想到的唯一解决方案是定期编写带有数据的ini文件以"保存"系统状态,并在重新启动时读取此文件,但如果结构必须在以后发展,则根本不灵活.

编辑:遵循@Wumpus Q.Wumbley的想法,我尝试使用msync这样的方式:

#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>

//add rt to eclipse librairies

typedef struct count {

    int counter;
} count;

int main()
{
    count *memory;
    int fd = shm_open("MYmemory.txt", …
Run Code Online (Sandbox Code Playgroud)

c linux memory-management

0
推荐指数
1
解决办法
847
查看次数

标签 统计

linux ×2

c ×1

c++ ×1

memory-management ×1

shared-memory ×1

stdstring ×1