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

时间:2014-08-26 19:39:08

标签: c linux memory

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

注意:

  1. mmap flag:两个进程的map_shared。
  2. msync标志:两个进程的ms_sync。
  3. b / w流程没有鸡群问题。
  4. 检查所有进程的错误代码。
  5. 很抱歉,我无法分享完整的代码,但我已经粘贴了其中的一小部分,我认为应该这样做。

    **/*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条消息同步到相应的文件。在进程2同步后,我在系统进行重启之前获取该文件的副本。我发现数据在重启之前的副本中是完整的,并且在系统启动后我发现由process2单独同步的数据丢失。根据我们的示例,仅丢失了由进程2同步的最后5条消息。

    注意: 当我在重新启动之前从同一个进程同步,即process1时,我没有看到这个问题,只有当我在重新启动之前从process2同步时才能看到它。

    有人可以告诉我这个 MS_INVALIDATE 标志,在这种情况下它能做什么。

1 个答案:

答案 0 :(得分:0)

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

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

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

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