操作系统课程设计二级文件系统.docx
《操作系统课程设计二级文件系统.docx》由会员分享,可在线阅读,更多相关《操作系统课程设计二级文件系统.docx(44页珍藏版)》请在冰豆网上搜索。
操作系统课程设计二级文件系统
操作系统课程设计二级文件系统(总36页)
操作系统课程设计报告
专业:
计算机信息处理
学号:
09103408
姓名:
纪旻材
提交日期:
2011-12-28
【设计目的】
1.课程设计目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。
2.结合数据结构、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。
3.通过对实际问题的分析、设计、编程实现,提高学生实际应用、编程的能力
【设计内容】
1、delete删除文件
2、open打开文件
3、close关闭文件
4、write写文件
【实验环境】
Windows7系统
Visualstudio2010
【相关知识综述】
本文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户帐号下的文件。
另外,为了简便文件系统未考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容。
首先应确定文件系统的数据结构:
主目录、子目录及活动文件等。
主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。
用户创建的文件,可以编号存储于磁盘上。
如:
file0,file1,file2…并以编号作为物理地址,在目录中进行登记。
【设计思路】
1主要数据结构
#defineMAXNAME25/*thelargestlengthofmfdname,ufdname,filename*/
#defineMAXCHILD50/*thelargestchild每个用户名下最多有50个文件*/
#defineMAX(MAXCHILD*MAXCHILD)/*thesizeoffpaddrno*/
typedefstruct/*thestructureofOSFILE定义主文件*/
{
intfpaddr;/*filephysicaladdress*/
intflength;/*filelength*/
intfmode;/*filemode:
0-ReadOnly;1-WriteOnly;2-ReadandWrite;3-Protect;*/
charfname[MAXNAME];/*filename*/
}OSFILE;
typedefstruct/*thestructureofOSUFD定义用户文件目录*/
{
charufdname[MAXNAME];/*ufdname*/
OSFILEufdfile[MAXCHILD];/*ufdownfile*/
}OSUFD;
typedefstruct/*thestructureofOSUFD'LOGIN定义登陆*/
{
charufdname[MAXNAME];/*ufdname*/
charufdpword[8];/*ufdpassword*/
}OSUFD_LOGIN;
typedefstruct/*fileopenmode定义操作方式*/
{
intifopen;/*ifopen:
0-close,1-open*/
intopenmode;/*0-readonly,1-writeonly,2-readandwrite,3-initial*/
}OSUFD_OPENMODE;
2主要函数
voidLoginF();/*LOGINFileSystem*/
voidDirF();/*DirFileSystem*/
voidCreateF();/*CreateFile*/
voidDeleteF();/*DeleteFile*/
voidModifyFM();/*ModifyFileMode*/
voidOpenF();/*OpenFile*/
voidCloseF();/*CloseFile*/
voidReadF();/*ReadFile*/
voidWriteF();/*WriteFile*/
voidQuitF();/*QuitFileSystem*/
voidCdF();/*ChangeDir*/
voidhelp();
【主要程序段】
1Delete函数
voidDeleteF()/*DeleteFile*/
{charfname[MAXNAME],str[50],str1[50];
inti,k,j;
intfpaddrno1;
if(strcmp(strupr(ltrim(rtrim(dirname))),"")==0)/*无法删除主目录的文件*/
{
printf("\converttoufddirbeforedelete.\n");
wgetchar=1;
}
if(strcmp(strupr(dirname),strupr(username))!
=0)/*无法删除非自己目录的文件*/
{
printf("\canonlymodifyfilemodeinyourselfdir.\n");
wgetchar=1;
}
else
{
printf("\nPleaseinputFileName:
");
gets(fname);fopen==1)/*文件打开时无法删除*/
{
printf("\nError.\'%s\'isinopenstatus.Closeitbeforedelete.\n",fname);
wgetchar=1;
}
else
{
if(ufd[k]->ufdfile[i].fmode==3)/*被保护的文件无法删除*/
{
printf("\nError.\'%s\'isinprotectstatus.Closeitbeforedelete.\n",fname);
wgetchar=1;
}
else
{
fpaddrno1=ufd[k]->ufdfile[i].fpaddr;xt");
remove(str);n",fname);
wgetchar=1;
}
}
}
else\'%s\'dosenotexist.\n",fname);
wgetchar=1;
}
}
}
2Open函数
voidOpenF()/*OpenFile*/
{
charfname[MAXNAME];
inti,k,j;
if(strcmp(strupr(dirname),strupr(username))!
=0)/*在自己的目录里才能打开*/
{
printf("\canonlyopeninyourselfdir.\n");
wgetchar=1;
}
else
{
k=ExistD(username);
for(j=0;j{
printf("%15s",ufd[k]->ufdfile[j].fname);
}
printf("\nPleaseinputFileName:
");
gets(fname);fopen==1)'%s\'isinopenstatus.\n",fname);
wgetchar=1;
}
else
{
ifopen[k][i].ifopen=1;mode==0)/*根据文件的模式设置打开模式*/
{ifopen[k][i].openmode=0;}
elseif(ufd[k]->ufdfile[i].fmode==1)
{ifopen[k][i].openmode=1;}
elseif(ufd[k]->ufdfile[i].fmode==2)
{ifopen[k][i].openmode=2;}
elseifopen[k][i].openmode=3;
printf("\n\'%s\'isopenedsuccessfully\n",fname);
wgetchar=1;
}
}
else\'%s\'dosenotexist.\n",fname);
wgetchar=1;
}
}
}
3Close函数
voidCloseF()/*CloseFile*/
{
charfname[MAXNAME];
inti,k,j;
if(strcmp(strupr(dirname),strupr(username))!
=0)/*不在自己的目录里没法进行*/
{
printf("\canonlyclosefileinyourselfdir.\n");
wgetchar=1;
}
else
{
k=ExistD(username);
for(j=0;j{
if(ifopen[k][j].ifopen==1)name);
}
printf("\nPleaseinputFileName:
");
gets(fname);fopen=0;/*关闭文件*/
printf("\n\'%s\'closedsuccessfully\n",fname);
wgetchar=1;
}
else\'%s\'dosenotexist.\n",fname);
wgetchar=1;
}
}
}
4Write函数
voidWriteF()/*WriteFile*/
{
inti,k,n=0;
intlength;
charfname[MAXNAME],values[1000];
charstr[255],str1[255],c;
if(strcmp(strupr(ltrim(rtrim(dirname))),"")==0)n");
wgetchar=1;
return;
}
printf("\nCaution:
Openfilefirst\n");
printf("OpenedFile(s)List:
\n");
k=ExistD(dirname);fopen==1)name);
n++;
}
if((n%4==0)&&(n!
=0))printf("\n");
}
printf("\n%dfilesopenned.\n",n);
if(n==0)wgetchar=1;fopen==1)penmode==1)||(ifopen[k][i].openmode==2))paddr,str,10);xt");
fp_file=fopen(str,"ab");length=ufd[k]->ufdfile[i].flength+length;n",ufd[k]->ufdfile[i].flength);
printf("\n\nYouhavewritefilesuccessfully!
!
");
fclose(fp_file);
wgetchar=0;
}
elseif(ifopen[k][i].openmode==0)'%s\'hasbeenopenedwithREADONLYmode.Itisn\'twrite.\n",fname);
wgetchar=1;
}
else'%s\'hasbeenopenedwithPROTECTmode.Itisn\'twrite.\n",fname);
wgetchar=1;
}
}
else'%s\'isinclosingstatus.Pleaseopenitbeforewrite\n",fname);
wgetchar=1;
}
}
else\'%s\'doesnotexist.\n",fname);
wgetchar=1;
}
}
}
【程序流程设计】
1总的功能结构图:
2部分子模块程序流程图
(1)打开命令的程序流程图:
(2)关闭命令的程序流程图:
(3)写命令的程序流程图:
(4)删除命令的程序流程图:
【测试结果】
1删除文件
2打开的文件不能删除
3打开文件,其中已经打开的文件不能再次打开
3关闭文件
4写文件,其中只有打开了文件才能写入
5写文件
6只读文件和保护文件不能写入
7其他函数
【参考文献】
计算机操作系统,西安电子科技大学出版社,方敏主编,
部分函数含义引用于【源程序清单】
#include""
#include""
#include""
#include""
#defineMAXNAME25/*thelargestlengthofmfdname,ufdname,filename*/
#defineMAXCHILD50/*thelargestchild每个用户名下最多有50个文件*/
#defineMAX(MAXCHILD*MAXCHILD)/*thesizeoffpaddrno*/
typedefstruct/*thestructureofOSFILE定义主文件*/
{
intfpaddr;/*filephysicaladdress*/
intflength;/*filelength*/
intfmode;/*filemode:
0-ReadOnly;1-WriteOnly;2-ReadandWrite;3-Protect;*/
charfname[MAXNAME];/*filename*/
}OSFILE;
typedefstruct/*thestructureofOSUFD定义用户文件目录*/
{
charufdname[MAXNAME];/*ufdname*/
OSFILEufdfile[MAXCHILD];/*ufdownfile*/
}OSUFD;
typedefstruct/*thestructureofOSUFD'LOGIN定义登陆*/
{
charufdname[MAXNAME];/*ufdname*/
charufdpword[8];/*ufdpassword*/
}OSUFD_LOGIN;
typedefstruct/*fileopenmode定义操作方式*/
{
intifopen;/*ifopen:
0-close,1-open*/
intopenmode;/*0-readonly,1-writeonly,2-readandwrite,3-protect*/
}OSUFD_OPENMODE;
voidLoginF();/*LOGINFileSystem*/
voidDirF();/*DirFileSystem*/
voidCreateF();/*CreateFile*/
voidDeleteF();/*DeleteFile*/
voidModifyFM();/*ModifyFileMode*/
voidOpenF();/*OpenFile*/
voidCloseF();/*CloseFile*/
voidReadF();/*ReadFile*/
voidWriteF();/*WriteFile*/
voidQuitF();/*QuitFileSystem*/
voidCdF();/*ChangeDir*/
voidhelp();
char*rtrim(char*str);/*removethetrailingblanks.*/
char*ltrim(char*str);/*removetheheadingblanks.*/
voidInputPW(char*password);/*inputpassword,use'*'replace*/
intExistD(char*dirname);/*WhetherDirNameExist,Exist-i,NotExist-0*/
intWriteF1();/*writefile*/
intExistF(char*filename);/*WhetherFileNameExist,Exist-i,NotExist-0*/
voidSetPANo(intRorW);/*Setphysicaladdressnum*/
intFindPANo();/*findoutphysicaladdressnum*/
intucount=0;/*thecountofmfd'sufds用户数*/
intfcount[MAXCHILD];/*thecountofufd'sfiles子文件数*/
intloginsuc=0;/*whetherloginsuccessfully登陆成功*/
charusername[MAXNAME];/*recordloginuser'sname22用户名*/
chardirname[MAXNAME];/*recordcurrentdirectory使用的用户目录*/
intfpaddrno[MAX];/*recordfilephysicaladdressnum物理地址号,存放自己所创建的所有文件的地址*/
intwgetchar;/*whethergetchar()*/
OSUFD*ufd[MAXCHILD];/*ufdandufdownfiles*/
OSUFD_LOGINufd_lp;;
QuitF();exit(0);break;
case10:
clrscr();choiceend=1;break;
case11:
CdF();choiceend=1;break;;
}
char*rtrim(char*str)/*removethetrailingblanks.去掉登陆用户名的尾空格*/
{
intn=strlen(str)-1;
while(n>=0)
{
if(*(str+n)!
='')
{
*(str+n+1)='\0';
break;
}
elsen--;
}
if(n<0)str[0]='\0';
returnstr;
}
char*ltrim(char*str)/*removetheheadingblanks.去掉登陆用户名的头空格*/
{
strrev(str);name),strupr(filename))==0)
{
exist=1;
break;
}
if(exist)return(i);
elsereturn(-1);
}
intFindPANo()/*findoutphysicaladdressnum*/
{
inti;
for(i=0;iif(fpaddrno[i]==0)
{
fpaddrno[i]=1;
break;
}
if(ielsereturn(-1);
}
intWriteF1()/*writefile*/
{
intlength=0;
charc;
printf("Pleaseinputtext(\'#\'standsforend):
\n");
while((c=getchar())!
='#')
{
fprintf(fp_file,"%c",c);
if(c!
='\n')length++;
}
fprintf(fp_file,"\n");
fclose(fp_file);
return(length);
}
voidLoginF()/*LOGINFileSystem登陆函数*/
{
charloginame[MAXNAME],loginpw[9],logincpw[9],str[50];xt");
ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));fopen=0;
ifopen[j][i].openmode=4;
}
fclose(fp_ufd);
}
fclose(fp_mfd);
ucount=j;
SetPANo(0);
printf("\n\nLoginsuccessful!
WelcometothisFileSystem\n\n");
loginsuc=1;
return;
}
elseTryAgain(Y/N):
");
gets(a);
ltrim(rtrim(a));
if(strcmp(strupr(a),"Y")==0)
{
loginsuc=0;
flag=0;
}
elseif(strcmp(strupr(a),"N")==0)
{
loginsuc=0;
flag=0;
return;
}
}
}
}
else/*usernotexist*/
{
printf("NewPassword(<=8):
");
InputPW(loginpw);/*inputnewpassword,use'*'replace*/
printf("\nConfirmPassword(<=8):
");/*inputnewpassword,use'*'replace*/
InputPW(logincpw);
if(strcmp(loginpw,logincpw)==0)xt");