0

I understand the semaphore are used to synchronize process for access to a shared memory.

Basically I do not understand how many semaphore set to create?

Suppose, I have only one segment of shared memory, and two process will access that. Then, only one semaphore is created.

But what if I have three or more process that will access to same shared memory, then do it need more semaphore sets?

What is the condition on which number of semaphore set creation depends on?

Thanks in advance.

pmverma
  • 1,653
  • 13
  • 27

3 Answers3

1

If you are using a semctl (IPC semaphore), then you require to create one semaphor. If you are using POSIX semaphores (sem_init), then also one, but only if you pass a true value for the pshared argument on creation and place it in shared memory.

The semaphore can be shared across threads or processes. Cross-process sharing is implemented on the operating system level. Two or more different processes can share the same semaphore even if those processes were not created by forking a single parent process.

Two types of semaphores are sysV and POSIX. find more information regarding defference please got to System V IPC vs POSIX IPC

sysV semaphores are maintained in the kernel so you don't need to allocate space for them or put them in shared memory to share them. But you do need a way for every process using them to find them.

See this example for sharing an unnamed UNIX semaphore between a parent process and its child (to compile with gcc you'll need the -pthread flag):

#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

int main(void)
{
  /* place semaphore in shared memory */
  sem_t *sema = mmap(NULL, sizeof(sema), 
      PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,
      -1, 0);
  if (sema == MAP_FAILED) {
    perror("mmap");
    exit(EXIT_FAILURE);
  }

  /* create/initialize semaphore */
  if ( sem_init(sema, 1, 0) < 0) {
    perror("sem_init");
    exit(EXIT_FAILURE);
  }
  int nloop=10;
  int pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid == 0) { 
    /* child process*/
Community
  • 1
  • 1
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • Do you mean only one semaphore set is needed whether processes which will access shared-memory are two or more? – pmverma Jan 31 '14 at 05:32
  • 1
    `semctl()`? A semaphore set is not a single value, but has a set of values. It is referred to through a semaphore set containing multiple semaphores. Each semaphore set is identified by a semid, which identifies the semaphore set, and a semnum, which identifies the semaphore within the set. Multiple semaphore operations may be specified on one `semop()` call. These operations are atomic on multiple semaphores within a semaphore set. – Jayesh Bhoi Jan 31 '14 at 06:02
0

Take a look at Pearce Wiki http://www.andy-pearce.com/wiki/notes/sharing_pthreads_primitives_between_processes There is a pthreads mutex and condition variable being placed in shared memory instead a semaphore. You can extend this solution more than two process.

niccom
  • 1
0

It does not depends on the number of process accessing the resource. Semaphore is used to avoid simultaneous access to the resource. If you have only one resource initialize the semaphore with one resource. once a process took the resource, other 2 or more process wait for the resource to be freed. Once the resource is freed based on process state/scheduling one of the other process will take the resource.

sequence will be like

init with one resource: sem_init()

Took the resource: sem_wait() or sem_timedwait()

free the resource : sem_post()

gaurav
  • 103
  • 1
  • 2
  • 9