系统在同一台机器同一个磁盘的不同账户中使用时,出现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;
}