如何在属于同一父级的两个子进程之间共享内存

时间:2022-11-14 17:03:38

I've two child processes (both having same parent) launched with exec() system call and i want these two processes to map to the same file for IPC through mmap(). The problem i'm having is that one process writes the data (it's own pid) using the pointer returned by mmap() but the other process isn't able to read that data. Also i want the second child process to use that pid to know the state of first child process. Any help would be much appreciated as i'm fairly new to this.

我用exec()系统调用启动了两个子进程(都有相同的父进程),我希望这两个进程通过mmap()映射到IPC的同一文件。我遇到的问题是,一个进程使用mmap()返回的指针写入数据(它自己的pid),但另一个进程无法读取该数据。此外,我希望第二个子进程使用该pid来了解第一个子进程的状态。任何帮助都会非常感激,因为我对此很新。

The Parent Process:

父流程:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(int argc, char **argv)
{ 
pid_t process, f_child, s_child;
int status;
sem_t synch; 
sem_init(&synch, 1, 0);
process = fork();
if(process<0)
{
perror("Fork Failed");
exit(1);
}
if(process>)
{
//Parent!!
sem_post(&synch); // signaling to child
f_child = wait(&status);
s_child = fork();
if(s_child==0)
{
//Second Child Process!!
execlp("./secondChild", "./secondChild", NULL);
}
}
else
{
//First Child Process!!
sem_wait(&synch);
execlp("./firstChild","./firstChild", NULL);
}
 return 0;
}

First Child Process:

第一个孩子过程:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd = shm_open("./myFile", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
int *sharedMem = mmap(0, 256, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, fd, 0);


*sharedMem = getpid();
printf("Child Process 1 wrote message : %d", *sharedMem);
exit(10);
return 0;
}

Second Child Process:

第二个子进程:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd = shm_open("./myFile", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
int *sharedMem = mmap(0, 256, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, fd, 0);


printf("Child Process 2 readmessage : %d", *sharedMem);
return 0;
}

2 个解决方案

#1


1  

MAP_ANONYMOUS requests anonymous memory, that is memory that is not independent of any file and the fd argument to mmap is ignored. Remove it.

MAP_ANONYMOUS请求匿名内存,即不依赖于任何文件的内存,并忽略mmap的fd参数。去掉它。

#2


0  

If you man sem_init on linux ( at least on mine ) it says that the sem_t has to be in a shared memory location; the stack of the parent process is not a shared memory area. The manual is a bit ambiguous in this regard, as it goes on to say that a forked child inherits these mappings, but I am pretty sure that it means that a forked child inherits shared mappings.

如果你在Linux上使用sem_init(至少在我的手机上)它会说sem_t必须位于共享内存位置;父进程的堆栈不是共享内存区域。手册在这方面有点含糊不清,因为它继续说分叉子继承这些映射,但我很确定这意味着分叉子继承共享映射。

#1


1  

MAP_ANONYMOUS requests anonymous memory, that is memory that is not independent of any file and the fd argument to mmap is ignored. Remove it.

MAP_ANONYMOUS请求匿名内存,即不依赖于任何文件的内存,并忽略mmap的fd参数。去掉它。

#2


0  

If you man sem_init on linux ( at least on mine ) it says that the sem_t has to be in a shared memory location; the stack of the parent process is not a shared memory area. The manual is a bit ambiguous in this regard, as it goes on to say that a forked child inherits these mappings, but I am pretty sure that it means that a forked child inherits shared mappings.

如果你在Linux上使用sem_init(至少在我的手机上)它会说sem_t必须位于共享内存位置;父进程的堆栈不是共享内存区域。手册在这方面有点含糊不清,因为它继续说分叉子继承这些映射,但我很确定这意味着分叉子继承共享映射。