Reputation: 1767
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.
Upvotes: 10
Views: 29625
Reputation: 15408
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.
Upvotes: 9
Reputation: 8576
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);
Upvotes: 7