当前位置:文档之家› 操作系统课程设计二级文件系统

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

操作系统课程设计报告专业:计算机信息处理学号:09103408姓名:纪旻材提交日期:2011-12-28【设计目的】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 ofmfdname,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-WriteOnly;2-Read and Write; 3-Protect;*/char fname[MAXNAME]; /*file name*/} OSFILE;typedef struct/*the structure of OSUFD定义用户文件目录*/{char ufdname[MAXNAME]; /*ufd name*/OSFILE ufdfile[MAXCHILD]; /*ufd own file*/}OSUFD;typedef struct/*the structure of OSUFD'LOGIN定义登陆*/{char ufdname[MAXNAME]; /*ufd name*/char ufdpword[8]; /*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 fname[MAXNAME],str[50],str1[50];int i,k,j;int fpaddrno1;if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) /*无法删除主目录的文件*/{printf("\ convert to ufd dir before delete.\n");wgetchar=1;}if (strcmp(strupr(dirname),strupr(username))!=0) /*无法删除非自己目录的文件*/{printf("\ can only modify filemode in yourself dir.\n");wgetchar=1;}else{printf("\nPlease input FileName:");gets(fname);fopen==1)/*文件打开时无法删除*/{printf("\nError.\'%s\' is in open status. Close it before delete.\n",fname);wgetchar=1;}else{if(ufd[k]->ufdfile[i].fmode==3)/*被保护的文件无法删除*/{printf("\nError.\'%s\' is in protect status. Close it before delete.\n",fname);wgetchar=1;}else{fpaddrno1=ufd[k]->ufdfile[i].fpaddr;xt");remove(str);n",fname);wgetchar=1;}}}else\'%s\' dose not exist.\n",fname);wgetchar=1;}}}2 Open函数void OpenF() /*Open File*/{char fname[MAXNAME];int i,k,j;if (strcmp(strupr(dirname),strupr(username))!=0) /*在自己的目录里才能打开*/{printf("\ can only open in yourself dir.\n");wgetchar=1;}else{k=ExistD(username);for(j=0;j<fcount[k];j++){printf("%15s",ufd[k]->ufdfile[j].fname);}printf("\nPlease input FileName:");gets(fname);fopen==1)'%s\' is in openstatus.\n",fname);wgetchar=1;}else{ifopen[k][i].ifopen=1;mode==0)/*根据文件的模式设置打开模式*/{ ifopen[k][i].openmode=0;}else if(ufd[k]->ufdfile[i].fmode==1){ifopen[k][i].openmode=1;}else if(ufd[k]->ufdfile[i].fmode==2){ifopen[k][i].openmode=2;}else ifopen[k][i].openmode=3;printf("\n\'%s\' is openedsuccessfully\n",fname);wgetchar=1;}}else\'%s\' dose not exist.\n",fname);wgetchar=1;}}}3 Close函数void CloseF() /*Close File*/{char fname[MAXNAME];int i,k,j;if (strcmp(strupr(dirname),strupr(username))!=0) /*不在自己的目录里没法进行*/{printf("\ can only close file in yourself dir.\n");wgetchar=1;}else{k=ExistD(username);for(j=0;j<fcount[k];j++)/*列出已经打开的文件*/ {if(ifopen[k][j].ifopen==1)name);}printf("\nPlease input FileName:");gets(fname);fopen=0;/*关闭文件*/printf("\n \'%s\' closed successfully\n",fname);wgetchar=1;}else\'%s\' dose not exist.\n",fname);wgetchar=1;}}}4 Write函数void WriteF() /*Write File*/{int i,k,n=0;int length;char fname[MAXNAME],values[1000];char str[255],str1[255],c;if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) n");wgetchar=1;return;}printf("\nCaution:Open file first\n");printf("Opened File(s) List:\n");k=ExistD(dirname);fopen==1)name);n++;}if((n%4==0)&&(n!=0)) printf("\n");}printf("\n%d files openned.\n",n);if (n==0) wgetchar=1;fopen==1)penmode==1)||(ifopen[k][i].openmode==2))paddr,str,10);xt");fp_file=fopen(str,"ab");length=ufd[k]->ufdfile[i].flengt h+length;n",ufd[k]->ufdfile[i].flength);printf("\n\nYou have write filesuccessfully!!");fclose(fp_file);wgetchar=0;}else if(ifopen[k][i].openmode==0)'%s\' has been opened with READ ONLY mode. It isn\'t write.\n",fname);wgetchar=1;}else'%s\' has been opened with PROTECT mode. It isn\'t write.\n",fname);wgetchar=1;}}else '%s\' is in closing status. Please open it before write\n",fname);wgetchar=1;}}else \'%s\' does not exist.\n",fname);wgetchar=1;}}}【程序流程设计】1总的功能结构图:2部分子模块程序流程图(1)打开命令的程序流程图:(2)关闭命令的程序流程图:(3)写命令的程序流程图:(4)删除命令的程序流程图:【测试结果】1 删除文件2 打开的文件不能删除3 打开文件,其中已经打开的文件不能再次打开3 关闭文件4 写文件,其中只有打开了文件才能写入5 写文件6 只读文件和保护文件不能写入7 其他函数【参考文献】计算机操作系统,西安电子科技大学出版社,方敏主编,部分函数含义引用于【源程序清单】#include""#include""#include""#include""#define MAXNAME 25 /*the largest length ofmfdname,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-WriteOnly;2-Read and Write; 3-Protect;*/char fname[MAXNAME]; /*file name*/} OSFILE;typedef struct/*the structure of OSUFD定义用户文件目录*/{char ufdname[MAXNAME]; /*ufd name*/OSFILE ufdfile[MAXCHILD]; /*ufd own file*/}OSUFD;typedef struct/*the structure of OSUFD'LOGIN定义登陆*/ {char ufdname[MAXNAME]; /*ufd name*/char ufdpword[8]; /*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 DirNameExist,Exist-i,Not Exist-0*/int WriteF1(); /*write file*/int ExistF(char *filename); /*Whether FileNameExist,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 mfd's ufds用户数*/int fcount[MAXCHILD]; /*the count of ufd's files子文件数*/ int loginsuc=0; /*whether login successfully登陆成功*/char username[MAXNAME]; /*record login user's name22用户名*/char dirname[MAXNAME];/*record current directory使用的用户目int fpaddrno[MAX]; /*record file physical address num物理地址号,存放自己所创建的所有文件的地址*/int wgetchar; /*whether getchar()*/OSUFD *ufd[MAXCHILD]; /*ufd and ufd own files*/OSUFD_LOGIN ufd_lp;;QuitF();exit(0);break;case 10:clrscr();choiceend=1;break;case 11:CdF();choiceend=1;break;;}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) str[0]='\0';return str;}char *ltrim(char *str) /*remove the heading blanks.去掉登陆用户名的头空格*/{strrev(str);name),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;i<MAX;i++)if (fpaddrno[i]==0){fpaddrno[i]=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 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登陆函数*/{charloginame[MAXNAME],loginpw[9],logincpw[9],str[50];xt");ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));fopen=0;ifopen[j][i].openmode=4;}fclose(fp_ufd);}fclose(fp_mfd);ucount=j;SetPANo(0);printf("\n\nLogin successful! Welcome to this FileSystem\n\n");loginsuc=1;return;}else 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(<=8):"); /*input new password,use '*' replace*/InputPW(logincpw);if (strcmp(loginpw,logincpw)==0)xt");if((fp_ufd=fopen(str,"rb"))==NULL){fp_ufd=fopen(str,"wb");fclose(fp_ufd);}fp_mfd=fopen("d:\\osfile\\","rb");for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j ++){strcpy(str,"d:\\osfile\\");strcat(str,;strcat(str,".txt");ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));strcpy(ufd[j]->ufdname,strupr);fp_ufd=fopen(str,"rb");for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_uf d)!=0;i++,fcount[j]++)fopen=0;ifopen[j][i].openmode=4;}fclose(fp_ufd);}fclose(fp_mfd);ucount=j;SetPANo(0);printf("\n\nLogin Successful! Welcome to this System\n\n");loginsuc=1;return;}else 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 sfmode[25],sfpaddr[25],str[25];clrscr();if (strcmp(strupr(ltrim(rtrim(dirname))),"")!=0)");getch();clrscr();printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddres s","FileLength","Type","FileMode");}itoa(ufd[j]->ufdfile[i].fpaddr,str,10);mode==0) strcpy(sfmode,"Read Only");else if(ufd[j]->ufdfile[i].fmode==1)strcpy(sfmode,"Write Only");elseif(ufd[j]->ufdfile[i].fmode==2)strcpy(sfmode,"Read And Write");else strcpy(sfmode,"Protect");printf("%14s%16s%14d%10s%18s\n",ufd[j]->ufdfile[i].fname ,sfpaddr,ufd[j]->ufdfile[i].flength,"<FILE>",sfmode);}printf("\n %3d file(s)\n",fcount[j]);}else.");getch();clrscr();printf("\n%14s%18s%8s\n","DirName","OwnFileCount","Type" );}printf("%14s%18d%8s\n",ufd[i]->ufdname,fcount[i],"<UFD>" );count=count+fcount[i];}printf("\n %3d dir(s),%5d file(s)\n",ucount,count);}}void CreateF() /*Create File*/{int fpaddrno,flag=1,i;char fname[MAXNAME],str[50],str1[50],a[25];char fmode[25];if (strcmp(strupr(dirname),strupr(username))!=0)You must create file in your own dir.\n");wgetchar=1;}else{printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));if (ExistF(fname)>=0){printf("\nError. Name \'%s\' has alreadyexisted.\n",fname);wgetchar=1;}else{printf("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)||(strc mp(fmode,"2")==0)||(strcmp(fmode,"3")==0)){fpaddrno=FindPANo();if (fpaddrno>=0)name,fname);paddr=fpaddrno;ufd[i]->ufdfile[fcount[i]].fmode=atoi(fmode);ifopen[i][fcount[i]].ifopen=0;ifopen[i][fcount[i]].openmode=4;strcpy(str,"d:\\osfile\\file\\file");itoa(fpaddrno,str1,10);strcat(str,str1);strcat(str,".txt");fp_file=fopen(str,"wb");fclose(fp_file);fcount[i]++;while(flag){printf("Input text now(Y/N):");gets(a);ltrim(rtrim(a));ufd[i]->ufdfile[fcount[i]-1].flength=0;if(strcmp(strupr(a),"Y")==0){fp_file=fopen(str,"wb+");length=WriteF1();flag=0;}else if(strcmp(strupr(a),"N")==0){flag=0;wgetchar=1;}}printf("\n\'%s\' has been created successfully!\n",fname);}else{printf("\nFail!No Disk Space. Please format your disk.\n");wgetchar=1;}}else{printf("\nError. FileMode\'s Range is 0-3\n");wgetchar=1;}}}}。

相关主题