Wow*_*owy 0 c linux memory-management
我目前正在使用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", O_CREAT | O_RDWR, S_IRWXU);
if(fd == -1)
{
perror("shm_open");
return 1;
}
ftruncate(fd, sizeof(count));
memory = mmap(NULL, sizeof(count), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
printf("before %d\n", memory->counter);
memory->counter = 5;
printf("after %d\n", memory->counter);
if(msync(memory, sizeof(count),MS_SYNC)<0)
{
printf("%s","msync ERROR.");
}
else { printf("%s","msync completed successfully.");}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
与shmget和shmat的结果相同,重新启动后数据为0.(printf"before"show 0)
编辑2:
这样做对我来说:
count *memory;
int fd = open(MYmemoryFile, O_CREAT | O_RDWR, S_IRWXU);
ftruncate(fd, sizeof(count));
memory = mmap(NULL, sizeof(count), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
printf("before %d\n", memory->counter);
memory->counter = 5;
printf("after %d\n", memory->counter);
if(msync(memory, sizeof(count),MS_SYNC)<0)
{
printf("%s","msync ERROR.");
}
else { printf("%s","msync completed successfully.\n");}
Run Code Online (Sandbox Code Playgroud)
仍然不完美,因为在我之前的代码中,我使用的是没有指针形式的内存(Zone [0] .param现在是Zone [0] - > param例如)但这是向前迈出的一步,感谢@Wumpus Q.Wumbley .
编辑3:如果有人稍后搜索它,这是我如何修复它以使用结构:
#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
const char *MYmemoryFile = "/home/memorystruct.txt";
typedef struct count
{
int counter;
int test;
} match[5];
int main()
{
int fd = open(MYmemoryFile, O_CREAT | O_RDWR, S_IRWXU);
ftruncate(fd, sizeof(match));
struct count *memory = mmap(NULL, sizeof(match), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
printf("before memory[0].counter %d\n", memory[0].counter);
printf("before memory[0].counter %d\n", memory[0].test);
printf("before memory[1].counter %d\n", memory[1].counter);
memory[0].counter = 5;
memory[0].test = 2;
memory[1].counter = 17;
printf("after memory[0].counter %d\n", memory[5].counter);
printf("after memory[1].counter %d\n", memory[1].counter);
if(msync(memory, sizeof(match),MS_SYNC)<0)
{
printf("%s","msync ERROR.");
}
else
{
printf("%s","msync completed successfully.\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)