c - 测试共享内存,发生奇怪的事情

标签 c linux shared-memory

我有 2 个在 4.1.2 中编译的程序在 RedHat 5.5 中运行, 测试共享内存是一项简单的工作,shmem1.c 如下所示:

#define STATE_FILE "/program.shared"
#define  NAMESIZE 1024
#define   MAXNAMES 100
typedef struct
{
    char name[MAXNAMES][NAMESIZE];
    int heartbeat ;
    int iFlag ;
}  SHARED_VAR;

int main (void)
{
    int first = 0;
    int shm_fd;
    static SHARED_VAR *conf;

    if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_EXCL | O_RDWR),
                   (S_IREAD | S_IWRITE))) > 0 ) {
        first = 1; /* We are the first instance */
    }
    else if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_RDWR),
                    (S_IREAD | S_IWRITE))) < 0) {
        printf("Could not create shm object. %s\n", strerror(errno));
        return errno;
    }
    if((conf =  mmap(0, sizeof(SHARED_VAR), (PROT_READ | PROT_WRITE),
               MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {

        return errno;
    }
    if(first) {
        for(idx=0;idx< 1000000000;idx++)
        {
            conf->heartbeat = conf->heartbeat + 1 ;
        }
    }
    printf("conf->heartbeat=(%d)\n",conf->heartbeat) ;
    close(shm_fd);
    shm_unlink(STATE_FILE);
    exit(0);
}//main

shmem2.c 如下:

#define STATE_FILE "/program.shared"
#define  NAMESIZE 1024
#define   MAXNAMES 100

typedef struct
{
    char name[MAXNAMES][NAMESIZE];
    int heartbeat ;
    int iFlag  ;
}  SHARED_VAR;

int main (void)
{
    int first = 0;
    int shm_fd;
    static SHARED_VAR *conf;

    if((shm_fd = shm_open(STATE_FILE, (O_RDWR),
                    (S_IREAD | S_IWRITE))) < 0) {
        printf("Could not create shm object. %s\n", strerror(errno));
        return errno;
    }
    ftruncate(shm_fd, sizeof(SHARED_VAR));
    if((conf =  mmap(0, sizeof(SHARED_VAR), (PROT_READ | PROT_WRITE),
               MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
        return errno;
    }
    int idx ;
    for(idx=0;idx< 1000000000;idx++)
    {
        conf->heartbeat = conf->heartbeat + 1 ;
    }
    printf("conf->heartbeat=(%d)\n",conf->heartbeat) ;
    close(shm_fd);
    exit(0);
}

编译后:

   gcc shmem1.c -lpthread -lrt -o shmem1.exe
   gcc shmem2.c -lpthread -lrt -o shmem2.exe

并且使用 2 个终端几乎同时运行两个程序:

   [test]$ ./shmem1.exe
   First creation of the shm. Setting up default values
   conf->heartbeat=(840825951)
   [test]$ ./shmem2.exe
   conf->heartbeat=(1215083817)

我感到很困惑!既然 shmem1.c 是一个循环 1,000,000,000 次,那怎么可能 可能有像 840,825,951 这样的答案吗?

我这样运行shmem1.exe和shmem2.exe,大部分结果都是conf->heartbeat 将大于 1,000,000,000,但很少且随机, 我将看到结果 conf->heartbeat 将小于 1,000,000,000 ,
在 shmem1.exe 或 shmem2.exe 中!!

如果只运行shmem1.exe,它总是打印1,000,000,000,我的问题是, shmem1.exe 中conf->heartbeat=(840825951) 的原因是什么?

更新:虽然不太确定,但是我想我明白是怎么回事了,如果shmem1.exe运行10次为例,那么conf->heartbeat = 10,此时shmem1.exe休息一下然后后面,shmem1.exe从共享内存中读取conf->heartbeat = 8,所以shmem1.exe将从8继续,为什么conf->heartbeat = 8?我认为这是因为 shmem2.exe 将共享内存数据更新为 8 ,shmem1.exe 在休息之前没有将 10 写入共享内存......这只是我的理论......我不知道如何证明这一点!!

最佳答案

您返回的值表明您没有以原子方式递增共享内存。以下循环:

int idx ;
for(idx=0;idx< 1000000000;idx++)
{
    conf->heartbeat = conf->heartbeat + 1 ;
}

归结为:

int idx ;
for(idx=0;idx< 1000000000;idx++)
{
    // read
    int heartbeat= conf->heartbeat;

    // write
    conf->heartbeat = heartbeat + 1 ;
}

在读写注释之间,可以交换一个进程以让另一个进程运行。如果 shmem1.exe 和 shmem2.exe 都在运行,则意味着您可以让 shmem1.exe 在 shmem2.exe 读取和写入 conf->heartbeat 之间多次递增 conf->heartbeat ,反之亦然。

如果您想要一致的更新,您需要使用平台的原子内存增量函数。这保证了读取/修改/写入操作始终会导致值递增,而不是可能写回过时的值。

例如,如果 shmem1.exe 和 shmem2.exe 之间没有任何同步,您可能会遇到 shmem1.exe 和 shmem2.exe 都输出 2 的病态情况:

shmem1.exe: read 0
shmem2.exe: read 0
// shmemem2.exe goes to sleep for a loooong time
shmem1.exe: write 1
// ... shmem1.exe keeps running
shmem1.exe: write 999,999,999
// shmem2.exe wakes up
shmem2.exe write 1
shmem2.exe read 1
// shmem2.exe goes back to sleep
shmem1.exe read 1(!)
// shmem1.exe goes to sleep
// shmem2.exe wakes up
shmem2.exe write 2
shmem2.exe read 2
shmem2.exe write 3
// shmem2.exe continues, shmem1.exe stays asleep
shmem2.exe read 999,999,999
shmem2.exe write 1,000,000,000
// shmem2.exe goes to sleep, shmem1.exe wakes up
shmem1.exe write 2(!)
shmem1.exe read 2
shmem1.exe print 2
//shmem2.exe wakes up
shmem2.exe read 2
shmem2.exe print 2

无需 CPU 重新排序,只需疯狂调度即可发生这种情况。

关于c - 测试共享内存,发生奇怪的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13832302/

相关文章:

linux - 如果使用 appimage 安装了 neovim,如何使用 nvim 命令?

linux - spi_driver.id_table 和 spi_driver.driver.of_match_table 的区别

c - 使用共享内存和信号量实现管道

c - 程序在执行期间改变变量值

c++ - 我怎么知道我的数组在缓存中?

C 编程、unicode 和 linux 终端

c++ - 跨进程的 POSIX 共享内存同步 C++/C++11

时间:2019-03-17 标签:c++boosticlcontainersinsharedmemory

c - 如何读取和输出带有空格和换行符的输入

c - 我如何获得 linux 内核服务中断所花费的时间