How to create semaphores in shared memory in C?

2020-02-13 09:44发布

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.

2条回答
\"骚年 ilove
2楼-- · 2020-02-13 10:02

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);
查看更多
爷、活的狠高调
3楼-- · 2020-02-13 10:13

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);

POSIX semaphores are implicitly destroyed when the last process having a handle to the semaphore exits. They are rumored to be faster than System V semaphores

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.

查看更多
登录 后发表回答