1

While learning about semaphores, and specifically System V semaphores, I went onto reading about the System V IPC key, but couldn't understand the need for such identifier.

Taking it back to semaphores, if the purpose of the key identifier is:

"To make it easier for different processes to get the identifier from an object they need to share (Advanced UNIX Programming, 2nd ed, pg 429)"

why can't we just use the semaphore id as an identifier that may be used by different processes?

P.S. This answer talks about close subjects, but don't refer specifically to the alleged-possibility to use the object id as an identifier (instead of a key).

Community
  • 1
  • 1
OfirD
  • 9,442
  • 5
  • 47
  • 90

3 Answers3

2

May be you should try answering the following question:

How would you organize a set of processes to use the very same semaphore for synchronisation?

Processes (and people running or configuring those) do not have an easy way for identifying a semapohre by id. This value is internal to the running kernel and a value known from one kernel boot might not exists when booted next time. So, given a key as an (external) identifier you can easily associate an internal kernel object with an externally known name.

Associating an external key with an internal object is what the SysV IPC XXget() methods do (with the proper paramters, namely IPC_CREATE flag)

rpy
  • 3,953
  • 2
  • 20
  • 31
  • What is the importance of having a fixed identifier from boot to boot? In files pathnames, I guess it is simply for reasons of an ease of use. Is it the same with IPC objects? – OfirD Mar 14 '16 at 16:45
  • The IPC id is like the inode of a file, while the key resembles the path. You are usually not interested in the inode of a file, but you might know the path in advance (even if the file has not yet been created) – rpy Mar 14 '16 at 16:57
1

When several independent processess need to share a certain resource, they need to know how to access this resource. For example, file is a shared resource, and file name tells how to access this resource. This is rather generic idea.

When you access System V IPC resources from independent applications, you need a way to identify them. When you want to open the file, you need to know it's name. When you want to check on semaphore, you need to know it's name too. In System V those names take a form of numeric keys (which are easier for kernel to operate). This is where key comes from. Sempahore identifier can be anything - like a file descriptor - but a key is something which is set in stone.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

Keys are like pathnames, unique across the machine and accessible from any process. Identifiers are like file descriptors, used within a given process to index open resources. You access a file by passing its pathname to open(2) and then refer to it in read/write/close using the fd that open(2) returns. You access a queue by passing its key to msgget(2) and refer to it in msg* functions using the id that the msgget call returned.

Blair Houghton
  • 467
  • 3
  • 10