




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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每个用
3、户名下最多有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*/ OSFIL
4、E;typedef struct /*the structure of OSUFD定义用户文件目录*/char ufdnameMAXNAME; /*ufd name*/OSFILE ufdfileMAXCHILD; /*ufd own file*/OSUFD;typedef struct /*the structure of OSUFD'LOGIN定义登陆*/char ufdnameMAXNAME; /*ufd name*/ char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /*file open mode定义操作方
5、式*/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
6、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(
7、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("nP
8、lease 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)/*被
9、保护的文件无法删除*/ 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;j<fcountk;j+) ufdk->ufdfilej=ufdk->ufdfilej+1;/从被删除的文件号开始,数组值全部前移一个 strcpy(str,"d:o
10、sfilefilefile");itoa(fpaddrno1,str1,10);/整数转化成字符串strcat(str,str1);strcat(str,".txt");remove(str);/删除物理文件fcountk-;/k用户文件数量少1printf("n '%s'is deleted successfully.n",fname);wgetchar=1;else/所要删除的文件不存在 printf("nError. '%s' dose not exist.n",fname);wgetc
11、har=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;j<fcountk;j+)printf("%15s",ufdk->ufdfilej.fn
12、ame); 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)/*根据文件
13、的模式设置打开模式*/ 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
14、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;j<fcountk;j+)/*列出已经打开的
15、文件*/if(ifopenkj.ifopen=1)/如果文件是打开的printf("%15s",ufdk->ufdfilej.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);wget
16、char=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.Plea
17、se 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;i<fcountk;i+)/*列出可以写的文件*/if(ifopenki.ifopen=1)/如果文件是打开的printf("%15s",ufdk->ufdfilei.fname);n+;if(n%4=
18、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) |(ifop
19、enki.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-&
20、gt;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 ONL
21、Y mode. It isn't write.n",fname);wgetchar=1;else/文件是保护的printf("nError.'%s' has been opened with PROTECT mode. It isn't write.n",fname);wgetchar=1;else /文件是关闭的printf("nError.'%s' is in closing status. Please open it before writen",fname);wgetchar=1;els
22、e /所指定的文件不存在printf("nError. '%s' does not exist.n",fname);wgetchar=1;【程序流程设计】1 总的功能结构图:2 部分子模块程序流程图(1)打开命令的程序流程图:(2)关闭命令的程序流程图:(3)写命令的程序流程图:(4)删除命令的程序流程图:【测试结果】1 删除文件2 打开的文件不能删除3 打开文件,其中已经打开的文件不能再次打开3 关闭文件4 写文件,其中只有打开了文件才能写入5 写文件6 只读文件和保护文件不能写入7 其他函数【参考文献】计算机操作系统,西安电子科技大学出版社,方敏主编,2
23、004.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 siz
24、e 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定义用户文件目录*/cha
25、r ufdnameMAXNAME; /*ufd name*/OSFILE ufdfileMAXCHILD; /*ufd own file*/OSUFD;typedef struct /*the structure of OSUFD'LOGIN定义登陆*/char ufdnameMAXNAME; /*ufd name*/ char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /*file open mode定义操作方式*/int ifopen; /*ifopen:0-close,1-open*/ int openmode;
26、 /*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 Read
27、F(); /*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*/i
28、nt 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
29、 of mfd's ufds用户数*/int fcountMAXCHILD; /*the count of ufd's files子文件数*/int loginsuc=0; /*whether login successfully登陆成功*/char usernameMAXNAME; /*record login user's name22用户名*/char dirnameMAXNAME;/*record current directory使用的用户目录*/int fpaddrnoMAX; /*record file physical address num物?理地址号
30、,存放自己所创建的所有文件的地址*/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("
31、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:o
32、sfilemfd.txt","wb");/打开一个二进制文件,只允许写数据fclose(fp_mfd);/关闭一个流for(i=0;i<MAX;i+) fpaddrnoi=0; clrscr(); /*clear screen*/LoginF(); /*user login*/if(loginsuc=1) /*Login Successfully*/while (1)wgetchar=0;if (choiceend=1)printf("nnd:%s>",strupr(dirname);/表示以字符串型输出else printf(&q
33、uot;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,"creat
34、e")=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(
35、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
36、();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) g
37、etchar();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()
38、;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 (n<0) str0='0'return str;char *ltrim(char *str
39、) /*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:osfilefilefi
40、le_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&qu
41、ot;);/只写for(i=0;i<MAX;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;j<=7;j+)passwordj=getch();/gerch()返回从键盘上读取到的字符if (int)(passwordj)!=13)if(int)(passwo
42、rdj)!=8)putchar('*');/输出一个字符elseif (j>0)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;i<ucount;i+)if (str
43、cmp(strupr(ufdi->ufdname),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;i<fcountj;i+)if (strcmp(strupr(ufdj->ufdfilei.fname),strupr(filename)=0)ex
44、ist=1;break;if (exist) return(i);else return(-1);int FindPANo() /*find out physical address num*/int i;for(i=0;i<MAX;i+)if (fpaddrnoi=0) fpaddrnoi=1;break;if (i<MAX) return(i);else return(-1);int WriteF1() /*write file*/int length=0;char c;printf("Please input text('#' stands for
45、end):n");while(c=getchar()!='#')fprintf(fp_file,"%c",c);if (c!='n') length+;fprintf(fp_file,"n");fclose(fp_file);return(length);void LoginF() /*LOGIN FileSystem登陆函数*/char loginameMAXNAME,loginpw9,logincpw9,str50;/loginpw-输入密码,logincpw-确认输入密码,str50存放地址int i,j,
46、flag=1;char a25;int findout; /*login user not exist*/while(1)findout=0;printf("nnLogin Name:");gets(loginame);/把用户名写入系统ltrim(rtrim(loginame);fp_mfd=fopen("d:osfilemfd.txt","rb");/打开文件mfdfor(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i+)if (strcmp(strupr(ufd_
47、lp.ufdname),strupr(loginame)=0)/两者相等findout=1;strcpy(logincpw,ufd_lp.ufdpword);fclose(fp_mfd);/关闭mfd文件if (findout=1) /*user exist*/该用户存在printf("Login Password:");/用户密码输入InputPW(loginpw); /*input password,use '*' replace*/if (strcmp(loginpw,logincpw)=0)/相等strcpy(username,strupr(logi
48、name);strcpy(dirname,username);fp_mfd=fopen("d:osfilemfd.txt","rb");for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j+)/记录用户名strcpy(str,"d:osfile");strcat(str,ufd_lp.ufdname);/strcat(dest,char)添加char到dest结尾处strcat(str,".txt");ufdj=(OSUFD*)malloc(siz
49、eof(OSUFD);/malloc向系统申请分配指定size个字节的内存空间分配二级文件空间strcpy(ufdj->ufdname,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);uc
50、ount=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;
51、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(<=8):"); /*input new password,use '*' replace*/InputPW(logincpw);if (strcmp(loginpw,logincpw)=0)/相等
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 水产养殖基地土地使用权合同
- 公司技术服务采购合同
- 豪华酒店厨师服务合同
- 电子产品购销合同标准版
- 房地产投资专项法律服务合同
- (完整版)农村土地租赁合同书
- 光学玻璃的紫外光固化涂层技术考核试卷
- 医疗用品行业服务平台拓展考核试卷
- 搪瓷原材料市场动态与价格趋势考核试卷
- 数字出版物的长期保存与数字遗产考核试卷
- 《汽车专业英语》2024年课程标准(含课程思政设计)
- 部编四年级道德与法治下册全册教案(含反思)
- AutoCAD 2020中文版从入门到精通(标准版)
- 烟草栽培(二级)鉴定理论考试复习题库-上(单选题汇总)
- 散文课堂教学评价重点标准
- 桥梁钢筋加工安装
- 动物生物化学(全套577PPT课件)
- 中国传统二十四节气立春节气介绍PPT模板课件
- 个人简历求职竞聘自我介绍PPT模板课件
- 活性炭生产工艺流程图
- 重量分析法实验
评论
0/150
提交评论