操作系统课程设计二级文件系统_第1页
操作系统课程设计二级文件系统_第2页
操作系统课程设计二级文件系统_第3页
操作系统课程设计二级文件系统_第4页
操作系统课程设计二级文件系统_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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个文件*

3、/#define max (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 st

4、ruct /*the 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; /*

5、ifopen:0-close,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 f

6、ile*/void 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(dirna

7、me),)=0) /*无法删除主目录的文件*/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(rtri

8、m(fname);i=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;

9、else fpaddrno1=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

10、,fname);wgetchar=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)

11、; for(j=0;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

12、.openmode=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*/cha

13、r fnamemaxname;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

14、);/获取文件编号if (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(rtr

15、im(dirname),)=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;/没有

16、打开的文件if(n!=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

17、,d:osfilefile);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

18、(ifopenki.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);

19、wgetchar=1;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定

21、义主文件*/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 structure of osufd定义用户文件目录*/char ufdnamemaxname; /*ufd name*/osfile ufdfilemaxchild; /*

22、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-close,1-open*/ int openmode; /*0-read only,1-write only,2-read and write,3-protect*/osufd

23、_openmode;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();

24、/*quit filesystem*/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*/

25、int writef1(); /*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子文件数*/in

26、t loginsuc=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

27、 own files*/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,delet

28、e,modify,read,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.

29、choose helpnd:%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=

30、3;else if(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

31、(strcmp(choice,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) g

32、etchar();break;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

33、();exit(0);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;brea

34、k;else n-;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_

35、file_p=fopen(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)fwr

36、ite(&i,sizeof(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 exis

37、t-(-1)*/int 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) retu

38、rn(i);else return(-1);int findpano() /*find out physical address num*/int i;for(i=0;imax;i+)if (fpaddrnoi=0) fpaddrnoi=1;break;if (iufdname,strupr(ufd_lp.ufdname);fp_ufd=fopen(str,rb);fcountj=0;/当前用户文件数目for(i=0;fread(&ufdj-ufdfilei,sizeof(osfile),1,fp_ufd)!=0;i+,fcountj+)/记录用户文件ifopenji.ifopen=0;ifopenji.openmode=4;fclose(fp_ufd);fclose(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论