Posted on 2008-08-21 20:05
Prayer 閱讀(677)
評論(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, 參數(shù)說明
pathname必須存在且可以訪問,如果是32位系統(tǒng),屢次返回為4294967295時請注意,它實際是16進(jìn)制的FFFFFFFF,換成2進(jìn)制就是32個1,計算機一般采用補碼表示,最高位(復(fù)用的)1表示它是個負(fù)數(shù), 對它進(jìn)行取反再加1(考慮是負(fù)數(shù))得到-1, 也就是說ftok調(diào)用失敗。對于其他位數(shù)的系統(tǒng)類似。
3,dos格式和unix格式轉(zhuǎn)換問題
由于dos的回車換行和unix的不太一樣,有可能導(dǎo)致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對應(yīng)文件的inode,因此,無論多少連接,指向的都是同一個inode。
5,在一般的UNIX實現(xiàn)中,是將文件的索引節(jié)點號取出,前面加上子序號得到key_t的返回值。
如指定文件的索引節(jié)點號為65538,換算成16進(jìn)制為0x010002,而你指定的ID值為38,換算成16進(jìn)制為0x26,則最后的key_t返回值為0x26010002。
查詢文件索引節(jié)點號的方法是: ls -i
當(dāng)刪除重建文件后,索引節(jié)點號由操作系統(tǒng)根據(jù)當(dāng)時文件系統(tǒng)的使用情況分配,因此與原來不同,所以得到的索引節(jié)點號也不同。
如果要確保key_t值不變,要目確保ftok的文件不被刪除,要么不用ftok,指定一個固定的key_t值。
另外說一句:在aix等操作系統(tǒng)上,有多個文件系統(tǒng),會出現(xiàn)分布在不同的文件系統(tǒng)上的兩個文件具有相同的索引節(jié)點號,此時用ftok對這兩個文件進(jìn)行操作,只要id參數(shù)不變,得到的key_t值相同,造成創(chuàng)建消息隊列失敗。不過這種情況相當(dāng)少見罷了。
6,也有人說如果pathname是二進(jìn)制文件,則ftok會完全破壞該文件,我沒有測試過,所以不知道是真是假。