3

I am trying to create a shared memory between two processes using mmap(). The child process is written in a sperate c file, and executed using execve().

Q1: Is there any way to share the shared memory address int *shmem with the child process other than the pipe?

Q2: I successfully could get the shared memory address on the child process but, I cannot access the data "array of int" stored by the parent process in the shared memory segment, is it related to some restrictions on mmap() or what?

Hazem Alabiad
  • 1,032
  • 1
  • 11
  • 24
  • So, how to reach the data on the shared memory then? – Hazem Alabiad May 07 '18 at 22:21
  • 2
    The parent process must mmap with MAP_SHARED, and the child process has to mmap the same file. The pointer will be different, but the mapping is shared between the processes. – FBergo May 07 '18 at 22:23
  • @FBergo How to do so? Can you share a code for that? – Hazem Alabiad May 07 '18 at 22:30
  • 1
    Maybe you should show the code that isn't working. – user253751 May 07 '18 at 23:15
  • Possible duplicate of [How to use shared memory with Linux in C](https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c) –  May 07 '18 at 23:27
  • @immibis That example is using the shared address within the main process file in my case, I wanna use the shared memory address that was created by the parent process in the child process c file. – Hazem Alabiad May 08 '18 at 07:19
  • After the fork has been done but before the child calls `execve`, it has the same variable environment as the parent. If the shared memory identifier is in scope then, it can simply use it and pass it on to the called program as one of its arguments. – Arndt Jonasson May 08 '18 at 14:34

0 Answers0