c - 共享内存不更新

标签 c exec mmap

所以我一直在努力学习更多关于共享内存的知识。我正在我的主人中创建一个共享内存结构并 fork child 。然后每个 child 都使用 execv 创建一个新进程。当我尝试从新进程更改共享内存的值时,父进程看不到该更改。我也期待 parent 与 child 交错执行。例如

Child 1 exec 
parent 
child 2 exec 
parent 
child 3 exec
parent 

但是我得到了

child 1 exec 
child 2 exec 
child 3 exec 
parent
parent
parent

这是我的代码

模拟.c

# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <time.h>
# include <stdlib.h>
# include <dirent.h>
# include <stdio.h>
# include <string.h>
# include <getopt.h>
# include <stdbool.h>
# include <ctype.h>
# include <sys/wait.h>
# include <signal.h>
# include <sys/mman.h>
# include <sys/time.h>
# include <stdint.h>
# include <fcntl.h>
# include <sys/shm.h>
struct sharedregion{
    volatile int seconds;
    volatile int sharedmsg;
};
struct sharedregion *mystruct;
void spawnchild()
{
    int fd = shm_open("sharedmemory", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRWXG | S_IRWXU);
    if (fd == -1)
        perror("Error in opening");
    if (ftruncate(fd, sizeof(struct sharedregion)) == -1)
        perror("Unable to truncate");
    mystruct = mmap(NULL, sizeof(struct sharedregion),PROT_READ | PROT_WRITE, MAP_SHARED| MAP_ANONYMOUS, fd, 0);
    if (mystruct == MAP_FAILED)
        perror("mapping failed");
    mystruct->seconds=0;
    mystruct->sharedmsg=0;
    int i;
    for(i=0;i<4;i++)
    {
        pid_t pID = fork();

        if (pID == 0)
        {
            static char *args[]={"./child",NULL};

            execv(args[0], args);
            perror("failed to execv");
            exit(EXIT_FAILURE);

        }
        else if (pID < 0)            // failed to fork
        {
            perror(" Failed to fork:");
            exit(EXIT_FAILURE);
        }
        else if(pID>0)
        {

            printf("\nfrom parent= %d",mystruct->sharedmsg);
            mystruct->seconds=mystruct->seconds+1;
            if(mystruct->seconds==1000000000)
            {
                mystruct->seconds=1;
            }
        }
    }
}
int main(int argc, char **argv)
{
    spawnchild();
}

child.c

# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <time.h>
# include <stdlib.h>
# include <dirent.h>
# include <stdio.h>
# include <string.h>
# include <getopt.h>
# include <stdbool.h>
# include <ctype.h>
# include <sys/wait.h>
# include <signal.h>
# include <sys/mman.h>
# include <sys/time.h>
# include <stdint.h>
# include <fcntl.h>
# include <sys/shm.h>
struct sharedregion{
    volatile int seconds;
        volatile int sharedmsg;
};
struct sharedregion *mystruct;

int main(int argc, char **argv)
{
    int memFd = shm_open("sharedmemory", O_RDWR, S_IRUSR | S_IWUSR | S_IRWXG | S_IRWXU);
    if (memFd == -1)
    {
        perror("Can't open file");
        return 1;
    }

    mystruct = mmap(NULL, sizeof (struct sharedregion), PROT_READ | PROT_WRITE, MAP_SHARED| MAP_ANONYMOUS, memFd, 0);

    printf("\nhello from exec");
    mystruct->sharedmsg=8;
    printf("buffer %d",mystruct->sharedmsg);

    return 0;
}

我的输出

hello from execbuffer 8
hello from execbuffer 8
hello from execbuffer 8
hello from execbuffer 8
hello from execbuffer 8
from parent= 0
from parent= 0
from parent= 0
from parent= 0
from parent= 0

最佳答案

移除标志 MAP_ANONYMOUS

根据手册:

MAP_ANONYMOUS [...] The fd argument is ignored;

因此它会忽略你的共享内存

关于c - 共享内存不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223001/

相关文章:

c - 结构数组的参数类型不兼容

c - 在 C x86 错误中反转字符串

c - 为什么带有双指针的链表会导致错误?

java - 在 Java 中实现进程之间的等待?

c - 使用 mmap 在 C 中共享内存中的结构

linux - 正确释放和取消映射内核内存

c - 使用 malloc 和 realloc 动态地将字符串添加到 char 指针

docker exec 无法在 docker-compose 容器中工作

linux - 如何等待进程子进程?

c++ - mmap 只适用于小文件?