Posted on 2008-08-21 20:05
Prayer 閱讀(671)
評論(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會完全破壞該文件,我沒有測試過,所以不知道是真是假。