Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

ftok点滴

Posted on 2008-08-21 20:05 Prayer 阅读(658) 评论(0)  编辑 收藏 引用 所属分类: LINUX/UNIX/AIX
1, Redhat9.0的man手册
FTOK(3)                                         Linux Programmers Manual                          
NAME
       ftok - convert a pathname and a project identifier to a System V IPC key
SYNOPSIS
       # include <sys/types.h>
       # include <sys/ipc.h>
       key_t ftok(const char *pathname, int proj_id);
DESCRIPTION
       The  ftok  function  uses  the  identity of the file named by the given pathname (which must refer to an existing,
       accessible file) and the least significant 8 bits of proj_id (which must be nonzero) to generate a key_t type Sys-
       tem V IPC key, suitable for use with msgget(2), semget(2), or shmget(2).
       The resulting value is the same for all pathnames that name the same file, when the same value of proj_id is used.
       The value returned should be different when the (simultaneously existing) files or the project IDs differ.
RETURN VALUE
       On success the generated key_t value is returned. On failure -1 is returned, with errno indicating  the  error  as
       for the stat(2) system call.
 

2, 参数说明
       pathname必须存在且可以访问,如果是32位系统,屡次返回为4294967295时请注意,它实际是16进制的FFFFFFFF,换成2进制就是32个1,计算机一般采用补码表示,最高位(复用的)1表示它是个负数, 对它进行取反再加1(考虑是负数)得到-1, 也就是说ftok调用失败。对于其他位数的系统类似。
 
 
3,dos格式和unix格式转换问题
       由于dos的回车换行和unix的不太一样,有可能导致pathname找不到。
 
 
4,The resulting value is the same for all pathnames that  name  the  same file, when the same value of proj_id is used.
       一般来说,ftok对应文件的inode,因此,无论多少连接,指向的都是同一个inode。
 
 
5,在一般的UNIX实现中,是将文件的索引节点号取出,前面加上子序号得到key_t的返回值。
如指定文件的索引节点号为65538,换算成16进制为0x010002,而你指定的ID值为38,换算成16进制为0x26,则最后的key_t返回值为0x26010002。
查询文件索引节点号的方法是: ls -i
当删除重建文件后,索引节点号由操作系统根据当时文件系统的使用情况分配,因此与原来不同,所以得到的索引节点号也不同。
如果要确保key_t值不变,要目确保ftok的文件不被删除,要么不用ftok,指定一个固定的key_t值。
另外说一句:在aix等操作系统上,有多个文件系统,会出现分布在不同的文件系统上的两个文件具有相同的索引节点号,此时用ftok对这两个文件进行操作,只要id参数不变,得到的key_t值相同,造成创建消息队列失败。不过这种情况相当少见罢了。
 
 
6,也有人说如果pathname是二进制文件,则ftok会完全破坏该文件,我没有测试过,所以不知道是真是假。


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理