操作系统课程设计基于Linux的模拟文件系统的设计与实现.doc
中北大学操作系统课程设计说 明 书 学 院、系:软件学院专 业:软件工程学 生 姓 名:学 号:设 计 题 目:基于Linux的模拟文件系统的设计与实现 起 迄 日 期:2013年11月22日- 2013年12月6日指 导 教 师: 2013 年 12月 6 日1.需求分析(1)设计目的:文件管理是操作系统的重要功能之一。用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程。从而加深学生对文件系统内部功能和内部实现的理解和认识。(2)基本功能:主要包括:改变目录(CD),创建目录(MD),显示目录(DIR),删除目录(RD),打开全部文件(openall),打开单个文件(open),建立一个文件(create),删除一个文件(delete),写文件(write),读文件(read),改文件的保护码(change),退出(exit)等。(3)设计要求:设计一个10个用户的文件系统。每个用户最多可以保存10个文件,一次运行用户可打开多个文件。程序采用二级文件目录。(即设置主目录(MFD)和用户文件目录(UFD)。另外,可打开文件设置指针。为了方便实现,对文件的读写作了简化。在执行读写命令时,只需改读写指针。并不进行实际的读写操作。2.总体设计 2.1数据结构struck file char name10; int code;/保护码 int size; char pwrite;/读写指针 ; struct user char name10; struct file *list;/目录指针 ; 2.2主要函数声明void running(); void delet(); void write(); void open(); void close(); void bye(); void read(); void create(); void input(); 2.3模块结构图 3.详细设计3.1 模块及功能说明3.1.1 input()模块功能:进入文件系统。3.1.2 running()模块功能:选择相应的操作。3.1.3 bye()模块功能:退出文件系统函数bye()。3.1.4 create()模块功能:创建文件。3.1.5 open()模块功能:打开文件。3.1.6 close()模块功能:关闭文件。3.1.7 write()模块功能:写文件(需要先打开文件)。3.1.8 read()模块功能:读文件(需要先打开文件)。3.1.9 delet()模块功能:删除文件。3.2 模块代码3.2.1 main()模块int main() input(); running(); return 0; 3.2.2 input()模块void input() char name; int i; printf("n*n"); printf(" Please choose the following username(input digital):n"); printf("1.one 2.two 3.three 4.four 5.fine 6.six 7.seven 8.eigh 9.nine 0.ten n"); printf("*nn"); printf("Please input username:"); scanf("%s",&name); switch(name) case '1': for(i=0;i<10;i+)UFDi=onei;MFD0=use1;running();break; case '2': for(i=0;i<10;i+)UFDi=twoi;MFD0=use1;running();break; case '3': for(i=0;i<10;i+)UFDi=threei;MFD0=use2;running();break; case '4': for(i=0;i<10;i+)UFDi=fouri;MFD0=use3;running();break; case '5': for(i=0;i<10;i+)UFDi=finei;MFD0=use4;running();break; case '6': for(i=0;i<10;i+)UFDi=sixi;MFD0=use5;running();break; case '7': for(i=0;i<10;i+)UFDi=seveni;MFD0=use6;running();break; case '8': for(i=0;i<10;i+)UFDi=eighti;MFD0=use7;running();break; case '9': for(i=0;i<10;i+)UFDi=ninei;MFD0=use8;running();break; case '0': for(i=0;i<10;i+)UFDi=teni;MFD0=use9;running();break; default : printf("Enter the user name wrong!n"); input(); 3.2.3 running()模块void running() char order; int i; printf("n*n"); printf("The user all the files below:n"); for(i=0;i<10;i+) printf("%s",UFDi.name); printf("t"); printf("n*n"); printf(" Choose option(input code):n"); printf("c-create d-delete o-open s-close r-read w-write b-bye t.exit(Re-election user)n"); printf("n*n"); printf("Input command:n"); scanf("%s",&order); switch(order) case 'c' : create();break; case 'd' : delet();break; case 'o' : open();break; case 's' : close();break; case 'r' : read();break; case 'w' : write();break; case 'b' : bye();break; case 't' : input();break; default : printf("Input error!nn"); running(); 4.心得体会操作系统是计算机科学与技术专业中的一门重要基础课,目的是让我们了解操作系统的基本概念,理解计算机系统的资源如何组织,操作系统如何有效地管理这些系统资源,用户如何通过操作系统与计算机系统打交道。通过课程设计,可以进一步理解在计算机系统上运行的其它各类操作系统,并懂得在操作系统的支持下建立自己的应用系统。操作系统课程设计,对于训练我们掌握程序设计、熟悉上机操作和程序调试技术都有重要作用。重点培养我们的思维能力、创新能力和排错能力。通过课程设计,进一步融会贯通教材内容,掌握程序各功能模块的工作原理,相互联系和来龙去脉,完整地建立系统的概念。激发我的学习热情和主动性,培养我的独立工作能力,在实践活动中,将所学知识综合运用,增长才干,并积累经验。对编程语言又有了更深一层次的认识,并对编程能力有所加强,但还是很多的不足有待改进,对于编程来说还需要大量的实践,才能在发现问题和解决问题中前进与提高。5.附录:源代码#include <stdlib.h>#include <stdio.h> #include <string.h> #define NULL 0 int count=0; struct file char name10; int code; int size; char pwrite; ; struct user char name10; struct file *list; ; struct file one10="a1",0,10,0,"a2",2,10,0,"a3",1,10,0, "a4",2,10,0,"a5",2,10,0,"a6",2,10,0,"a7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file two10="b1",0,10,0,"b2",2,10,0,"b3",1,10,0, "b4",2,10,0,"b5",2,10,0,"b6",2,10,0,"b7",2,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file three10="c1",0,10,0,"c2",2,10,0,"c3",1,10,0, "c4",2,10,0,"c5",2,10,0,"c6",2,10,0,"c7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file four10="d1",0,10,0,"d2",2,10,0,"d3",1,10,0, "d4",2,10,0,"d5",2,10,0,"d6",2,10,0,"d7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file fine10="e1",0,10,0,"e2",2,10,0,"e3",1,10,0, "e4",2,10,0,"e5",2,10,0,"e6",2,10,0,"e7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file six10="f1",0,10,0,"f2",2,10,0,"f3",1,10,0, "f4",2,10,0,"f5",2,10,0,"f6",2,10,0,"f7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file seven10="g1",0,10,0,"g2",2,10,0,"g3",1,10,0, "g4",2,10,0,"g5",2,10,0,"g6",2,10,0,"g7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file eight10="h1",0,10,0,"h2",2,10,0,"h3",1,10,0, "h4",2,10,0,"h5",2,10,0,"h6",2,10,0,"h7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file nine10="j1",0,10,0,"j2",2,10,0,"j3",1,10,0, "j4",2,10,0,"j5",2,10,0,"j6",2,10,0,"j7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file ten10="k1",0,10,0,"k2",2,10,0,"k3",1,10,0, "k4",2,10,0,"k5",2,10,0,"k6",2,10,0,"k7",1,10,0,"0",0,0,0, "0",0,0,0,"0",0,0,0; struct file UFD10; struct file AFD5="0",0,0,0,"0",0,0,0,"0",0,0,0,"0",0,0,0,"0",0,0,0; struct user use10="number1",one,"number2",two,"number3",three,"number4",four, "number5",fine,"number6",six,"number7",seven,"number8",eight,"number9",nine, "number10",ten; struct user MFD1="0",0; void running(); void delet(); void write(); void open(); void close(); void bye(); void read(); void create(); void input(); int main() input(); running(); return 0; void running() char order; int i; printf("n*n"); printf("The user all the files below:n"); for(i=0;i<10;i+) printf("%s",UFDi.name); printf("t"); printf("n*n"); printf(" Choose option<input code>: n"); printf("c-create d-delete o-open s-close r-read w-write b-bye t.exit<Re-election user> n"); printf("n*n"); printf("Input command:n"); scanf("%s",&order); switch(order) case 'c' : create();break; case 'd' : delet();break; case 'o' : open();break; case 's' : close();break; case 'r' : read();break; case 'w' : write();break; case 'b' : bye();break; case 't' : input();break; default : printf("Input error!nn");running(); void delet() char name10; int i,flag; do printf("Input file name: nn"); scanf("%s",&name); flag=0; for(i=0;i<10;i+) if(strcmp(UFDi.name,name) flag=1; else flag=0; strcpy(UFDi.name,"0"); UFDi.code=0; UFDi.pwrite=0; UFDi.size=0; printf("Delete success!n"); break; if(flag=1) printf("Without this file,please enter again!n"); while(flag=1); printf("n"); running(); void open() char name10; int i,j,flag; if(count=5) printf("Run queue is full!n"); running(); else do printf("Input file name:n"); scanf("%s",&name); flag=0; for(i=0;i<10;i+) if(strcmp(UFDi.name,name) flag=1; else flag=0; break; if(flag=1) printf("Without this file,please enter again!n"); while(flag=1); for(j=0;j<5;j+) if(strcmp(AFDj.name,name) flag=1; else flag=0; break; if(flag=1) for(j=0;j<5;j+) if(!strcmp(AFDj.name,"0") AFDj=UFDi; printf("The file has been opend!n"); count+; break; else printf("The file has been opended in the queue!nn"); running(); void close() char name10; int j,flag; printf("Input file name: n"); scanf("%s",&name); flag=0; for(j=0;j<5;j+) if(strcmp(AFDj.name,name) flag=1; else flag=0; strcpy(AFDj.name,"0"); AFDj.code=0; AFDj.pwrite=0; AFDj.size=0; printf("The file has been closed in the run queue!n"); break; if(flag=1) printf("Run queue without the open file ,please operate again!nn"); running(); void bye() int i,j; for(i=0;i<5;i+) for(j=0;j<10;j+) if(!strcmp(AFDi.name,UFDj.name) UFDi=AFDi; break; printf("The saved file directory!n"); printf("End of the operation!n"); exit(0); void read() char name10; int j,flag=0; printf("Input file name: n"); scanf("%s",&name); for(j=0;j<5;j+) if(!strcmp(AFDj.name,name) flag=1; break; if(flag=1) if(AFDj.code=0|AFDj.code=2) AFDj.pwrite='R' printf("The state of the files are read!n"); else printf("No permission to write this file!n"); else printf("Run queue without this file,please open the file first!n"); printf("n"); running(); void write() char name10; int j,flag=0; printf("Input file name: n"); scanf("%s",&name); for(j=0;j<5;j+) if(!strcmp(AFDj.name,name) flag=1; break; if(flag=1) if(AFDj.code=1|AFDj.code=2) AFDj.pwrite='W' printf("The state of the files are write!n"); else printf("No permission to write this file!n"); else printf("Run queue without this file,please open the file first!n"); printf("n"); running(); void create() char name10; int i,j=0,flag; for(i=0;i<10;i+) if(!strcmp(UFDi.name,"0") j+; if(j>9) printf("The folder is full,please delete!n"); running(); else do printf("Input file name; n"); scanf("%s",&name); flag=0; for(i=0;i<10;i+) if(!strcmp(UFDi.name,name) printf("File name duplication,please enter again!n"); flag=1; break; while(flag=1); for(i=0;i<10;i+) if(!strcmp(UFDi.name,"0") strcpy(UFDi.name,name); do printf("Please enter the protected codechoose0-2,0-Read 1-Write 2-Read or Write:n"); scanf("%d",&UFDi.code); if(UFDi.code!=0&&UFDi.code!=1&&UFDi.code!=2) printf("Input error,input protection code again!