The ftok function creates an identifier type that will be used with IPC System V functions ( semget , shmget , msgget ). Think of it like a filedescriptor: when you open a file, you pass the path to open and get the number in reverse order, which is then used for read and write to identify the file. The ftok function performs a similar task, but while the filedescriptor scope is limited only to a process called open (and its children), the ftok token ftok valid on the system.
The reason for the system area is that you want two or more independent processes to have access to the same IPC resources. Therefore, if you have two programs, each of which executes key = ftok("/home/beej/somefile", 'b'); , both will receive the same token and can receive the same resources (semaphores, shared memory, message queues). That is the whole point of Inter Process Communication.
You canβt just use a βprimeβ, because you donβt know if the token can be, for example, an index for a system-internal table or something else. In other words, you don't know how this token is used internally, so you need to use ftok .
The manual page says: "The specified path must indicate an existing file available to the calling process, or the call will not be completed. Also note that file links return the same key, given the same identifier." From this, I assume that at least some ftok implementations create a marker by looking at the inode number of the file given along the path and combine it with the second argument to create a token. The second argument exists simply so that you can create a bunch of IPC resources (for example, several semaphores to protect different resources).
Regarding the difference key_t (the value returned by ftok ) and the value stored by msgget : the first gives you access to a bunch of IPC resources (semaphore, shared memory and message queue) and later identifies a specific message queue.
Darkdust
source share