I am trying to communicate between two processes using a shared memory. In both of them, I create/access to the shared memory using:
int fd = shm_open(this->shm_name, O_RDWR | O_CREAT, 0666);
ftruncate(fd, this->shm_size);
void *p_shm = mmap((void *)0, (size_t)this->shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
I write to it in one process, but the other one always reads 0. I tried writing both reader/writer in the same process but in different threads and it worked fine. I also verified that they both connect(?) to the same shared memory by opening the shm in the first process, resizing it; opening it in the second process and confirming that the shm size matched the resizing done by the first process. I also tried msync’ing after i write to the shm but it also didn’t work. What am I doing wrong? Thanks.
user26516284 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.