问题描述
我已经开始读消息队列在Linux上。但IPC机制之一,在第一步,我有一些非常基本的问题。
-
ftok()
来生成唯一的ID(键),什么的使用是唯一的ID将被生成。 -
我们不能用简单的一个数字,让我们的钥匙,而不是使用
ftok()
? -
什么是争论的目的
键
在msget
功能?的#includeSYS / msg.h
键= ftok(/家庭/ beej / somefile,B);
=将对msqid了msgget(键,0666 | IPC_CREAT); -
是什么区别
将对msqid
和键
?
的 ftok
函数创建一个类标识符与系统V IPC功能使用(了semget
, shmget的
,了msgget
)。你可以把它像一个文件描述符:当你打开一个文件时,你传递给打开
和得到的回报一个数字,然后用于读取路径
和写
来识别文件。在 ftok
功能提供了类似的目的,但同时文件描述符的范围是有限的,只是调用进程打开
(和它的孩子),在 ftok
标记是整个系统中有效。
的原因该系统的范围是要两个或多个独立的进程能够访问相同的IPC资源。所以,如果你有两个项目,这两个执行键= ftok(/家庭/ beej / somefile,'B');
,都将获得同样的道理,并可以为此访问相同的资源(信号量,共享存储器,消息队列)。这是进程间通信的关键所在。
您不能只用一个简单的数字因为你不知道令牌是否可能是例如索引到一个系统内部表什么的。换句话说,你不知道怎么说令牌内部使用,这样你就需要使用 ftok
。
该名男子页说:指定的路径必须指定文件的链接将返回相同密钥的现有文件是调用进程或调用将失败也可以访问,请注意,由于相同的ID。 。的从这个我认为至少有一些 ftok
实现创建通过查找通过的中指定的文件的inode编号令牌路径和第二个参数来创建令牌相结合。第二个参数只是存在这样你就可以创建一批IPC资源(比如几个信号来保护不同的资源)。
至于的key_t
的差异(由 ftok
返回的值),并通过<$ C retured值$ C>了msgget :前者允许您访问了一堆IPC资源(信号量,共享内存和消息队列),而后来识别特定的消息队列
I have started reading message queues one of the IPC mechanism on Linux .But at very first step I have some very basic questions.
Use of
ftok()
to generate unique ID (key) and what is unique ID which is to be generated.Can't we use simple a number to get our keys rather than using
ftok()
?What is the purpose of the argument
key
in themsget
function?#include "sys/msg.h" key = ftok("/home/beej/somefile", 'b'); msqid = msgget(key, 0666 | IPC_CREAT);
What is the difference between
msqid
andkey
?
The ftok
function creates a sort of identifier to be used with the System V IPC functions (semget
, shmget
, msgget
). Think of it like a filedescriptor: when you open a file, you pass a path to open
and get a number in return that is then used for read
and write
to identify the file. The ftok
function serves a similar purpose, but while the filedescriptor's scope is limited to just the process that called open
(and its children), the ftok
token is valid across the system.
The reason for the system scope is that you want two or more independent processes to have access to the same IPC resources. So if you have two programs, both of which execute key = ftok("/home/beej/somefile", 'b');
, both will get the same token and can therefor access the same resources (semaphores, shared memory, message queues). That's the whole point of Inter Process Communication.
You cannot just use a "simple number" as you don't know whether the token might be for example an index to an system-internal table or something. In other words, you don't know how that token is used internally so you do need to use ftok
.
The man page says: "The specified path must specify an existing file that is accessible to the calling process or the call will fail. Also, note that links to files will return the same key, given the same id." From this I assume that at least some ftok
implementations create the token by looking up the inode number of the file specified by path and combine it with the second argument to create the token. The second argument exists simply so you can create a bunch of IPC resources (like several semaphores to protect different resources).
As for the difference of key_t
(the value returned by ftok
) and the value retured by msgget
: the former gives you access to a bunch of IPC resources (semaphore, shared memory and message queue), while the later identifies a specific message queue.
这篇关于什么是消息队列目的ftok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!