hey folks,
i was trying to use memory based semaphores in shared memory to do some kind of synchronization between two (or more) processes when i ran into a core dump.
I simplified my program where one process does this (create the shm and initialize the semaphore):
things are fine till here, i can use &ptr->mutex in sem_*() calls.
and the other process does this:
here if i try to use &ptr->mutex in any of sem_*() calls i get EINVAL (invalid semaphore)
am i doing anything wrong here ?
i was trying to use memory based semaphores in shared memory to do some kind of synchronization between two (or more) processes when i ran into a core dump.
I simplified my program where one process does this (create the shm and initialize the semaphore):
Code:
// this is a header file used by both progs
typedef struct {
int cnt;
sem_t mutex;
} shmd;
Code:
shmd *ptr;
size = sizeof(shmd);
fd = shm_open(path, O_CREAT | O_RDWR, mode);
ftruncate(fd, size);
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
sem_init(&ptr->mutex, 1, 1); // pshared = 1, value = 1
things are fine till here, i can use &ptr->mutex in sem_*() calls.
and the other process does this:
Code:
shmd *ptr;
size = sizeof(shmd);
fd = shm_open(path, O_RDWR, mode);
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
here if i try to use &ptr->mutex in any of sem_*() calls i get EINVAL (invalid semaphore)
am i doing anything wrong here ?