系統在同一臺機器同一個磁盤的不同賬戶中使用時,出現ftok創建key時重複的現象,
而通過ls -i命令查看,i-node結點值明顯不一致,爲什麼還會出現重複呢,
經過測試發現幾個問題:
1、ftok產生key時,組合了3個變量:st_dev、st_ino、proj_id;
在Solaris系統上,取proj_id%0xff,st_dev後3位,st_ino後3位組成。
st_dev:1e000000c7, st_ino:8d03, key:10c7d03
st_dev:1e000000c7, st_ino:8d03, key:100c7d03
st_dev:1e000000c7, st_ino:8d03, key:ff0c7d03
st_dev:1e000000c7, st_ino:8d03, key:c7d03
st_dev:1e00000047, st_ino:46e0, key:10476e0
st_dev:1e00000047, st_ino:46e0, key:100476e0
st_dev:1e00000047, st_ino:46e0, key:ff0476e0
st_dev:1e00000047, st_ino:46e0, key:476e0
st_dev:1e00000087, st_ino:155eb, key:10875eb
st_dev:1e00000087, st_ino:155eb, key:100875eb
st_dev:1e00000087, st_ino:155eb, key:ff0875eb
st_dev:1e00000087, st_ino:155eb, key:875eb
在Linux系統上,取proj_id%0xff,st_dev後2位,st_ino後4位組成。
st_dev:802, st_ino:1c60001, key:1020001
st_dev:802, st_ino:1c60001, key:10020001
st_dev:802, st_ino:1c60001, key:ff020001
st_dev:802, st_ino:1c60001, key:20001
st_dev:802, st_ino:1c70006, key:1020006
st_dev:802, st_ino:1c70006, key:10020006
st_dev:802, st_ino:1c70006, key:ff020006
st_dev:802, st_ino:1c70006, key:20006
st_dev:802, st_ino:1cd0262, key:1020262
st_dev:802, st_ino:1cd0262, key:10020262
st_dev:802, st_ino:1cd0262, key:ff020262
st_dev:802, st_ino:1cd0262, key:20262
2、操作系統i-node結點創建重複
發現2個賬戶下創建的key重複後,考慮到創建key使用了文件夾的i-node信息,將制定的目錄刪除後,測試key還是重複。
後ls -i發現,刪除目錄在創建時,i-node信息沒有變化,還是使用剛刪除的那個,無語了。
只好先建立一個臨時目錄,將重複的i-node用掉後,再創建一個新的。
測試代碼如下:
#include <stdio.h>
#include <errno.h>#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ipc.h>
#define N 3
print_inode(char *fn)
{
struct stat stat1 ;
stat(fn, &stat1 ) ;
printf("st_dev:%llx, st_ino:%llx, key:%x\n", stat1.st_dev, stat1.st_ino , ftok(fn,0x1));
printf("st_dev:%llx, st_ino:%llx, key:%x\n", stat1.st_dev, stat1.st_ino , ftok(fn,0x10));
printf("st_dev:%llx, st_ino:%llx, key:%x\n", stat1.st_dev, stat1.st_ino , ftok(fn,0xff));
printf("st_dev:%llx, st_ino:%llx, key:%x\n\n", stat1.st_dev, stat1.st_ino , ftok(fn,0x100));
}
main()
{
//char fn[N][256]={"/home/g-yang/ctest/", "/home/g-yang/gyang/", "/home/g-yang/mcpp3.0/"};
char fn[N][256]={"/export/home2/g-yang/", "/export/home/mcpp25/", "/export/home1/"};
int i;
for(i=0; i<N; i++){
print_inode(fn[i]);
}
return;
}