Sharing semaphores

Hi. I am using semaphores but I have been in trouble when I try to share them via shared memory. I need synchronization between processes. Is there any one out there who can help me, please, it would be usefull if any code (as example) is available since QNX help does not have examples about how to do this (share semaphores via shared memory). Thanks a lot. Frang

Maybe you should try named semaphores?

When initialisating a semaphore, you need to tell it that’s it’s a shared semaphore qdn.qnx.com/developers/docs/mome … _init.html

I tried diferent ways in order to use semaphores among proccesses but I was no able to to make them work. I initialized them properly, using a non zero value for the shared parameter), I put them on the shared memory, but when I try to use them from another proccess I get an error and the system call (sem_wait) fails. I need semaphores since access to data in shared memory must be exclusive, and I do not thing we are able to use one byte or something like that because there is not warranty that when one proccess reads that byte then another could be doing the same at the time and both get into a critical region when just one must, I´m using C code . Thanks.

here is an example if it can help you, works in qnx4 and qnx6

but it is just an example, it doesnt contain any checks for library function failure …

#include <semaphore.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>

int main ()
{
	char *path = "tshmem";
	int i, pid, fd, size = sizeof(sem_t);
	void *ptr;
	sem_t *sem;
	
	if ((pid = fork()) == -1)
		return -1;
	
	if (pid)
	{
		fd = shm_open(path, O_RDWR|O_CREAT, 0777);
		ltrunc(fd, size, SEEK_SET);
		ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
		sem = ptr;
		sem_init(sem, 1, 0);

		sleep(5);
		sem_post(sem);

		for (i = 0; i < 5; i++)
		{
			sem_wait(sem);
			printf("%d parent process - inside critical section\n", getpid());
			sem_post(sem);
		}
	}
	else
	{
		sleep(1);
		
		fd = shm_open(path, O_RDWR, 0777);
		ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
		sem = ptr;

		for (i = 0; i < 5; i++)
		{
			sem_wait(sem);
			printf("%d child process - inside critical section\n", getpid());
			sleep(1);
			sem_post(sem);
		}
	}
	
	munmap(ptr, size);
	close(fd);
	shm_unlink(path);
	
	return 0;
}

It is working !!! Thanks a lot.

You can also put pthread mutexes into the shared memory itself as well. The sem_() API is an older POSIX API for inner-process syncronization and it isn’t as fast as the pthread_() API.

I’m using QNX 6.30.

that example no work!!

bash-2.05a# gcc -o semshared semshared.c
bash-2.05a# ./semshared
bus error (core dumped)
bash-2.05a#

help me!!!

That example has no error handling/checking. Add it to every os function call to detect what went wrong.

the code is the same that mezek!

[code]
#include <semaphore.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>

int main ()
{
char *path = “tshmem”;
int i, pid, fd, size = sizeof(sem_t);
void *ptr;
sem_t *sem;

if ((pid = fork()) == -1)
return -1;

if (pid)
{
fd = shm_open(path, O_RDWR|O_CREAT, 0777);
ltrunc(fd, size, SEEK_SET);
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
sem = ptr;
sem_init(sem, 1, 0);

  sleep(5);
  sem_post(sem);

  for (i = 0; i < 5; i++)
  {
     sem_wait(sem);
     printf("%d parent process - inside critical section\n", getpid());
     sem_post(sem);
  }

}
else
{
sleep(1);

  fd = shm_open(path, O_RDWR, 0777);
  ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  sem = ptr;

  for (i = 0; i < 5; i++)
  {
     sem_wait(sem);
     printf("%d child process - inside critical section\n", getpid());
     sleep(1);
     sem_post(sem);
  }

}

munmap(ptr, size);
close(fd);
shm_unlink(path);

return 0;
}[/code]

I did not make any modification .

when use sem_wait or sem_post … then … bus error (core dumped)

As mario said, there is no error checking. What if shm_open() fails for instance? Add some error handling, and you’ll find out what is going on.

With semaphores, you don’t have priority inheritance. You should mutexes. And of course you need to open the shared memory in the other process before you can acess the semaphore/mutex. That is the only problem I can think of that could cause the error scenario you describe.

replace the ltrunc() function with shm_ctl(fd, SHMCTL_ANON | SHMCTL_GLOBAL , 0, size) and the example works…