Name and Unnamed Semaphore

2020-02-25 08:34发布

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:

  • Unnamed semaphores might be usable by more than one process
  • Named semaphores are sharable by several processes

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?

3条回答
Deceive 欺骗
2楼-- · 2020-02-25 09:04

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.

查看更多
Luminary・发光体
3楼-- · 2020-02-25 09:05

Named semaphore has an actual name in the file system and can be shared by multiple unrelated processes.

Unnamed semaphores can be used only by threads belonging to the same process.

semaphores are created using the function

                 sem init(ptr_semaphore, flag, initial_value);

where

ptr_semaphore: a pointer to semaphore

flag: a flag indicating level of sharing

initial_value: semaphores's initial value

If you pass flag=0 to sem_init() then semaphore can be shared only by threads belonging to the process that created the semaphore.Thus it will create unamed semaphore.

          sem_init(ptr_semaphore,0,initial_value)   //unamed semaphore

Passing a nonzero value would allow other processes to access the semaphore as well. Thus it will result in named semaphore.

          sem_init(ptr_semaphore,1,initial_value)   //named semaphore
查看更多
在下西门庆
4楼-- · 2020-02-25 09:27

The accepted answer is wrong (as well as the other one given by @electron). Unnamed POSIX semaphores can be used by unrelated processes. You just have to store the data structure in shared memory accessible by the relevant processes and init it with the shareable flag set to one, as shown here (shamelessly copied from http://blog.superpat.com/2010/07/14/semaphores-on-linux-sem_init-vs-sem_open/):

int shm;
sem_t * mutex;

if ((shm = shm_open("myshm", O_RDWR | O_CREAT, S_IRWXU))   0) {
    perror("shm_open");
    exit(1);
}

if (ftruncate(shm, sizeof(sem_t)) < 0 ) {
    perror("ftruncate");
    exit(1);
}

if ((mutex = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0)) == MAP_FAILED) {
    perror("mmap");
    exit(1);
}

if (sem_init(mutex, 1, 1) < 0) {
    perror("semaphore initialization");
    exit(1);
}

Another process mapping that shared memory into its address space can access the same semaphore and synchronize with the original process. See also the POSIX specification of sem_init() and its linux man page.

查看更多
登录 后发表回答