I'm trying to understand the similarities and differences between named and unnamed semaphore so my google searches yielded me this. I had a question about the wording on the page though, it says:
Do those two words create any important distinction between those two types of semaphores or are they irrelevant?
So so far here's what I have:
Similarities
-Several processes can do something with the semaphore
Difference
-Named are referenced with pathname and unnamed are referenced by pshared value
That's all I could glean from that definition. Is that everything and are they correct? Or am I missing some significant concept?
Named semaphores A named semaphore is identified by a name of the form /somename; that is, a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters consisting of an initial slash, followed by one or more characters, none of which are slashes.
Specifically, where sem_open() returns a pointer to a newly allocated semaphore, sem_init() takes a reference to a semaphore variable (declared as a sem_t ) that has already been allocated within the current process's memory space and sets the semaphore to the value passed.
sem_destroy() destroys an unnamed semaphore at the address pointed to by sem. Only a semaphore that has been initialized by sem_init() should be destroyed using sem_destroy() .
POSIX semaphores allow processes and threads to synchronize their actions. A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (sem_post(3)); and decrement the semaphore value by one (sem_wait(3)).
Think in terms of who can access the semaphore.
Unnamed semaphores (lacking any name or handle to locate them) must exist in some pre-existing, agreed upon memory location. Usually that is (1) shared memory (inherited by children after fork
) in the case of child processes; or (2) shared memory, global variable or the heap in the case where they are shared between threads of a single process. The essential thing here is that the code in parent, child, or threads already knows the address of the semaphore.
Named semaphores are necessary for unrelated processes. For example a producer and consumer might be written by two different developers and run as completely unrelated processes. But they have to share some resource that needs to be protected by a semaphore. The named semaphore gives them a path to the semaphore.
In reality you can use a named semaphore in all scenarios but they come with a little extra baggage because you have to deal with the paths and permissions and such that are unnecessary if the programs are related and already know how to access an unnamed semaphore. It's a little silly, for instance, to use a named semaphore to share a resource between threads. The threads already have access to the same memory where an unnamed semaphore could reside.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With