版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、操作系统课程设计报告专 业: 学 号: 姓名 提交日期:【设计目的 】1、本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和 内部实现。2、结合数据结构、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步 理解操作系统。【设计内容 】为 Linux 系统设计一个简单的二级文件系统。要求做到以下几点: 可以实现下列几条命令:login用户登录dir列目录create创建文件delete删除文件open打开文件close关闭文件read读文件write写文件cd进出目录列目录时要列出文件名,物理地址,保护码和文件长度源文件可以进行读写保护【实验环境 】Wind
2、ows7 操作平台Visual Studio2010【相关知识综述 】理解二级目录的文件系统的组织; 掌握常用的数据结构; 系统采用两级目录, 其中第一级对 应于用户账号,第二级对应于用户帐号下的文件 ; 使用文件来模拟外存,进行数据结构设计 和操作算法的设计, 实现一个文件系统并实现基本的文件操作 (为了简便文件系统, 不考虑 文件共享,文件系统安全以及管道文件与设备文件等特殊内容) 。【设计思路 】 采用的数据结构、主要的函数说明、程序流程设计等本文件系统采用两级目录, 其中第一级对应于用户账号, 第二级对应于用户帐号下的文 件。另外, 为了简便文件系统未考虑文件共享, 文件系统安全以及管
3、道文件与设备文件等特 殊内容。首先应确定文件系统的数据结构: 主目录、 子目录及活动文件等。 主目录和子目录都以 文件的形式存放于磁盘,这样便于查找和修改。用户创建的文件,可以编号存储于磁盘上。如:并以编号作为物理地址, 在目录中进行登 记1.主要的数据结构#define MAXNAME 25 /*the largest length of mfdname,ufdname,表示三种文件的长度都为 25*/#define MAXCHILD 50 /*the largest child 每个用户下可以有 50 个文件 */#define MAX (MAXCHILD*MAXCHILD) /*the
4、size of fpaddrno定义一个常量 2500个扇区 */ typedef struct /*the structure of OSFILE*/int fpaddr;/* address物理地址 */int flength; /* 文件长度 */int fmode; /* Only;1-Write Only;2-Read and Write; 3-Protect;*/ char fnameMAXNAME; /* 文件名 */ OSFILE;typedef struct /*the structure of OSUFD*/char ufdnameMAXNAME; /*ufd name*/
5、OSMAXCHILD; /*ufd own file*/OSUFD;/*osf 文件的数据结构 */typedef struct /*the structure of OSUFDLOGIN*/char ufdnameMAXNAME; /*ufd name*/char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /* mode*/int ifopen; /*ifopen:0-close,1-open*/int openmode; /*0-read only,1-write only,2-read and write,3-initi
6、al*/ OSUFD_OPENMODE;2. 主要函数void LoginF(); /*LOGIN */void DirF(); /*Dir */void CdF(); /*Change Dir*/void CreateF(); /*Create File*/void DeleteF(); /*Delete File*/void ModifyFM(); /*Modify */void OpenF(); /*Open File*/void CloseF(); /*Close File*/void ReadF(); /*Read File*/void WriteF(); /*Write File*
7、/void QuitF(); /*Quit 退出文件系统 */void help();3总体功能程序结构图打开命令的程序流程图关闭命令的程序流程图写命令程序流程图删除命令的程序流程图:【源程序清单 】#include stdio.h#include string.h#include conio.h#include stdlib.h#define MAXNAME 25 /*the largest length of mfdname,ufdname, 表示三种文件的长度都为 25*/#define MAXCHILD 50 /*the largest child 每个用户下可以有 50 个文件 */
8、#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno 定义一个常量 2500 个扇区 */typedef struct /*the structure of OSFILE*/int fpaddr;/* address 物理地址 */int flength;/* 文件长度 */int fmode; /* Only;1-Write Only;2-Read and Write; 3-Protect; 文件标识可读可写可执 行*/char fnameMAXNAME;/* 文件名 */ OS 文件的数据结构,一级文件系统上创建的文件上例如 WANG
9、FANG 存储的就是这些内 容typedef struct/*the structure of OSUFD*/char ufdnameMAXNAME; /*ufd name*/OSMAXCHILD; /*ufd own file*/OSUFD;/*osf 文件的数据结构 */typedef struct /*the structure of OSUFDLOGIN 登录文件的数据结构 */char ufdnameMAXNAME; /*ufd name*/char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /* mode*/int
10、 ifopen; /*ifopen:0-close,1-open*/int openmode; /*0-read only,1-write only,2-read and write,3-initial*/ OSUFD_OPENMODE;OSUFD *ufdMAXCHILD;/*ufd and ufd own files 将 osfile 实例化 ufd ,有 50 个指向该结构的指针 */OSUFD_LOGIN ufd_lp;/ 声明了一个具体的变量int ucount=0; /*the count of mfds ufds 表示用户的个数 */ int fcountMAXCHILD;/*t
11、he count of ufds files*/int loginsuc=0; /*whether login successfully*/char usernameMAXNAME; /*record login users name22 定义了一个字符数组, 存放用户名 最大可放 25 个 */char dirnameMAXNAME;/*record current directory*/ int fpaddrnoMAX; /*record address num*/ OSUFD_OPENMODE ifopenMAXCHILDMAXCHILD; /*record */ int wgetcha
12、r; /*whether getchar()*/FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file;void LoginF(); /*LOGIN */void DirF(); /*Dir */void CdF(); /*Change Dir*/void CreateF(); /*Create File*/void DeleteF(); /*Delete File*/ void ModifyFM(); /*Modify */ void OpenF(); /*Open File*/ void CloseF(); /*Close File*/ void ReadF();
13、 /*Read File*/ void WriteF(); /*Write File*/ void QuitF(); /*Quit 退出文件系统 */ 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*/ void SetPANo(int RorW); /*Set physical
14、address num 设置物理地址 */ int ExistD(char *dirname); /*Whether DirName Exist,Exist-i,Not Exist-0 该函数在退出的时候 将内容写回磁盘 */ int WriteF1(); /*write file*/int ExistF(char *); /*Whether Exist,Exist-i,Not Exist-0*/int FindPANo(); /*find out physical address num 找到物理地址 */清屏 void clrscr() system(cls);int main()int
15、i,choice1;char choice50; /*choice operation:dir,create,delete,open,delete,modify,read,write*/int choiceend=1; /*whether choice end 表示选择是否结束 */char *rtrim(char *str); /*remove the trailing blanks. 去除后面的空格 */char *ltrim(char *str); /*remove the heading blanks. 去除前面的空格 */该 if 语句的目的只是为了检测该文件是否存在,若不存在则创建
16、 if(fp_mfd=fopen(c:osfilemfd.txt,rb)=NULL)/ 以只写的方式打开文件,返回一个 指针,若不存在fp_mfd=fopen(c:osfilemfd.txt,wb);/ 则创建该文件 fclose(fp_mfd);/ 创建成功之后关闭该文件/将模拟的磁盘区域全部初始化为零 for(i=0;i,strupr(dirname);elseprintf(Bad command or .nC:%s,strupr(username); gets(choice);/ 输入所选择的 strcpy(choice,ltrim(rtrim(strlwr(choice);/ 将输入的
17、值赋给 choice if (strcmp(choice,dir)=0) choice1=1;/ 依次将输入的值与 dir ,create 等等进行 比较else if(strcmp(choice,create)=0) choice1=2;/ 如果输入 create 则将 choice1 置 为 2 通过 switch 进行选择else if(strcmp(choice,delete)=0) choice1=3;/ 依次内推else if(strcmp(choice,attrib)=0) choice1=4;else if(strcmp(choice,open)=0) choice1=5;el
18、se 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=
19、12;/choicel=12 时跳转到 default:choiceend=0; 因为 while ( 1)所以要不断循环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(
20、);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:c
21、lrscr();choiceend=1;break;case 11:CdF();choiceend=1;break;case 20:help();choiceend=1;break; default:choiceend=0;else /如果没有登录成功printf(nAccess denied.);void help(void)printf(nThe Command Listn); printf(nCd Attrib Create write Read Open Cls Delete Exit Closen);char *rtrim(char *str) 一个 */*remove the tr
22、ailing blanks. 除去末尾端的空格符号指针指向字符串第int n=strlen(str)-1;/n 为字符串的长度减 1 while(n=0)/if(*(str+n)!= )/ 末尾不存在空格*(str+n+1)=0;/0 表示结束符 break;else n-;/如果存在空格,则将空格去掉if (nufdname,strupr(ufd_lp.ufdname); fp_ufd=fopen(str,rb);fcountj=0; for(i=0;fread(&ufdj-ufdfilei,sizeof(OS)!=0;i+,fcountj+) ifopenji.ifopen=0; ifop
23、enji.openmode=4;fclose(fp_ufd);fclose(fp_mfd);ucount=j;SetPANo(0);printf(nnLogin successful! Welcome to this nn); loginsuc=1;return;elseprintf(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(strcm
24、p(strupr(a),N)=0)loginsuc=0;flag=0;return;elseprintf(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(OS)!=0;i+,fcountj+) ifopenji.ifopen=0; ifopenji.openmode=4;
25、fclose(fp_ufd); fclose(fp_mfd); ucount=j;SetPANo(0); printf(nnLogin Successful! Welcome to this Systemnn); loginsuc=1;return;elseprintf(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(st
26、rupr(a),N)=0) loginsuc=0; flag=0; return;void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/int i,j;if (RorW=0) if(fp_(c:osfilefile,rb)=NULL)/ 如果文件未读成功 fp_(c:osfilefile,wb);/ 则创建该文件 fclose(fp_file_p);fp_(c:osfilefile,rb);/for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i+)fpaddrnoj=1;/ 真
27、正模拟的位示图的关系/*for(i=1;iMAX;i+)if (i%13)=0) fpaddrnoi=1;*/ elsefp_(c:osfilefile,wb);/*for(i=1;iMAX;i+)if(i%13)=0) fpaddrnoi=0;*/ for(i=0;iMAX;i+)/if (fpaddrnoi=1)/ 表示已使用 fwrite(&i,sizeof(int),1,fp_file_p);/ 把第几个扇区号写进文件 fclose(fp_file_p);void InputPW(char *password) /*input password,use * replace*/int j
28、;for(j=0;j0)/ 且密码个数大于 0putchar(b);putchar( );putchar(b);/b 表示退格 putchar 函数只能用 于单个字符的输出,且一次只能输出一个字符 else j-;else passwordj=0;/0 是字符串的结束符 ,如果输出完毕则终止 break; passwordj=0;void DirF() /*Dir */int i,j,count=0;char sfmode25,sfpaddr25,str25;clrscr();if (strcmp(strupr(ltrim(rtrim(dirname),)!=0) printf(nnC:%sd
29、irn,dirname); printf(n%14s%16s%14s%10s%18sn,Type,);j=ExistD(dirname);for(i=0;iufdfilei.fpaddr,str,10); strcpy(sfpaddr,file); strcat(sfpaddr,str);if (ufdj-ufdfilei.fmode=0) strcpy(sfmode,Read Only);else if(ufdj-ufdfilei.fmode=1) strcpy(sfmode,Write Only);else if(ufdj-ufdfilei.fmode=2)strcpy(sfmode,Re
30、ad And Write);else strcpy(sfmode,Protect);printf(%14s%16s%14d%10s%18sn,ufdj-ufdfilei.fname,sfpaddr,ufdj-ufdfilei.flen gth,sfmode);printf(n %3d file(s)n,fcountj); else printf(nnC:dirn); printf(n%14s%18s%8sn,DirName,Own,Type); for(i=0;iufdname,fcounti,); count=count+fcounti; printf(n %3d dir(s),%5d fi
31、le(s)n,ucount,count);int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/ int i;int exist=0; for(i=0;iufdname),strupr(dirname)=0) exist=1; break; if (exist) return(i); else return(-1);void CdF() /*Exchange Dir*/char dnameMAXNAME;printf(nPlease input DirName (cd.-Previous dir; DirN
32、AME-cd DirNAME):); gets(dname);ltrim(rtrim(dname); if (ExistD(dname)=0) strcpy(dirname,strupr(dname);elseif(strcmp(strupr(dname),CD.)=0) strcpy(ltrim(rtrim(dirname),);else printf(nError.%s does not exist.n,dname);void CreateF() /*Create File*/int fpaddrno,flag=1,i;char fnameMAXNAME,str50,str150,a25;
33、char fmode25;if (strcmp(strupr(dirname),strupr(username)!=0)printf(nError. You must create your own dir.n);wgetchar=1;elseprintf(nPlease input :);gets(fname);ltrim(rtrim(fname);if (ExistF(fname)=0)printf(nError. Name %s has already existed.n,fname);wgetchar=1;elseprintf(Please input (0-Read Only, 1-
34、Write Only, 2-Read and Write, 3-Protect):); gets(fmode);ltrim(rtrim(fmode);if(strcmp(fmode,0)=0)|(strcmp(fmode,1)=0)|(strcmp(fmode,2)=0)|(strcmp(fmode ,3)=0)fpaddrno=FindPANo();if (fpaddrno=0)i=ExistD(username);strcpy(ufdi-ufdi.fname,fname);ufdi-ufdi.fpaddr=fpaddrno;ufdi-ufdi.fmode=atoi(fmode);ifope
35、nifcounti.ifopen=0;ifopenifcounti.openmode=4; strcpy(str,c:osfilefilefile); itoa(fpaddrno,str1,10); strcat(str,str1); strcat(str,.txt); fp_(str,wb); fclose(fp_file); fcounti+;while(flag)printf(Input text now(Y/N):);gets(a);ltrim(rtrim(a);ufdi-ufdi-1.flength=0; if(strcmp(strupr(a),Y)=0) fp_(str,wb+);
36、 ufdi-ufdi-1.flength=WriteF1(); flag=0;else if(strcmp(strupr(a),N)=0)flag=0; wgetchar=1;printf(n%s has been created successfully!n,fname);elseprintf(nFail!No Disk Space. Please format your disk.n); wgetchar=1;elseprintf(nError. s Range is 0-3n);wgetchar=1;int ExistF(char *) /*Whether Exist,Exist-i,N
37、ot Exist-0 该函数检测某文件是否存在 */int i,j;int exist=0;j=ExistD(dirname);for(i=0;iufdfilei.fname),strupr()=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;/ 然后加以使用bre
38、ak;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 fpaddrno
39、1=ufdk-ufdfilei.fpaddr;/ 获取文件对应的物理文件名 fpaddrnofpaddrno1=0;/ 回收盘块 for(n=i;nufdfilen=ufdk-ufd;/ 从被删除的文件号开始,数组值全部 前移一个strcpy(str,d:osfilefilefile); itoa(fpaddrno1,str1,10);/ 整数转化成字符串 strcat(str,str1);/把 str1 添加到 str 结尾处strcat(str,.txt); /把 .txt 添加到 str 结尾处 remove(str);/ 删除物理文件 fcountk-;/k 用户文件数量少 1 pri
40、ntf(n %sis deleted successfully.n,fname); wgetchar=1;else/所要删除的文件不存在printf(nError. %s dose not exist.n,fname);wgetchar=1;void ModifyFM() /*Modify */char fnameMAXNAME,str50;int i,k;char fmode25; /*whether delete*/if (strcmp(strupr(dirname),strupr(username)!=0)printf(nError.You can only modify in yourself dir.n);wgetchar=1;elseprintf(nPlease input :);gets(fname); ltrim(rtrim(fname); i=ExistF(fname);if (i=0)k=ExistD(username);if(ifopenki.ifopen=1)printf(nError
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 甘肃海南公务员考试试题及答案
- 农林产业融合发展项目建设工程方案
- 2025中建七局第六建筑有限公司校园招聘笔试历年典型考点题库附带答案详解试卷3套
- 方塘公务员考试口碑试题及答案
- 赤峰公务员考试辅导试题及答案
- 污水处理厂尾水深海排放工程施工方案
- 高效混凝土搅拌站生产调度管理方案
- 城市基础设施改造技术方案
- 白山市公务员考试知识试题及答案
- 十五五规划纲要:碳捕集利用与封存(CCUS)产业布局
- 幼儿园保温桶管理制度
- 放射科提高(CT)图像质量的甲级率品管圈汇报书ppt模板
- 结婚登记审查处理表
- 小学六年级全册体育教案(已整理)
- 2 试验二 系统相频特性对信号传输的影响试验 2
- 非煤矿山企业安全标准化管理台帐(浙江省)
- 冀教版六年级上册数学第二单元测试卷(比和比例)
- GB 18281.4-2015医疗保健产品灭菌生物指示物第4部分:干热灭菌用生物指示物
- 阀门维修要求及验收标准
- 劳动防护用品规范使用培训课件
- 2022年首席质量官试题答案
评论
0/150
提交评论