简体   繁体   English

我应该传递哪个文件作为ftok()的路径名参数

[英]Which file should I pass as pathname argument of ftok()

It is mentioned in ftok() manual 它在ftok()手册中提到过

key_t ftok(const char *pathname, int proj_id);

The ftok() function uses the identity of the file named by the given pathname (which must refer to an existing, accessible file) ... ftok()函数使用由给定路径名命名的文件的标识(必须引用现有的可访问文件) ...

I am confused about const char *pathname . 我对const char *pathname感到困惑。

What would be the best practice for it? 它最好的做法是什么? On my current system I can pass "/home/Andrew/anyfile" but it is not possible that other systems, on which my program has to work, will have this file. 在我当前的系统上,我可以传递"/home/Andrew/anyfile"但是我的程序必须工作的其他系统不可能有这个文件。

How about I use "/etc/hosts/" or "/etc/inittab" because I am sure all such systems will have these two files? 我怎么用"/etc/hosts/""/etc/inittab"因为我相信所有这些系统都会有这两个文件? Is it a good idea? 这是个好主意吗? Can it cause any problem? 它会引起任何问题吗?

I do not want to ask user to enter filename at the time of execution or pass file name as command line argument. 我不想要求用户在执行时输入文件名或将文件名作为命令行参数传递。

Is there any other different and better way to decide pathname ? 是否还有其他更好的方法来决定pathname
Which way is the best and most reliable one? 哪种方式最好,最可靠?

Thanks for your time. 谢谢你的时间。

Well, generally you would use a file associated with the application itself. 好吧,通常你会使用与应用程序本身相关的文件。

For example, we had an application which loaded a configuration file into shared memory (in a parsed efficiently accessible way - think of an XML file which has been turned into in-memory structures with fast pointers and so forth) and we created the shared memory segment from the ftok derived from the configuration file itself. 例如,我们有一个应用程序将配置文件加载到共享内存中(以解析的高效可访问的方式 - 想到一个XML文件已经变成具有快速指针的内存结构等等)我们创建了共享内存来自配置文件本身的ftok段。

Worst case, if you have no configuration files for your application, try to use the executable itself. 最糟糕的情况是,如果您的应用程序没有配置文件,请尝试使用可执行文件本身。 You can be pretty certain that it exists on the system somewhere (since you're running it). 你可以非常肯定它存在于某个地方的系统上(因为你正在运行它)。

You're also not restricted to files, you can use /etc itself or /tmp or even / if you must. 您也不限于文件,您可以使用/etc本身或/tmp甚至/如果必须。

I say "if you must" because it's a little bit dangerous. 我说“如果你必须”,因为它有点危险。 The ftok call will give you a unique key based on your file spec and your ID. ftok调用将根据您的文件规范和您的ID为您提供唯一的密钥。 If you use your own file such as /etc/andrew.conf , you can be reasonably certain that you won't get a clash with any other ftok -returned key. 如果您使用自己的文件,例如/etc/andrew.conf ,您可以合理地确定您不会与任何其他ftok recurned密钥发生冲突。

However, if you, and everyone else, decides to use /tmp as the file spec part then the only differentiator is the ID. 但是,如果您和其他所有人决定使用/tmp作为文件规范部分,则唯一的区别是ID。 Hence it's a lot easier to get a collision with other keys. 因此,与其他键发生冲突要容易得多。

What I've always done is to use the file spec as a truly unique value for my application and then just use the ID for the particular thing I want to create. 我一直在做的是使用文件规范作为我的应用程序的真正唯一值,然后只使用ID我想要创建的特定事物。

So, if I need 27 semaphores and 15 shared memory blocks, they all use /etc/pax.conf as the file spec and the IDs from 1 to 42 (and my application knows what ID relates to what object). 因此,如果我需要27个信号量和15个共享内存块,它们使用/etc/pax.conf作为文件规范,ID为1到42(我的应用程序知道哪个ID与哪个对象相关)。

Probably the best is to use argv[0] of one of your executables. 可能最好的方法是使用你的一个可执行文件的argv [0]。 The man page says 手册页说

The resulting value is the same for all pathnames that name the same file, ...

so you should be safe, even if your executable is sometimes called through a symbolic link or so. 因此,即使您的可执行文件有时通过符号链接调用,您也应该是安全的。

You can dynamically build a char * for a path based on a config file, or a command line parameter, etc. 您可以根据配置文件或命令行参数等为路径动态构建char *。

Just pass that char * into the function. 只需将char *传递给函数即可。

Use "." 采用 ”。” as the first parameter. 作为第一个参数。 It will send the current running directory to ftok. 它会将当前运行的目录发送到ftok。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM