《操作系统课程设计二级文件系统.doc》由会员分享,可在线阅读,更多相关《操作系统课程设计二级文件系统.doc(41页珍藏版)》请在三一办公上搜索。
1、操作系统课程设计报告专 业:计算机信息处理 【设计目的】1. 课程设计目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。2. 结合数据结构、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。3. 通过对实际问题的分析、设计、编程实现,提高学生实际应用、编程的能力【设计内容】1、delete 删除文件2、open 打开文件3、close 关闭文件4、write 写文件【实验环境】Windows7系统 Visual studio 2010【相关知识综述】本文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户帐号下的文件。另外,为了简
2、便文件系统未考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容。首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2并以编号作为物理地址,在目录中进行登记。【设计思路】1 主要数据结构#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/#define MAXCHILD 50 /*the largest child每个用户名下最多有50个文件*/#define M
3、AX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/typedef struct /*the structure of OSFILE定义主文件*/int fpaddr; /*file physical address*/ int flength; /*file length*/ int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write; 3-Protect;*/ char fnameMAXNAME; /*file name*/ OSFILE;typedef struct /*the
4、 structure of OSUFD定义用户文件目录*/char ufdnameMAXNAME; /*ufd name*/OSFILE ufdfileMAXCHILD; /*ufd own file*/OSUFD;typedef struct /*the structure of OSUFDLOGIN定义登陆*/char ufdnameMAXNAME; /*ufd name*/ char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /*file open mode定义操作方式*/int ifopen; /*ifopen:0-c
5、lose,1-open*/ int openmode; /*0-read only,1-write only,2-read and write,3-initial*/OSUFD_OPENMODE;2 主要函数void LoginF(); /*LOGIN FileSystem*/void DirF(); /*Dir FileSystem*/void CreateF(); /*Create File*/void DeleteF(); /*Delete File*/void ModifyFM(); /*Modify FileMode*/void OpenF(); /*Open File*/void
6、CloseF(); /*Close File*/void ReadF(); /*Read File*/void WriteF(); /*Write File*/void QuitF(); /*Quit FileSystem*/void CdF(); /*Change Dir*/void help();【主要程序段】1 Delete函数void DeleteF() /*Delete File*/ char fnameMAXNAME,str50,str150;int i,k,j;int fpaddrno1; if (strcmp(strupr(ltrim(rtrim(dirname),)=0) /
7、*无法删除主目录的文件*/printf(nError.Please convert to ufd dir before delete.n);wgetchar=1;if (strcmp(strupr(dirname),strupr(username)!=0) /*无法删除非自己目录的文件*/printf(nError.You can only modify filemode in yourself dir.n);wgetchar=1;elseprintf(nPlease input FileName:);gets(fname);/从键盘获取所要删除的文件名ltrim(rtrim(fname);i
8、=ExistF(fname);/获取文件编号if (i=0)k=ExistD(username);if(ifopenki.ifopen=1)/*文件打开时无法删除*/printf(nError.%s is in open status. Close it before delete.n,fname);wgetchar=1;else if(ufdk-ufdfilei.fmode=3)/*被保护的文件无法删除*/ printf(nError.%s is in protect status. Close it before delete.n,fname);wgetchar=1; else fpadd
9、rno1=ufdk-ufdfilei.fpaddr;/获取文件对应的物理文件名fpaddrnofpaddrno1=0;/回收盘块for(j=i;jufdfilej=ufdk-ufdfilej+1;/从被删除的文件号开始,数组值全部前移一个 strcpy(str,d:osfilefilefile);itoa(fpaddrno1,str1,10);/整数转化成字符串strcat(str,str1);strcat(str,.txt);remove(str);/删除物理文件fcountk-;/k用户文件数量少1printf(n %sis deleted successfully.n,fname);wg
10、etchar=1;else/所要删除的文件不存在 printf(nError. %s dose not exist.n,fname);wgetchar=1;2 Open函数void OpenF() /*Open File*/ char fnameMAXNAME;int i,k,j; if (strcmp(strupr(dirname),strupr(username)!=0) /*在自己的目录里才能打开*/printf(nError.You can only open in yourself dir.n);wgetchar=1;else k=ExistD(username); for(j=0;
11、jufdfilej.fname); printf(nPlease input FileName:);gets(fname);/获取所要打开的文件名ltrim(rtrim(fname);i=ExistF(fname);/获取目录编号if (i=0)if(ifopenki.ifopen=1)/输入的文件是打开的printf(nError.%s is in open status.n,fname);wgetchar=1;else ifopenki.ifopen=1;/修改为打开的if(ufdk-ufdfilei.fmode=0)/*根据文件的模式设置打开模式*/ ifopenki.openmode=
12、0; else if(ufdk-ufdfilei.fmode=1) ifopenki.openmode=1; else if(ufdk-ufdfilei.fmode=2) ifopenki.openmode=2; else ifopenki.openmode=3; printf(n%s is opened successfullyn,fname); wgetchar=1;else/文件不存在printf(nError. %s dose not exist.n,fname);wgetchar=1;3 Close函数void CloseF() /*Close File*/char fnameMAX
13、NAME;int i,k,j; if (strcmp(strupr(dirname),strupr(username)!=0) /*不在自己的目录里没法进行*/printf(nError.You can only close file in yourself dir.n);wgetchar=1;else k=ExistD(username); for(j=0;jufdfilej.fname); printf(nPlease input FileName:);gets(fname);/从键盘上读入所要关闭的文件ltrim(rtrim(fname);i=ExistF(fname);/获取文件编号i
14、f (i=0)ifopenki.ifopen=0;/*关闭文件*/printf(n %s closed successfullyn,fname);wgetchar=1;else/所要关闭的文件不存在printf(nError. %s dose not exist.n,fname);wgetchar=1;4 Write函数void WriteF() /*Write File*/int i,k,n=0;int length;char fnameMAXNAME,values1000;char str255,str1255,c;if (strcmp(strupr(ltrim(rtrim(dirname
15、),)=0) /只能写自己目录下的文件printf(nError.Please convert to ufd dir before write.n);wgetchar=1;return;printf(nCaution:Open file firstn);printf(Opened File(s) List:n);k=ExistD(dirname);/获取目录编号for(i=0;iufdfilei.fname);n+;if(n%4=0)&(n!=0) printf(n);printf(n%d files openned.n,n);if (n=0) wgetchar=1;/没有打开的文件if(n!
16、=0)printf(nPlease input FileName:);gets(fname);/从键盘获取所要写的文件ltrim(rtrim(fname);i=ExistF(fname);/获取文件编号if(i=0)if(ifopenki.ifopen=1)/如果文件是打开的if(ifopenki.openmode=1) |(ifopenki.openmode=2)/文件是只写,或者是可读可写的itoa(ufdk-ufdfilei.fpaddr,str,10);/获取文件对应的物理地址strcpy(str1,file);strcat(str1,str);strcpy(str,d:osfilef
17、ile);strcat(str,str1);strcat(str,.txt);fp_file=fopen(str,ab);/以二进制只写的形式打开,每次将内容添加到文件末尾接着上一次内容length=WriteF1();ufdk-ufdfilei.flength=ufdk-ufdfilei.flength+length;/计算总长度printf(nn%d Length.n,ufdk-ufdfilei.flength);printf(nnYou have write file successfully!); fclose(fp_file);wgetchar=0;else if(ifopenki.
18、openmode=0)/文件是只读的printf(nError.%s has been opened with READ ONLY mode. It isnt write.n,fname);wgetchar=1;else/文件是保护的printf(nError.%s has been opened with PROTECT mode. It isnt write.n,fname);wgetchar=1;else /文件是关闭的printf(nError.%s is in closing status. Please open it before writen,fname);wgetchar=1
19、;else /所指定的文件不存在printf(nError. %s does not exist.n,fname);wgetchar=1;【程序流程设计】1 总的功能结构图:2 部分子模块程序流程图(1)打开命令的程序流程图:(2)关闭命令的程序流程图:(3)写命令的程序流程图:(4)删除命令的程序流程图:【测试结果】1 删除文件2 打开的文件不能删除3 打开文件,其中已经打开的文件不能再次打开3 关闭文件4 写文件,其中只有打开了文件才能写入5 写文件6 只读文件和保护文件不能写入7 其他函数【参考文献】计算机操作系统,西安电子科技大学出版社,方敏主编,2004.8部分函数含义引用于【源程序
20、清单】#include stdio.h#include string.h#include conio.h#include stdlib.h#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/#define MAXCHILD 50 /*the largest child每个用户名下最多有50个文件*/#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/typedef struct /*the structure of OSFILE定义主文件*/int
21、fpaddr; /*file physical address*/ int flength; /*file length*/ int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write; 3-Protect;*/ char fnameMAXNAME; /*file name*/ OSFILE;typedef struct /*the structure of OSUFD定义用户文件目录*/char ufdnameMAXNAME; /*ufd name*/OSFILE ufdfileMAXCHILD; /*ufd own fi
22、le*/OSUFD;typedef struct /*the structure of OSUFDLOGIN定义登陆*/char ufdnameMAXNAME; /*ufd name*/ char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /*file open mode定义操作方式*/int ifopen; /*ifopen:0-close,1-open*/ int openmode; /*0-read only,1-write only,2-read and write,3-protect*/OSUFD_OPENMODE;
23、void LoginF(); /*LOGIN FileSystem*/void DirF(); /*Dir FileSystem*/void CreateF(); /*Create File*/void DeleteF(); /*Delete File*/void ModifyFM(); /*Modify FileMode*/void OpenF(); /*Open File*/void CloseF(); /*Close File*/void ReadF(); /*Read File*/void WriteF(); /*Write File*/void QuitF(); /*Quit Fil
24、eSystem*/void CdF(); /*Change Dir*/void help();char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/void InputPW(char *password); /*input password,use * replace*/int ExistD(char *dirname); /*Whether DirName Exist,Exist-i,Not Exist-0*/int WriteF
25、1(); /*write file*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/void SetPANo(int RorW); /*Set physical address num*/int FindPANo(); /*find out physical address num*/int ucount=0; /*the count of mfds ufds用户数*/int fcountMAXCHILD; /*the count of ufds files子文件数*/int loginsuc
26、=0; /*whether login successfully登陆成功*/char usernameMAXNAME; /*record login users name22用户名*/char dirnameMAXNAME;/*record current directory使用的用户目录*/int fpaddrnoMAX; /*record file physical address num物?理地址号,存放自己所创建的所有文件的地址*/int wgetchar; /*whether getchar()*/OSUFD *ufdMAXCHILD; /*ufd and ufd own files
27、*/OSUFD_LOGIN ufd_lp;/用户登录结构体类型的变量OSUFD_OPENMODE ifopenMAXCHILDMAXCHILD; /*record file open/close*/FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file;/FILE 是变量类型,实际上是语言定义的标准数据结构,用于文件void clrscr()/*清屏*/system(cls);void main()int i,choice1;char choice50; /*choice operation:dir,create,delete,open,delete,modify,r
28、ead,write*/int choiceend=1; /*whether choice end*/fopen标准函数,打开文件msd.txt,打开一个二进制文件,只允许读数据,送返指针,指向FILE类型对象。if(fp_mfd=fopen(d:osfilemfd.txt,rb)=NULL)fp_mfd=fopen(d:osfilemfd.txt,wb);/打开一个二进制文件,只允许写数据fclose(fp_mfd);/关闭一个流for(i=0;i,strupr(dirname);/表示以字符串型输出else printf(Bad command or file name.Choose hel
29、pnd:%s,strupr(dirname);/strupr-变为大写字母gets(choice);strcpy(choice,ltrim(rtrim(strlwr(choice);/strlwr将字符串转换为小写形式,strcpy(dest,char)复制 /ltrim(rtrim()去掉输入用户名的头部和尾部空格if (strcmp(choice,dir)=0) choice1=1;/strcmp相同为0else if(strcmp(choice,create)=0) choice1=2;else if(strcmp(choice,delete)=0) choice1=3;else if(
30、strcmp(choice,attrib)=0) choice1=4;else if(strcmp(choice,open)=0) choice1=5;else if(strcmp(choice,close)=0) choice1=6;else if(strcmp(choice,read)=0) choice1=7;else if(strcmp(choice,write)=0) choice1=8;else if(strcmp(choice,exit)=0) choice1=9;else if(strcmp(choice,cls)=0) choice1=10;else if(strcmp(ch
31、oice,cd)=0) choice1=11;else if(strcmp(choice,help)=0) choice1=20;else choice1=12;switch(choice1)case 1:DirF();choiceend=1;break;case 2:CreateF();choiceend=1;if(!wgetchar) getchar();break;case 3:DeleteF();choiceend=1;if(!wgetchar)getchar();break;case 4:ModifyFM();choiceend=1;if(!wgetchar) getchar();b
32、reak;case 5:OpenF();choiceend=1;if (!wgetchar) getchar();break;case 6:CloseF();choiceend=1;if (!wgetchar) getchar();break;case 7:ReadF();choiceend=1;if (!wgetchar) getchar();break;case 8:WriteF();choiceend=1;if (!wgetchar) getchar();break;case 9:printf(nYou have exited this system.); QuitF();exit(0)
33、;break;case 10:clrscr();choiceend=1;break;case 11:CdF();choiceend=1;break;/更改目录名称case 20:help();choiceend=1;break;default:choiceend=0;else printf(nAccess denied.);char *rtrim(char *str) /*remove the trailing blanks.去掉登陆用户名的尾空格*/int n=strlen(str)-1;while(n=0)if(*(str+n)!= )*(str+n+1)=0;break;else n-;
34、if (n0) str0=0;return str;char *ltrim(char *str) /*remove the heading blanks.去掉登陆用户名的头空格*/strrev(str);/字符的顺序颠倒rtrim(str);strrev(str);return str;void SetPANo(int RorW) /*Set physical address num,0-read,1-write物理地址*/int i,j;if (RorW=0)if(fp_file_p=fopen(d:osfilefilefile_p.txt,rb)=NULL)/只读fp_file_p=fop
35、en(d:osfilefilefile_p.txt,wb);/只写fclose(fp_file_p);fp_file_p=fopen(d:osfilefilefile_p.txt,rb);/只读/fread读数据(输入地址,大小,个数,提供数据的文件),sizeof是内存空间的大小不是长度for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i+)fpaddrnoj=1;elsefp_file_p=fopen(d:osfilefilefile_p.txt,wb);/只写for(i=0;iMAX;i+)if (fpaddrnoi=1)fwrite(&i,siz
36、eof(int),1,fp_file_p);/fwrite写数据输出地址,字节数,个数,目标文件)fclose(fp_file_p);void InputPW(char *password) /*input password,use * replace*/int j;for(j=0;j0)j-;putchar(b);putchar( );putchar(b);else j-;elsepasswordj=0; break;passwordj=0;int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-(-1)*/in
37、t i;int exist=0;for(i=0;iufdname),strupr(dirname)=0)exist=1;break;if (exist) return(i);else return(-1);int ExistF(char *filename) /*Whether FileName Exist,Exist-i,Not Exist-(-1)*/int i,j;int exist=0;j=ExistD(dirname);for(i=0;iufdfilei.fname),strupr(filename)=0)exist=1;break;if (exist) return(i);else return(-1);int FindPANo() /*find ou