gpt4 book ai didi

c - 当我们从不同的上下文调用 msync 时,文件内容不持久

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:04 26 4
gpt4 key购买 nike

我有两个上下文,即 process1 和 process2 共享相同的映射区域,process1 大多数时候将数据同步到文件,而 process2 只在系统重新启动之前同步一次以确保所有数据都已同步。我发现在系统重新启动之前文件中的数据是完整的,一旦系统在重新启动后出现,我发现单独由 process2 同步的数据丢失了

注意:

  1. mmap 标志:两个进程共享 map_shared。
  2. msync 标志:两个进程的 ms_sync。
  3. 没有 flock 问题 b/w 进程。
  4. 检查所有进程的错误代码。

抱歉,我无法分享完整的代码,但我已经粘贴了其中的一小部分,我认为应该这样做。

**/*Process1(Daemon process)
* This process does syncing on two files int_fd and ext_fd. It stacks upto 50 messages and * then does a sync for every 50 messages.If the system goes for reboot within these 50 * messages then process2 does syncing before going for reboot.
*/**
int_cntr -> file1 data counter(when int_cntr==50 does syncing for file1)
ext_cntr -> file2 data counter(when ext_cntr==50 does syncing for file2)
my_flg -> this flag will be set only when any counter reaches 50.
int_fd-> file1 to be synced
ext_fd-> file2 to be synced

**/*This is a daemon process so mapping is done only once during booting of this process it is done exactly as shown in process2*/**

**/*Counter value check*/**
**/*checks counter values int_cntr and ext_cntr and sets flag "**my_flg**"only when any of the counter reaches 50,.*/**

**/*Syncing part*/**
-> checks the **my_flg** if it is set,then files will be locked.
-> Uses locks/Unlocks with same flags mentioned in process2.
-> Call msync with MS_SYNC,exact flags are used as shown in process2
-> reset the corresponding counter
-> if the my_flg is not set then data will be added to the queue syncing will not be done.



**/*Process2
* This process will be called when system goes for reboot,before going for reboot it syncs * the data to corresponding files.
*/**
int_fd-> file1 to be synced
ext_fd-> file2 to be synced

ha_int_file = mmap(0, sizeof (file1), PROT_READ | PROT_WRITE,
MAP_SHARED, int_fd, 0);
ha_ext_file = mmap(0, sizeof (file2), PROT_READ | PROT_WRITE,
MAP_SHARED, ext_fd, 0);

if(ha_int_file== MAP_FAILED || ha_ext_file==MAP_FAILED)
printf("Process2 mmap failed,errno =%d\n",errno);
/*
* Take the LOCK for syncing file1
*/
if ((flock_rc = flock(int_fd, LOCK_EX)) != 0) {
printf("Forcesync Internal file lock failed##############\n");
rc = ERROR;
} else {
if(msync(ha_int_file, sizeof (rls_storage_file_t), MS_SYNC | MS_INVALIDATE)) {
printf("Forcesync Internal file sync failed,errno =%d##############\n",errno);
rc = ERROR;
} else
printf("Force sync successful for internal file#####################\n");
/*
* Release the lock on file1
*/
if ((flock_rc = flock(int_fd, LOCK_UN)) != 0) {
printf("Forcesync Internal file un-lock failed##############\n");
rc = ERROR;
}

}

/*
* Take the LOCK for syncing file2
*/
if ((flock_rc = flock(ext_fd, LOCK_EX)) != 0) {
printf("Forcesync External file lock failed##############\n");
rc = ERROR;
} else {
if(msync(ha_ext_file, sizeof (rls_storage_file_t), MS_SYNC | MS_INVALIDATE)) {
printf("Forcesync External file sync failed=%d##############\n",errno);
rc = ERROR;
} else
printf("Force sync successful for external file#####################\n");
/*
* Release the LOCK on file2
*/
if ((flock_rc = flock(ext_fd, LOCK_UN)) != 0) {
printf("Forcesync External file un-lock failed##############\n");
rc= ERROR;
}
}

close(int_fd);
close(ext_fd);

问题摘要:

假设我收到了 255 条消息,每 50 条消息同步将由 process1 完成,因此 250 条消息由 process1 同步,现在其余消息(5 条消息)将由 process1 添加到队列中。假设如果系统现在重新启动,Process2 会将这 5 条消息同步到相应的文件。在由 process2 同步后,我在系统重新启动之前获取了文件的副本。我发现重启前我获取的副本中的数据完好无损,而在系统启动后,我发现单独由 process2 同步的数据丢失了。根据我们的示例,进程 2 单独同步的最后 5 条消息丢失了。

注意:当我在重新启动之前从同一进程(即进程 1)同步时,我没有看到问题,只有当我在重新启动之前从进程 2 同步时,我才看到它。

有人可以向我介绍这个 MS_INVALIDATE 标志吗,在这种情况下它能做什么。

最佳答案

不是答案只是一些想法。通常,如果您要共享内存,您将首先共享内存,然后将所述内存映射到文件。

只要您通过某种合法的共享内存机制(如 shmat/shmget)获取共享内存,就可以在进程之间共享内存。即使您将使用共享内存,您也可能希望使用提供内存屏障/检查点的线程原语与该内存进行交互。

如果您不能使用线程原语,您可以使用 __sync_synchronize() 等 gcc 原语在您的代码中手动创建内存屏障。 Visual Studio 和其他编译器可能类似。

一旦您确信两个进程之间的内存是一致的,无论哪个进程想要执行最后的同步步骤都应该调用 msync、munmap,然后退出。

关于c - 当我们从不同的上下文调用 msync 时,文件内容不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25513918/

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