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.
You said that you're using Ubuntu GNU/Linux, so...
Use named semaphores!
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()
: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()
.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.
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.