When I do this :
myProgram.h myProgram.c struct PipeShm { // all my fields // more // ... }; struct PipeShm myPipe = { /* initialization for all fields */ }; struct PipeShm * sharedPipe = &myPipe; void func() { sharedPipe = mmap (NULL, sizeof * sharedPipe, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0); } When I mmap the pointer sharedPipe, if I invoke from main() any methods from myProgram code, would all processes share the exact shared memory that I shared with myPipe struct?
Or would each new child that's created, would have a new myPipe of his own?
Regards
EDIT:
This is after I read the comments & answers : now changes were made , and I initialize the values of the segment only after I allocate it :
#include "my_pipe.h" struct PipeShm * sharedPipe = NULL; int shm_pipe_init() { if (!sharedPipe) { int myFd = shm_open ("/myregion", O_CREAT | O_TRUNC | O_RDWR, 0600); if (myFd == -1) error_out ("shm_open"); // Allocate some memory in the region - We use ftruncate, write(2) would work just as well int retAlloc = ftruncate (myFd, sizeof * sharedPipe); if (retAlloc < 0) error_out("ftruncate"); sharedPipe = mmap (NULL, sizeof * sharedPipe, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, myFd, 0); if (!sem_init (&sharedPipe->semaphore, 1, 0)) { sharedPipe->init = TRUE; sharedPipe->flag = FALSE; sharedPipe->ptr1 = NULL; sharedPipe->ptr2 = NULL; sharedPipe->status1 = -10; sharedPipe->status2 = -10; sharedPipe->semaphoreFlag = FALSE; sharedPipe->currentPipeIndex = 0; } else perror ("shm_pipe_init"); } return 1; // always successful } The problem however continues ,the shared memory seems to be not so shared between the processes , since while running and forking that main :
int main() { int spd, pid, rb; char buff[4096]; fork(); shm_pipe_init(); // more return 0; } I still get outputs , that simulates the behavior like only one process is running (instead of multiple outputs , I get only a single one or a couple ,depends on a race condition between the processes) .