2022年文件系统——Hash .pdf
《2022年文件系统——Hash .pdf》由会员分享,可在线阅读,更多相关《2022年文件系统——Hash .pdf(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、文件系统 Hash一、实验目的1、理解 linux 文件系统的内部技术2、掌握 linux 与文件有关的系统调用命令,并在此基础上建立面向随机检索的 hash结构文件。二、实验内容与设计思想实验内容:1、 参考教程中 Hash文件构造算法, 设计一组 Hash文件函数,包括 Hash文件创建、打开、关闭、读、写等。2、编写测试程序,通过记录保存、查找、删除等操作,检查上述Hash文件是否实现相关功能。三、实验使用环境Linux Redhat4 四、实验结果实验代码:HashFile.h #include #define COLLISIONFACTOR 0.5 /Hash函数冲突因子struct
2、 HashFileHeader int sig; /Hash文件印鉴int reclen; /记录长度int total_rec_num; /总记录数int current_rec_num; /当前记录数; struct CFTag char collision; /冲突计数char free; /空闲标志; /函数声明int hashfile_creat(const char *filename,mode_t mode,int reclen,int recnum); int hashfile_open(const char *filename,int flags, mode_t mode);
3、 int hashfile_close(int fd); int hashfile_read(int fd,int keyoffset,int keylen,void *buf); int hashfile_write(int fd,int keyoffset,int keylen,void *buf); int hashfile_delrec(int fd,int keyoffset,int keylen,void *buf); int hashfile_findrec(int fd,int keyoffset,int keylen,void *buf); int hashfile_save
4、rec(int fd,int keyoffset,int keylen,void *buf); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 11 页 - - - - - - - - - int hash(int keyoffset,int keylen,void *buf,int recnum); int checkHashFileFull(int fd); int readHashFileHeader(int fd,struct HashFileHeader *hf
5、h ) ; HashFile.c #include #include #include #include #include #include HashFile.h int hashfile_creat(const char *filename,mode_t mode,int reclen,int total_rec_num) struct HashFileHeader hfh; int fd; int rtn; char *buf; int i=0; hfh.sig=31415926; hfh.reclen=reclen; hfh.total_rec_num=total_rec_num; hf
6、h.current_rec_num=0; /fd=open(filename,mode); fd=creat(filename,mode); if(fd!=-1) rtn=write(fd,&hfh,sizeof(struct HashFileHeader); /lseek(fd,sizeof(struct HashFileHeader),SEEK_SET); if(rtn!=-1) buf=(char*)malloc(reclen+sizeof(struct CFTag)*total_rec_num); memset(buf,0,(reclen+sizeof(struct CFTag)*to
7、tal_rec_num); rtn=write(fd,buf,(reclen+sizeof(struct CFTag)*total_rec_num); free(buf); close(fd); return rtn; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 11 页 - - - - - - - - - else close(fd); return -1; int hashfile_open(const char *filename,int flags, mode
8、_t mode) int fd=open(filename,flags,mode); struct HashFileHeader hfh; if(read(fd,&hfh,sizeof(struct HashFileHeader)!=-1) lseek(fd,0,SEEK_SET); if(hfh.sig=31415926) return fd; else return -1; else return -1; int hashfile_close(int fd) return close(fd); int hashfile_read(int fd,int keyoffset,int keyle
9、n,void *buf) struct HashFileHeader hfh; readHashFileHeader(fd,&hfh); int offset=hashfile_findrec(fd,keyoffset,keylen,buf); if(offset!=-1) lseek(fd,offset+sizeof(struct CFTag),SEEK_SET); return read(fd,buf,hfh.reclen); else return -1; int hashfile_write(int fd,int keyoffset,int keylen,void *buf) 名师资料
10、总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 11 页 - - - - - - - - - return hashfile_saverec(fd,keyoffset,keylen,buf); /return -1; int hashfile_delrec(int fd,int keyoffset,int keylen,void *buf) int offset; offset=hashfile_findrec(fd,keyoffset,keylen,buf); if(offse
11、t!=-1) struct CFTag tag; read(fd,&tag,sizeof(struct CFTag); tag.free=0; /置空闲标志lseek(fd,offset,SEEK_SET); write(fd,&tag,sizeof(struct CFTag); struct HashFileHeader hfh; readHashFileHeader(fd,&hfh); int addr=hash(keyoffset,keylen,buf,hfh.total_rec_num); offset=sizeof(struct HashFileHeader)+addr*(hfh.r
12、eclen+sizeof(struct CFTag); if(lseek(fd,offset,SEEK_SET)=-1) return -1; read(fd,&tag,sizeof(struct CFTag); tag.collision-; /冲突记数减 1 lseek(fd,offset,SEEK_SET); / write(fd,&tag,sizeof(struct CFTag); hfh.current_rec_num-; /当前记录数减 1 lseek(fd,0,SEEK_SET); write(fd,&hfh,sizeof(struct HashFileHeader); else
13、 return -1; int hashfile_findrec(int fd,int keyoffset,int keylen,void *buf) struct HashFileHeader hfh; readHashFileHeader(fd,&hfh); int addr=hash(keyoffset,keylen,buf,hfh.total_rec_num); int offset=sizeof(struct HashFileHeader)+addr*(hfh.reclen+sizeof(struct CFTag); if(lseek(fd,offset,SEEK_SET)=-1)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年文件系统Hash 2022 文件系统 Hash
限制150内