1、专 业:软件工程学 号:11103303姓 名:程路峰提交日期:2014/1/2操作系统课程设计报告操作系统课程设计报告 11103303 程路峰1【设计目的】1、本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。2、结合数据结构、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。【设计内容】为 Linux 系统设计一个简单的二级文件系统。要求做到以下几点:可以实现下列几条命令:login 用户登录dir 列目录Attrib 修改文件属性create 创建文件delete 删除文件open 打开文件close 关闭文件read 读文
2、件write 写文件cd 进出目录列目录时要列出文件名,物理地址,保护码和文件长度源文件可以进行读写保护.【实验环境】Windows7 操作平台Visual Studio2010【相关知识综述】理解二级目录的文件系统的组织;掌握常用的数据结构;系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户帐号下的文件;使用文件来模拟外存,进行数据结构设计和操作算法的设计,实现一个文件系统并实现基本的文件操作(为了简便文件系统,不考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容) 。【设计思路】采用的数据结构、主要的函数说明、程序流程设计等本文件系统采用两级目录,其中第一级对应于用户账号
3、,第二级对应于用户帐号下的文件。另外,为了简便文件系统未考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容。首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2并以编号作为物理地址,在目录中进行登记1.主要的数据结构操作系统课程设计报告 11103303 程路峰2#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename 表示三种文件的长度都为 25*/#define M
4、AXCHILD 50 /*the largest child 每个用户下可以有 50 个文件*/#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno 定义一个常量 2500 个扇区*/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 Writ
5、e; 3-Protect;*/char fnameMAXNAME; /*file name 文件名*/ OSFILE;typedef struct /*the structure of OSUFD*/char ufdnameMAXNAME; /*ufd name*/OSFILE ufdfileMAXCHILD; /*ufd own file*/OSUFD;/*osf 文件的数据结构*/typedef struct /*the structure of OSUFDLOGIN*/char ufdnameMAXNAME; /*ufd name*/char ufdpword8; /*ufd passw
6、ord*/ 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 CdF(); /*Change Dir*/void CreateF(); /*Create File*
7、/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 help();3总体功能程序结构图操作系统课程设计报告 11103303 程路峰3打开命令的程序流程图 关闭命令的程序流程图操作系统课程设计报告 11
8、103303 程路峰4写命令程序流程图 删除命令的程序流程图:操作系统课程设计报告 11103303 程路峰5【部分源程序清单】打开函数void OpenF() int fc,i;char fnameMAXNAME,fmode25;int fm;printf(“nPlease input FileName:“);gets(fname);ltrim(rtrim(fname);if (ExistF(fname)ufdfilea.fname)=0)fc=a;break;ifopenifc.ifopen=1;printf(“Please input OpenMode(0-Read Only, 1-Wr
9、ite Only, 2-Read and Write, 3-Protect):“);gets(fmode);fm=atoi(fmode);ifopenifc.openmode=fm;printf(“nOpen successed“);wgetchar=1;关闭函数void CloseF() char fnameMAXNAME;int i,k;操作系统课程设计报告 11103303 程路峰6if (strcmp(strupr(dirname),strupr(username)!=0) printf(“nError.You can only modify filemode in yourself
10、dir.n“);elseprintf(“nPlease input FileName:“);gets(fname);ltrim(rtrim(fname); i=ExistF(fname); if (i=0) k=ExistD(username); if(ifopenki.ifopen=0) printf(“nError. %s has been closed. you can not close it again.n“,fname);else ifopenki.ifopen=0; ifopenki.openmode=4; printf(“%s has been closed successfu
11、lly!“,fname);else printf(“nError. %s dose not exist.n“,fname);删除函数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“);操作系统课程设计报告 11103303 程路峰7wgetchar=1;if (strcmp
12、(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 d
13、elete.n“,fname); wgetchar=1;elseif(ufdk-ufdfilei.fmode=3)操作系统课程设计报告 11103303 程路峰8printf(“nError.%s is in protect status. Close it before delete.n“,fname); wgetchar=1;elsefpaddrno1=ufdk-ufdfilei.fpaddr;fpaddrnofpaddrno1=0;for(j=i;jufdfilej=ufdk-ufdfilej+1;strcpy(str,“d:osfilefilefile“);itoa(fpaddrno1
14、,str1,10);strcat(str,str1);strcat(str,“.txt“);remove(str);fcountk-;printf(“n %sis deleted successfully.n“,fname);wgetchar=1;else操作系统课程设计报告 11103303 程路峰9printf(“nError. %s dose not exist.n“,fname);wgetchar=1;写入函数void WriteF() int i,k,m=0;int length;char fnameMAXNAME;char str255,str1255;if (strcmp(str
15、upr(dirname),strupr(username)!=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);m+;if(m%4=0)printf(“n%d files openned.n“,m);if (m=0) wgetchar=1;if(m!=0)操作系统课程设
16、计报告 11103303 程路峰10printf(“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,“c:osfilefile“);strcat(str,str1);strcat(str,“
17、.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
18、. It isnt write.n“,fname);wgetchar=1;elseprintf(“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;操作系统课程设计报告 11103303 程路峰11else printf(“nError. %s does not exist.n“,fname);wget
19、char=1;【测试结果】登录用户操作系统课程设计报告 11103303 程路峰12创建文件:打开文件:关闭文件:操作系统课程设计报告 11103303 程路峰13第一次读文件(查看原文件内容):操作系统课程设计报告 11103303 程路峰14写文件:再次读取文件(查看是否写入成功)列出目录和帮助:操作系统课程设计报告 11103303 程路峰15删除文件:操作系统课程设计报告 11103303 程路峰16【设计总结】【源程序】#include “stdio.h“#include “string.h“#include “conio.h“#include “stdlib.h“#define M
20、AXNAME 25 /*the largest length of mfdname,ufdname,filename*/#define MAXCHILD 50 /*the largest child*/#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/typedef struct /*the structure of OSFILE*/int fpaddr; /*file physical address*/int flength; /*file length*/操作系统课程设计报告 11103303 程路峰17int fmode;
21、/*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
22、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;OSUFD *ufdMAXCHILD; /*ufd and ufd own files*/OSUFD_LOGIN ufd_lp;int ucount=0; /*the count of mf
23、ds 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*/OSUFD_OPENMODE ifopenMAXCHILDMAXCHILD; /*recor
24、d file open/close*/int wgetchar; /*whether getchar()*/FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file;void LoginF(); /*LOGIN FileSystem*/void DirF(); /*Dir FileSystem*/void CdF(); /*Change Dir*/void CreateF(); /*Create File*/void DeleteF(); /*Delete File*/void ModifyFM(); /*Modify FileMode*/void OpenF(); /
25、*Open File*/void CloseF(); /*Close File*/操作系统课程设计报告 11103303 程路峰18void ReadF(); /*Read File*/void WriteF(); /*Write File*/void QuitF(); /*Quit FileSystem*/void help();char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/void InputPW(char *passw
26、ord); /*input password,use * replace*/void SetPANo(int RorW); /*Set physical address num*/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*/int FindPANo(); /*find out physical
27、 address num*/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*/char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/if(
28、fp_mfd=fopen(“c:osfilemfd.txt“,“rb“)=NULL)fp_mfd=fopen(“c:osfilemfd.txt“,“wb“);fclose(fp_mfd);for(i=0;i“,strupr(dirname);else printf(“Bad command or file name.nC:%s“,strupr(username);gets(choice);strcpy(choice,ltrim(rtrim(strlwr(choice);if (strcmp(choice,“dir“)=0) choice1=1;else if(strcmp(choice,“cr
29、eate“)=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(c
30、hoice,“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();choic
31、eend=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;i
32、f (!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.“);操作系统课程设计报告 11103303 程路峰20void help(void)printf(“nT
33、he Command Listn“);printf(“n 改变目录-Cd 修改文件属性Attrib 新建文件Create 写文件write 读文件Read 打开文件Open 清屏Cls 删除Delete 退出Exit 关闭文件Closen“);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 (nufdname,strupr(ufd_lp.ufdname);fp_ufd=fopen(str
34、,“rb“);fcountj=0;for(i=0;fread(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;操作系统课程设计报告 11103303 程路峰22elseprintf(“nn“);flag=1;while(flag)printf(“Login Failed! Password E
35、rror. 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;elseprintf(“New Password(ufdname,strupr(ufd_lp.ufdname);fp_ufd=fopen(str,“rb“);for(i=0;fread(i+,fcountj+)ifopenji.ifopen=0;ifopenji.openmode=4;fclose
36、(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;操作系统课程设计报告 11103303 程路
37、峰24else if(strcmp(strupr(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_file_p=fopen(“c:osfilefilefile_p.txt“,“rb“)=NULL)fp_file_p=fopen(“c:osfilefilefile_p.txt“,“wb“);fclose(fp_file_p);fp_file_p=fopen(“c:osfilefilefile_p
38、.txt“,“rb“);/for(i=0;fread(i+)fpaddrnoj=1;/*for(i=1;i0)j-;j-;putchar(b);putchar( );putchar(b);else j-;else passwordj=0;break;passwordj=0;void DirF() /*Dir FileSystem*/int i,j,count=0;char sfmode25,sfpaddr25,str25;clrscr();if (strcmp(strupr(ltrim(rtrim(dirname),“)!=0)printf(“nnC:%sdirn“,dirname);prin
39、tf(“n%14s%16s%14s%10s%18sn“,“FileName“,“FileAddress“,“FileLength“,“Type“,“FileMode“);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(
40、ufdj-ufdfilei.fmode=2)strcpy(sfmode,“Read And Write“);else strcpy(sfmode,“Protect“);printf(“%14s%16s%14d%10s%18sn“,ufdj-ufdfilei.fname,sfpaddr,ufdj-ufdfilei.flength,“,sfmode);printf(“n %3d file(s)n“,fcountj);elseprintf(“nnC:dirn“);printf(“n%14s%18s%8sn“,“DirName“,“OwnFileCount“,“Type“);for(i=0;iufdn
41、ame,fcounti,“);count=count+fcounti;printf(“n %3d dir(s),%5d file(s)n“,ucount,count);int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/操作系统课程设计报告 11103303 程路峰27int i;int exist=0;for(i=0;iufdname),strupr(dirname)=0)exist=1;break;if (exist) return(i);else return(-1);void CdF() /*Ex
42、change Dir*/char dnameMAXNAME;printf(“nPlease input DirName (cd-Previous dir; DirNAME-cd DirNAME):“);gets(dname);ltrim(rtrim(dname);if (ExistD(dname)=0) strcpy(dirname,strupr(dname);else if(strcmp(strupr(dname),“CD“)=0) strcpy(ltrim(rtrim(dirname),“);else printf(“nError.%s does not exist.n“,dname);v
43、oid CreateF() /*Create File*/int fpaddrno,flag=1,i;char fnameMAXNAME,str50,str150,a25;char fmode25;if (strcmp(strupr(dirname),strupr(username)!=0)printf(“nError. You must create file in your own dir.n“);wgetchar=1;elseprintf(“nPlease input FileName:“);gets(fname);ltrim(rtrim(fname);if (ExistF(fname)
44、=0)操作系统课程设计报告 11103303 程路峰28printf(“nError. Name %s has already existed.n“,fname);wgetchar=1;elseprintf(“Please input FileMode(0-Read Only, 1-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,“
45、3“)=0)fpaddrno=FindPANo();if (fpaddrno=0)i=ExistD(username);strcpy(ufdi-ufdfilefcounti.fname,fname);ufdi-ufdfilefcounti.fpaddr=fpaddrno;ufdi-ufdfilefcounti.fmode=atoi(fmode);ifopenifcounti.ifopen=0;ifopenifcounti.openmode=4;strcpy(str,“c:osfilefilefile“);itoa(fpaddrno,str1,10);strcat(str,str1);strca
46、t(str,“.txt“);fp_file=fopen(str,“wb“);fclose(fp_file);fcounti+;while(flag)printf(“Input text now(Y/N):“);gets(a);ltrim(rtrim(a);ufdi-ufdfilefcounti-1.flength=0;if(strcmp(strupr(a),“Y“)=0)fp_file=fopen(str,“wb+“);ufdi-ufdfilefcounti-1.flength=WriteF1();flag=0;else if(strcmp(strupr(a),“N“)=0)操作系统课程设计报告 11103303 程路峰29flag=0;wgetchar=1;printf(“n%s has been created successfully!n“,fname);elseprintf(“nFail!No Disk Space. Please format your disk.n“);wgetchar=1;else printf(“nError. FileModes Range is 0