操作系统课程设计二级文件系统

上传人:仙*** 文档编号:28324803 上传时间:2021-08-25 格式:DOC 页数:41 大小:527KB
返回 下载 相关 举报
操作系统课程设计二级文件系统_第1页
第1页 / 共41页
操作系统课程设计二级文件系统_第2页
第2页 / 共41页
操作系统课程设计二级文件系统_第3页
第3页 / 共41页
点击查看更多>>
资源描述
操作系统课程设计报告操作系统课程设计报告专 业:计算机信息处理 【设计目的】1. 课程设计目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。2. 结合数据结构、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。3. 通过对实际问题的分析、设计、编程实现,提高学生实际应用、编程的能力【设计内容】1、delete 删除文件2、open 打开文件3、close 关闭文件4、write 写文件【实验环境】Windows7系统 Visual studio 2010【相关知识综述】本文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户帐号下的文件。另外,为了简便文件系统未考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容。首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2并以编号作为物理地址,在目录中进行登记。【设计思路】1 主要数据结构#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 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 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-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 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) /*无法删除主目录的文件*/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=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 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,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); 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.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*/char 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);/获取文件编号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(rtrim(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;/没有打开的文件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,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(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);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部分函数含义引用于【源程序清单】#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 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 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_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(); /*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*/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子文件数*/int 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 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,delete,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.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=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(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) getchar();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();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;break;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_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)fwrite(&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 Exist-(-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) return(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(fp_mfd);ucount=j;SetPANo(0);printf(nnLogin successful! Welcome to this FileSystemnn);loginsuc=1;return;else/不相等printf(nn);flag=1;while(flag)printf(Login Failed! Password Error. Try Again(Y/N):);gets(a);ltrim(rtrim(a);if (strcmp(strupr(a),Y)=0) loginsuc=0;flag=0;else if(strcmp(strupr(a),N)=0)loginsuc=0;flag=0;return;else/*user not exist*/printf(New Password(=8):);InputPW(loginpw); /*input new password,use * replace*/printf(nConfirm Password(ufdname,strupr(ufd_lp.ufdname);fp_ufd=fopen(str,rb);for(i=0;fread(&ufdj-ufdfilei,sizeof(OSFILE),1,fp_ufd)!=0;i+,fcountj+)/记录用户文件ifopenji.ifopen=0;ifopenji.openmode=4;fclose(fp_ufd);fclose(fp_mfd);ucount=j;SetPANo(0);printf(nnLogin Successful! Welcome to this Systemnn);loginsuc=1;return;else/不相等printf(nn);flag=1;while(flag)printf(Login Failed! Password Error. Try Again(Y/N):);gets(a);ltrim(rtrim(a);if (strcmp(strupr(a),Y)=0) loginsuc=0;flag=0;else if(strcmp(strupr(a),N)=0)loginsuc=0;flag=0;return;void DirF() /*Dir FileSystem主目录*/int i,j,count=0;char sfmode25,sfpaddr25,str25;clrscr();if (strcmp(strupr(ltrim(rtrim(dirname),)!=0)/不为空printf(nnd:%sdirn,dirname);printf(n%14s%16s%14s%10s%18sn,FileName,FileAddress,FileLength,Type,FileMode);j=ExistD(dirname);for(i=0;ifcountj;i+)if (i%16=0)&(i!=0)/限制长度printf(nPress any key to
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!