10

My task is to create two different C files and then use the semaphores for process synchronization (I run both C files simultaneously).

My main concern is: if I want to access the semaphores in both the processes (executables of the C files), I need to create the semaphores in shared memory. Also I need to create binary semaphores.

As it is my first program, can someone suggest how to get started on this?

I am able to create and use shared memory, used semaphores within the threads. I watched some lectures on YouTube also but could not find a proper solution.

psmears
  • 26,070
  • 4
  • 40
  • 48
Abhishek Gangwar
  • 1,697
  • 3
  • 17
  • 29
  • What operating system are you using? If, like Linux, it has semaphores that can be used between processes, just use that. – Scott Hunter Aug 22 '15 at 07:50
  • I am doing this on UBUNTU – Abhishek Gangwar Aug 22 '15 at 07:56
  • @AbhishekGangwar - Are you asking, speicfically about how you let thesecond process know, what is the ID of the shared memory? – Amrith Krishna Aug 22 '15 at 12:22
  • I am able to access the named counting semaphores in between the processes by their names. But to make a binary semaphore we use Mutex and mutex is not shared between the processes so i am searching for a method to restrict the counting semaphore to binary semaphore – Abhishek Gangwar Aug 22 '15 at 12:40

2 Answers2

9

Cross-process semaphores are an operating system specific operation.

What most of these share is that you create the semaphore in one process via a virtual path which dubs as the semaphore's name. If permissions are correctly set, you can the open the semaphore in another process by using the same virtual path. These virtual paths aren't usually real filesystem paths even if they look familiar.

On POSIX/System V-based systems, you typically have two options. The differences between the two options are very well explained in this answer.

System V semaphores

These are path-based semaphores that can be obtained with semget():

#include <sys/types.h>
#include <sys/ipc.h>    
#include <sys/sem.h>

int sem;
int sem_id = 1;
key_t key;

key = ftok("/virtualpathtosemaphore", 1);
// create a new semaphore
sem = semget(key, 1, IPC_CREAT);
// use sem = semget(key, 1, 0); to attach to an existing semaphore
// flags also contain access rights, to take care to set them appropriately

// increment semaphore
struct sembuf semopinc = {
  .sem_num = 0,
  .sem_op = 1,
  .sem_flg = 0
};    
semop(sem, &semopinc, 1);

/* decrement semaphore, may block */
struct sembuf semopdec = {
  .sem_num = 0,
  .sem_op = -1,
  .sem_flg = 0
};   
semop(sem, &semopdec, 1);

Note that it is important to cleanup the semaphores, as System V semaphores stay around until explicitly unlinked. Which is kind of a problem when a process crashes without cleaning up its semaphores (e.g. FreeBSD comes with a utility ipcrm do remove dangling System V IPC objects).

POSIX Semaphores

These are actually less widely implemented, so check if your kernel supports them. The named version of these is are obtained via sem_open().

#include <semaphore.h>

sem_t *sem;
sem = sem_open("/nameofsemaphore", O_CREAT, permissions, 0);
// use sem = sem_open("/nameofsemaphore", 0) to open an existing semaphore

/* increment semaphore */
sem_post(sem);

/* decrement semaphore */
sem_wait(sem);

Edit: Named POSIX semaphores need to be destroyed and unlinked using sem_destroy() and sem_unlink(). Unnamed semaphores (obtained from sem_init() get destroyed on last close. Note that it is save to unlink an open semaphore, it will get destroyed on last close.

Windows

Windows has its own semaphore APIs: Semaphores are created by CreateSemaphore().

Windows uses the same naming trick as POSIX but with different namespace conventions.

HANDLE hSem;
hSem = CreateSemaphore(NULL, 0, LONG_MAX, _T("Local\\PathToMySemaphore");

// Use OpenSemaphore() to attach to an existing semaphore

// increment semaphore:
ReleaseSemaphore(hSem, 1, NULL);

// decrement semaphore
WaitForSingleObject(hSem, 0);

Don't forget to add error checking when adapting the examples above. Also note that I have deliberately ignored permissions to simplify the code. Don't forget to add the relevant flags.

In addition to all this you can also (as commonly done before the arrival of real semaphores) abuse file locks as a form of binary mutex.

dhke
  • 15,008
  • 2
  • 39
  • 56
  • > POSIX semaphores are implicitly destroyed when the last process having a handle to the semaphore exits. This is not true, they do survive process exits. There is an explicit sem_unlink() to destroy a POSIX semaphore much like IPC_RMID in SysV. – Sergei Kulik Dec 15 '22 at 09:00
6

You said that you're using Ubuntu GNU/Linux, so...

Use named semaphores!

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

// On first process (the one that creates the semaphore)

char semaphoreName[1 + 6 + 1];
semaphoreName[0] = '/';
semaphoreName[1 + snprintf(&semaphore[1], 6 + 1, "%d", getpid())] = '/0';

sem_t *sem = sem_open(semaphoreName, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0);

// On second process

sem_t *sem = sem_open(semaphoreName, O_RDWR);
3442
  • 8,248
  • 2
  • 19
  • 41
  • Named semaphores works Thanks Can you give me some help to make binary semaphore which can be used among the different processes as well – Abhishek Gangwar Aug 22 '15 at 13:06
  • Do you mean unnamed semaphores? Of course, you just need to declare a `sem_t` **inside** of the shared-memory area, then do `sem_init(&theSemaphoreInSharedMemory, 1, 0)`. The second argument `1` is *very* important (read [sem_init(3)](http://linux.die.net/man/3/sem_init)) – 3442 Aug 23 '15 at 00:58
  • How to declare an unnamed semaphore in shared memory area. As well as how to create strict binary semaphores from counting semaphores – Abhishek Gangwar Aug 23 '15 at 03:55
  • Well, i did already described you above how to do it. Edit: sorry, didn't read your edit at time, I'll post another comment in a while. – 3442 Aug 23 '15 at 03:57
  • To declare a `sem_t` inside a shared memory segment, do this: `/* shmPtr is a pointer to the base of your shared memory area */ sem_t *sem = (sem_t*)shmPtr; void *usableSharedMemory = (char*)shmPtr + sizeof(sem_t);`. With respect to binary semaphores, they're not standarized, and you should create your own (see http://stackoverflow.com/a/7478825/5249858). – 3442 Aug 23 '15 at 04:10