What is ftok assignment in message queue

I started reading message queues with one of the IPC mechanisms on Linux. But in the first step, I have some very simple questions.

  • Using ftok() to generate a unique identifier (key) and a unique identifier to be generated.

  • Can't we use a prime to get our keys, and not use ftok() ?

  • What is the purpose of the key argument in msget function?

     #include "sys/msg.h" key = ftok("/home/beej/somefile", 'b'); msqid = msgget(key, 0666 | IPC_CREAT); 
  • What is the difference between msqid and key ?

+7
source share
3 answers

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.

+16
source
  • I do not quite understand your question, but it generates a unique system identifier (not a process) based on the specified file path. This unique identifier (bound to a path) allows different processes to communicate with the same message queue.

  • Yes, you could if you designed it like that. But the path to the file is a more universal way of arriving at a general mechanism for generating a deterministic key, to which several processes are easily accessible.

  • See 1 and 2

  • msqid is similar to a file descriptor that you can send and receive messages to this descriptor. The key is what allows you to tie your hook into the message queue in which you are interested. By analogy, if the key refers to the file path in the global file system, then msqid will be your process handle for reading / writing to it.

+1
source
  • What?

  • This may work, but which one do you choose and who guarantees that other programs (or the system itself) will not use the same numbers? This will lead to confusion.

  • The goal is to provide a system-wide unique value for identifying a message queue. As the manpage says,

    Typically, a best-effort attempt combines the given proj_id byte, the lower 16 bits of the inode number, and the lower 8 bits of the device number into a 32-bit result. Collisions can easily occur, for example, between files on / dev / hda 1 and files on / dev / sda 1.

    Thus, he simply takes the file and calculates the identifier, from which it is known that another program using the same file and the project identifier will get the same result.

  • key is just a unique identifier, but can be used for other purposes, and msqid is an identifier (type of descriptor) for a really existing queue.

0
source

All Articles