前言
没有前言,今天废话少说,赶紧写完结束。
1,实验目的
- 理解Linux文件系统的内部技术,掌握Linux与文件有关的系统调用命令,并在此基础上建立面向随机检索的hash结构文件。
- Linux系统保持UNIX文件系统的风格,提供流式文件界面,这种结构具有简洁灵活的特点,但并不直接支持记录式文件和关键字检索。本实验是在Linux文件系统基础上,设计一组库函数,以提供对随机检索的支持。
2,实验内容
- 参考教材中hash文件构造算法,设计一组hash文件函数,包括hash文件创建、打开、关闭、读、写等。
- 编写一个测试程序,通过记录保存、查找、删除等操作,检查上述hash文件是否实现相关功能。
3,实验准备
creat系统调用
可以看这个文章:
(5条消息) creat系统调用_艾听先生的博客-优快云博客_create调用
open系统调用:
可以看这个文章:
(5条消息) Linux open系统调用流程(1)_chenjin_zhong的博客-优快云博客
close系统调用:
可以看这个文章:
(5条消息) 系统调用Close_剑决浮云气的博客-优快云博客_系统调用close
read系统调用:
可以看这个文章:
(5条消息) read 系统调用剖析_张鸷的博客-优快云博客_read系统调用
write系统调用:
可以看这个文章:
(5条消息) Linux系统调用 - write_|宇文拓>的博客-优快云博客_linux write系统调用
lseek系统调用:
可以看这个文章:
(5条消息) Linux系统调用四、lseek()函数详解_Mindtechnist的博客-优快云博客_linux中lseek函数
4,实验设计
- 由于在Linux系统核心之外模拟实现hash文件,有关hash文件的说明信息不能保存在inode中,而只能记录在文件的头部。这些信息包括hash文件标志、记录大小、文件长度、记录数量等。
- 可以根据hash文件核心算法设计内部函数,包括记录的保存、查找、删除等,在此基础上实现读、写等常规操作。
实验代码:
HashFile.h:
#ifndef HASHFILE_H
#define HASHFILE_H
#include<unistd.h>
#include<sys/stat.h>
#define COLLISIONFACTOR 0.5
struct 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 total_rec_num);
//int hashfile_open(const char *filename,int flags);
int hashfile_open(const char *filename,int flags,mode_t mode);
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_saverec(int fd,int keyoffset,int keylen,void *buf);
int hash(int keyoffset,int keylen,void *buf,int total_rec_num);
int checkHashFileFull(int fd);
int readHashFileHeader(int fd,struct HashFileHeader *hfh);
#endif
HashFIle.c:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#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;
hfh.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))*total_rec_num);
rtn = write(fd,buf,(reclen+sizeof(struct CFTag))*total_rec_num);
free(buf);
}
close(fd);
return rtn;
}
else{
close(fd);
return -1;
}
}
int hashfile_open(const char *filename,int flags,mode_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 keylen,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)
{
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(offset != -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.reclen+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{
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)
return -1;
struct CFTag tag;
read(fd,&tag,sizeof(struct CFTag));
char count = tag.collision;
if(count==0)
return -1; //不存在
recfree:
if(tag.free == 0)
{
offset += hfh.reclen+sizeof(struct CFTag);
if(lseek(fd,offset,SEEK_SET)==-1)
return -1;
read(fd,&tag,sizeof(struct CFTag));
goto recfree;
}
else{
char *p =(char*)malloc(hfh.reclen*sizeof(char));
read(fd,p,hfh.reclen);
//printf("Record is {%d , %s}\n",((struct jtRecord *)p)->key,((struct jtRecord *p)->other);
char *p1,*p2;
p1 = (char *)buf+keyoffset;
p2 = p + keyoffset;
int j=0;
while((*p1 == *p2)&&(j<keylen))
{
p1++;
p2++;
j++;
}
if(j==keylen)
{
free(p);
p = NULL;
return(offset);
}
else{
if(addr == hash(keyoffset,keylen,p,hfh.total_rec_num))
{
count--;
if(count == 0)
{
free(p);
p = NULL;
return -1;//不存在
}
}
free(p);
p = NULL;
offset += hfh.reclen+sizeof(struct CFTag);
if(lseek(fd,offset,SEEK_SET) == -1)
return -1;
read(fd,&tag,sizeof(struct CFTag));
goto recfree;
}
}
}
int hashfile_saverec(int fd,int keyoffset,int keylen,void *buf)
{
if(checkHashFileFull(fd))
{
return -1;
}
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)
return -1;
struct CFTag tag;
read(fd,&tag,sizeof(struct CFTag));
tag.collision++;
lseek(fd,sizeof(struct CFTag)*(-1),SEEK_CUR);
write(fd,&tag,sizeof(struct CFTag));
while(tag.free!=0) //冲突,顺序探查
{
offset += hfh.reclen+sizeof(struct CFTag);
if(offset >= lseek(fd,0,SEEK_END))
offset = sizeof(struct HashFileHeader); //reach at and,then rewind
if(lseek(fd,offset,SEEK_SET)==-1)
return -1;
read(fd,&tag,sizeof(struct CFTag));
}
tag.free =-1;
lseek(fd,sizeof(struct CFTag)*(-1),SEEK_CUR);
write(fd,&tag,sizeof(struct CFTag));
write(fd,buf,hfh.reclen);
hfh.current_rec_num++;
lseek(fd,0,SEEK_SET);
return write(fd,&hfh,sizeof(struct HashFileHeader)); //存入记录
}
int hash(int keyoffset,int keylen,void *buf,int total_rec_num)
{
int i=0;
char *p =(char*)buf+keyoffset;
int addr =0;
for(i=0;i<keylen;i++)
{
addr += (int)(*p);
p++;
}
return addr%(int)(total_rec_num*COLLISIONFACTOR);
}
int checkHashFileFull(int fd)
{
struct HashFileHeader hfh;
readHashFileHeader(fd,&hfh);
if(hfh.current_rec_num<hfh.total_rec_num)
return 0;
else
return 1;
}
int readHashFileHeader(int fd,struct HashFileHeader *hfh)
{
lseek(fd,0,SEEK_SET);
return read(fd,hfh,sizeof(struct HashFileHeader));
}
jtRecord.h:
#define RECORDLEN 32
struct jtRecord
{
int key;
char other[RECORDLEN-sizeof(int)];
};
#ifdef HAVE_CONFIG_H
#include<config.h>
#endif
jtRecord.c:
#ifdef HAVE_CONFIG_H
#include<config.h>
#endif
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include"HashFile.h"
#include"jtRecord.h"
#define KEYOFFSET 0
#define KEYLEN sizeof(int)
#define FileNAME "jing.hash"
void showHashFile();
int main(int argc,char *argv[])
{
struct jtRecord rec[6] = {
{1,"jing"},{2,"wang"},{3,"li"},
{4,"zhang"},{5,"qing"},{6,"yuan"}
};
int j=0;
for(j=0;j<6;j++)
{
printf("<%d,%d>\t",rec[j].key,hash(KEYOFFSET,KEYLEN,&rec[j],6));
}
int fd = hashfile_creat(FileNAME,O_RDWR|O_CREAT,RECORDLEN,6);
int i=0;
printf("\nOpen and Save Record...\n");
fd = hashfile_open(FileNAME,O_RDWR,0);
for(i=0;i<6;i++)
{
hashfile_saverec(fd,KEYOFFSET,KEYLEN,&rec[i]);
}
hashfile_close(fd);
showHashFile();
//Demo find Rec
printf("\nFind Record...");
fd = hashfile_open(FileNAME,O_RDWR,0);
int offset = hashfile_findrec(fd,KEYOFFSET,KEYLEN,&rec[4]);
printf("\noffset is %d\n",offset);
hashfile_close(fd);
struct jtRecord jt;
struct CFTag tag;
fd = open(FileNAME,O_RDWR);
lseek(fd,offset,SEEK_SET);
read(fd,&tag,sizeof(struct CFTag));
printf("Tag is <%d,%d>\t",tag.collision,tag.free);
read(fd,&jt,sizeof(struct jtRecord));
printf("Record is {%d,%s}\n",jt.key,jt.other);
//Demo Delete Rec
printf("\nDelete Record...");
fd = hashfile_open(FileNAME,O_RDWR,0);
hashfile_delrec(fd,KEYOFFSET,KEYLEN,&rec[2]);
hashfile_close(fd);
showHashFile();
//Demo Read
fd = hashfile_open(FileNAME,O_RDWR,0);
char buf[32];
memcpy(buf,&rec[1],KEYLEN);
hashfile_read(fd,KEYOFFSET,KEYLEN,buf);
printf("\nRead Record is {%d,%s}\n",
((struct jtRecord *)buf)->key,((struct jtRecord *)buf)->other);
hashfile_close(fd);
//Demo Write
printf("\nWrite Record...");
fd = hashfile_open(FileNAME,O_RDWR,0);
hashfile_write(fd,KEYOFFSET,KEYLEN,&rec[3]);
hashfile_close(fd);
showHashFile();
return 0;
}
void showHashFile()
{
int fd;
printf("\n");
fd = open(FileNAME,O_RDWR);
lseek(fd,sizeof(struct HashFileHeader),SEEK_SET);
struct jtRecord jt;
struct CFTag tag;
while(1)
{
if(read(fd,&tag,sizeof(struct CFTag))<=0)
break;
printf("Tag is <%d,%d>\t",tag.collision,tag.free);
if(read(fd,&jt,sizeof(struct jtRecord))<=0)
break;
printf("Record is {%d,%s}\n",jt.key,jt.other);
}
close(fd);
}
修改:
- 在HashFile.h里面在最外侧加上了#ifndef HASHFILE_H,#define HASHFILE_H,#endif。
- 在HashFile.h里面加入了#include<sys/stat.h>
- 在HashFile.h里面,把hashfile_creat里的参数int recnum改成了int total_rec_num,把hash里面的参数int recnum改成了int total_rec_num。
- 在HashFile.c里面加入了#include<stdlib.h>,#include<memory.h>。
- 在HashFile.c里面把tag.free=1;改成了tag.free=-1;
输出结果:
说明:
原本我编译的命令是:“gcc HashFile.h jtRecord.h HashFile.c jtRecord.c -o ex”,但是会有这样的错误:
经过上网查正发现命令只需要“gcc HashFile.c jtRecord.c -o ex”即可。
分析实验结果:
函数功能:
hashfile_creat函数:
创建一个新的Hash数据结构的对象
hashfile_open
将一个文件以Hash的形式打开,并且返回标识符,返回-1代表失败
hashfile_close
将一个文件关闭
hashfile_read
读取一个文件到Hash里面,并且将读写标识符放在读的最后一位
hashfile_write
调用hashfile_saverec,实现文件写功能
hashfile_delrec
删除Hash表里的记录
hashfile_findrec
找到Hash表里的某个记录
hashfile_saverec
存入Hash表里一个新的记录
hash
用于hash算法执行过程中不断遍历hash表的指针的地方
checkHashFileFull
调用readHashFileHeader,并且检查该Hash是否已经满了
readHashFileHeader
将文件的内容读到Hash对象里
showHashFile
打印目前Hash表里的信息
函数之间调用关系:
在jtRecord的main函数里,首先调用了hashfile_creat,创建了一个新的Hash表,然后调用hashfile_open,打开新的文件,然后在for循环里将rec的6个记录都使用hashfile_saverec记录,然后hashfile_close关闭,然后调用showHashFile打印。
在find demo中,首先hashfile_open打开Hash,然后调用hashfile_findrec来查找,然后hashfile_close关闭Hash表
在delete demo中,首先hashfile_open打开Hash,然后调用hashfile_delrec来删除,然后hashfile_close关闭Hash表,然后调用showHashFile打印一下
在read demo中,首先hashfile_open打开Hash,然后调用hashfile_read来读,然后hashfile_close关闭Hash表
在write demo中,首先hashfile_open打开Hash,然后调用hashfile_write来写,然后hashfile_close关闭Hash表
在hashfile_read中,调用了readHashFileHeader,hashfile_findrec。
在hashfile_write中,调用了hashfile_saverec
在hashfile_delrec中,调用了hashfile_findrec,readHashFileHeader,hash,
在hashfile_findrec中,调用了readHashFileHeader,hash
在hashfile_saverec中,调用了checkHashFileFull,readHashFileHeader,hash
在checkHashFileFull中,调用了readHashFileHeader
关键语句功能说明:
struct HashFileHeader
{
int sig; //Hash文件印鉴
int reclen; //记录长度
int total_rec_num; //总记录数
int current_rec_num; //当前记录数
};
这一段是Hash表的定义
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;
hfh.current_rec_num = 0;
//fd = open(filename,mode);
fd = creat(filename,mode);
这一段是Hash表初始化的部分,其中fd是creat的一个新文件,后续会在里面写内容
if(read(fd,&hfh,sizeof(struct HashFileHeader))!= -1)
{
lseek(fd,0,SEEK_SET);
这一段是将这个Hash打开,并且将读写标识符弄到最开始,便于后续操作
5,思考题
- 上述Hash文件缺少记录更新操作,试利用已有函数,编写hashfile_update()函数以完善Hash文件功能。
- 本实验所实现的hash文件是在Linux核心之外构造的,可以头文件或库函数的形式提供给其它应用程序。如果在核心级别上实现上述功能,应如何做?有何困难?
没写