《操作系统原理与Linux》课程设计报告

一、设计题目模拟unix文件系统的设计与实现二、设计目的:本课程设计是学习完《操作系统原理》课程后,进行的一次全面的综合训练,通过课程设计,更好地掌握操作系统的原理及实现方法,加深对操作系统基础理论和重要算法的理解,加强动手能力。通过本课程设计掌握文件系统的工作原理,理解文件系统的主要数据结构。学习较为复杂的Linux或windows下的编程。三、设计要求1、设计一个简单的文件系统,包含格式化、显示文件(目录)、创建文件、登录等几个简单命令的实现,而且能完成超级块的读写、节点的读写等过程。编写一个基本体现文件系统理论的程序。在节点的使用上,主要模仿Linux的EXT2文件系统。2.程序流程参考如下:四、设计思想说明通过c++模拟Linux文件系统,利用系统内存分成文件系统的可用块数。设计用户结构体存放用户系统,创建文件结构体存放文件信息,通过命令可以对创建、删除、登陆用户,对文件有创建、删除、更改、读、写等操作。在存储文件时使用位矢图法。五、文件系统结构的说明六、数据结构的说明//文件或目录struct file{ struct file * parent;//父块指针 struct file * boy;//子块指针 struct file * hou;//同级的前一个指针 struct file * qian;//同级的后一个指针 char name[9];//文件、目录名 char text[disks];//文件内容 int way; int kj; };//用户struct user{ char username[unsize];//用户名 char usepassword[passize];//用户密码 struct file * fumenu;//链表组指针 };struct kz{ struct file * diz; int free; }; struct user yhbiao[usercounts]; struct kz disk[disks]; int yhcount=0; int dangqianyh; int kb; struct file * now; struct file * now2; char iput[20]; char iput2[20]; int over; char load[100];七、程序清单:#include #include #include #define disks 512//定义盘块数#define usercounts 8#define unsize 11#define passize 7extern int begin();extern void command();extern void clearbuf();extern int commandjiexi();extern void files(char *name,int kb);extern void menu(char *name,int kb);extern void fileliebiao();extern void menugaibian(char name[10]);extern int rol(int kb);//文件或目录struct file{ struct file * parent;//父块指针 struct file * boy;//子块指针 struct file * hou;//同级的前一个指针 struct file * qian;//同级的后一个指针 char name[9];//文件、目录名 char text[disks];//文件内容 int way; int kj; };//用户struct user{ char username[unsize];//用户名 char usepassword[passize];//用户密码 struct file * fumenu;//链表组指针 };struct kz{ struct file * diz; int free; }; struct user yhbiao[usercounts]; struct kz disk[disks]; int yhcount=0; int dangqianyh; int kb; struct file * now; struct file * now2; char iput[20]; char iput2[20]; int over; char load[100];//主函数void main() { char *ipnut; int i,tui,j; int cs; cs=begin(); if (cs==0) cout<<"n Welcome to my UNIX Files System ! n"; else {ipnut=(char *)malloc(sizeof(char)*8); biao: //主界面 cout<< "n"; cout<<" creat menu: md menu's name ** creat file: creat file's name n"; cout<<" show menu: dir ** open file: open file's name n"; cout<<" change menu: cd dirname ** close file: close file's name n"; cout<<" back menu: cd .. ** write file: write file's name n"; cout<<" delete menu: rm dirname ** read file: read file's name n"; cout<<" login: login ** delete file: del file's name n"; cout<<" register: register ** change user: exit n"; cout<<" logout: logout ** for hlp: hlp n"; cout<< "n"; cout<< "n"; cout<< "n Please input Command: " ; do { cin>>ipnut ; if(strcmp(ipnut,"register")==0)//判断输入的是否为注册的命令 { kb=1; rol(kb); goto biao; } else if(strcmp(ipnut,"login")==0)//判断登陆的是否为登陆命令 {kb=0; if( rol(kb) == 1) goto biao; break; } else if(strcmp(ipnut,"logout")==0) goto biao2; else {cout<<"n Wrong command ! n";goto biao;} }while( 1 ) ; do { tui=commandjiexi(); if(tui == 1 ) break; if (tui==2) goto biao; }while(1) ; biao2:; } }int begin(){ int i,j; for(i=0;iname[0]=''; disk[j].free=0; } return 1;}int rol(int kb){ char user[unsize]; char pw[passize],pw2[passize]; int i,j,k; if( kb==1) { if (yhcount==usercounts)//判断用户名是否过长 cout<<"n Sorry,your name is too long,please input another short one!n"; else { cout<<"n n"; cout<<"username: "; cin>>user; for(i=0;i>pw; cout<<" n n"; cout<<"n password : ";//输入确认密码 cin>>pw2; if (strcmp(pw,pw2)!=0)//判断两次输入的密码是否一样 { cout<<" n Your passwords is error,so you can't register! n";return 1;} else { cout<<" n Welcome!! You had register success ! n"; strcpy(yhbiao[yhcount].username,user); strcpy(yhbiao[yhcount].usepassword,pw); for(k=0;kparent=0; disk[k].diz->boy=0; disk[k].diz->hou=0; disk[k].diz->qian=0; disk[k].diz->kj=k; disk[k].free=1; strcpy(disk[k].diz->name,yhbiao[yhcount].username); yhbiao[yhcount].fumenu=disk[k].diz; now=disk[k].diz; yhcount++;//用户数目加一 } } } return 1; } else if( kb==0) { cout<<"n Welcome to Liangshiyan's UNIX FILES SYSTEM !n"; cout<< "n username : " ;//登陆输入用户名 cin>>user; cout<< "n Password: " ;//登陆输入用户密码 cin>>pw; for(i=0;iboy; while(move1!=0) { if((strcmp(name,move1->name)==0)&&(move1->way==-1)) break; move1=move1->hou; } if (move1!=0)//判断目录名是否相同 {cout<<"n The filemenu's name is same to another one,please input another one! n"; return ;} else//创建目录 { disk[k].free=1; disk[k].diz->parent=now; disk[k].diz->boy=0; disk[k].diz->qian=0; disk[k].diz->way=-1; disk[k].diz->kj=k; strcpy(disk[k].diz->name,name); if (now->boy!=0) now->boy->qian= disk[k].diz; disk[k].diz->hou=now->boy; now->boy= disk[k].diz; cout<<"n Well done! Creat success! n"; return ; } } } else if (kb==0) { move1=now->boy; while(move1!=0) { if((strcmp(name,move1->name)==0)&&(move1->way==-1)) break; move1=move1->hou; } if (move1==0)//输入错误的目录名 { cout<<"n You had inputted the wrong filemenu's name,please input again!! n"; return;} else { now2=move1; if (now2->boy!=0)//含有文件,不能删除 {cout<<"n The filemenu has some files in,so you can't delete it! n";return;} else//删除成功 { cout<<"n"<name<<" Delete success! n "; if (now2->qian==0) {now->boy=now2->hou; now2->hou->qian=0; } else if (now2->hou==0) now2->qian->hou=0; else { now2->hou->qian=now2->qian; now2->qian->hou=now2->hou; } disk[now2->kj].free=0; return; } } } } void command(){ cin>>iput; return;}void clearbuf(){char k; strcpy(iput,""); return ;}//出项错误解释int commandjiexi(){ char buf[5]; over = 0; cout<< "n"<"; command(); if( strcmp( iput, "dir" ) == 0 )//判断查看命令 { fileliebiao(); clearbuf(); return 0; } if( strcmp( iput, "md" ) == 0 )//判断创建目录命令 { if( over ) { cout<< "md: too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "mkdir: too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) {kb=1;menu(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "cd" ) == 0 )//判断输入的是否更改目录命令 { if( over ) { cout<< "cd: too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "cd: too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) { menugaibian(iput); } clearbuf(); return 0; } if( strcmp( iput, "rm" ) == 0 )//删除目录命令 { if( over ) { cout<< "rm: too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "rmdir: too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) {kb=0;menu(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "open" ) == 0 )//打开文件命令 { if( over ) { cout<< "open: too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "open: too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) {kb=3; files(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "close" ) == 0 )//关闭文件命令 { if( over ) { cout<< "close: too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "close: too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) { kb=4;files(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "read" ) == 0 )//读文件命令 { if( over ) { cout<< "read: too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "read: too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) { kb=5;files(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "write" ) == 0 )//判断写文件命令 { if( over ) { cout<< "write: too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "write: too few argumentsn"; clearbuf(); return 0; } if( iput[0] != '' ) {kb=6;files(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "del" ) == 0 )//判断删除文件命令 { if( over ) { cout<< "del : too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "del : too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) { kb=2; files(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "creat" ) == 0 )//判断创建文件命令 { if( over ) { cout<< "creat : too few argumentsn" ; clearbuf(); return 0; } command(); if( iput[0] == '' ) { cout<< "creat : too few argumentsn" ; clearbuf(); return 0; } if( iput[0] != '' ) {kb=1; files(iput,kb);} clearbuf(); return 0; } if( strcmp( iput, "logout" ) == 0 )//判断注销命令 { clearbuf(); return 1; } if( strcmp( iput, "exit" ) == 0 )//判断推出命令 { clearbuf(); return 2; } if ( strcmp( iput, "hlp" ) == 0 )//判断帮助命令 { clearbuf(); return 2; } if( iput[0] != '' ) { cout<boy; while(move1!=0) { if((strcmp(name,move1->name)==0)&&(move1->way!=-1)) break; move1=move1->hou; } if (move1!=0)//判断时候有相同的文件名 {cout<<"n Creat error! The file's name is same to another one,please input another one! n";return ;} else//更改目录指针 { disk[k].free=1; disk[k].diz->parent=now; disk[k].diz->boy=0; disk[k].diz->qian=0; disk[k].diz->way=0; disk[k].diz->kj=k; disk[k].diz->text[0]=''; strcpy(disk[k].diz->name,name); if (now->boy!=0) now->boy->qian= disk[k].diz; disk[k].diz->hou=now->boy; now->boy= disk[k].diz; cout<<"n Creat success! n"; return ; } } } case 2: { move1=now->boy; while(move1!=0) { if((strcmp(name,move1->name)==0)&&(move1->way==0)) break; move1=move1->hou; } if (move1==0)//判断文件时候能够删除 { cout<<"n Delete error! n"; cout<<"n You can't delete the file,because the file is openning or there is a wrong name! n"; return;} else { cout<<"n"<name<<" Delete success! n "; now2=move1; if (now2->qian==0) {now->boy=now2->hou; now2->hou->qian=0; } else if (now2->hou==0) now2->qian->hou=0; else { now2->hou->qian=now2->qian; now2->qian->hou=now2->hou; } disk[now2->kj].free=0; } return; } case 3: { move1=now->boy; while(move1!=0) { if((strcmp(name,move1->name)==0)&&(move1->way==0)) break; move1=move1->hou; } if (move1==0)//判断文件是否能够打开 { cout<<"n Open error! n"; cout<<"n You can't open the file,because the file is openning or there is a wrong name! n";} else { cout<<"n Please change the way to open the file, R(read) or W(write)? n"; cin>>fang; if ((fang=='r')||(fang=='R'))//文件能够读 way=1; else if ((fang=='w')||(fang=='W'))//文件能够读和写 way=2; else way=1; move1->way=way; cout<<"n"<name; now2=move1; if (way==1) cout<<"n It is openning by read only ! n"; else cout<<"n It is openning by read and write ! n"; } return; } case 4: { move1=now->boy; while(move1!=0) { if(((strcmp(name,move1->name)==0)&&(move1->way==1))||((strcmp(name,move1->name)==0)&&(move1->way==2))) break; move1=move1->hou; } if (move1==0)//关闭文件 {cout<<"n Close error! n"; cout<<"n It is the wrong name or the file have being closed! n";} else { move1->way=0; cout<<"n"<name; cout<<" Close success! n"; } return; }case 5: {move1=now->boy; while(move1!=0) { if(((strcmp(name,move1->name)==0)&&(move1->way==1))||((strcmp(name,move1->name)==0)&&(move1->way==2))) break; move1=move1->hou; } if (move1==0)//没有东西可以读出 {cout<<"n Rrite error ! n"; cout<<"n It is the wrong name or the file have not being openned! n";} else { cout<<"n"; cout<<"n "<< move1->name <<" Read success! n "; cout<<"n "; cout<<"n"<text; cout<<"n"; } return; }case 6: {move1=now->boy; while(move1!=0) { if((strcmp(name,move1->name)==0)&&(move1->way==2)) break; move1=move1->hou; } if (move1==0)//没有东西可以写入 { cout<<"n Write error ! n"; cout<<"n It is the wrong name or the file have being openned! n";return; } else { cout<<"n"; cout<<"n iput "<name<<"Write success! n "; cout<<"n"; cout<<"n"; cin>>move1->text; return; } }}}//文件列表void fileliebiao(){ int geshu=1,filecount=0,menucout=0; struct file * move1; move1=now->boy; cout<<"n n[..]"; while(move1!=0) { //计算目录个数 if (move1->way==-1) {cout<<" "; cout<<"["<name<<"]";menucout++; geshu++;} if (geshu%4==0) cout<<"n"; move1=move1->hou; } move1=now->boy; while(move1!=0) { //计算文件个数 if (move1->way!=-1) { cout<<" "; cout<name;filecount++; geshu++;} if (geshu%4==0) cout<<"n"; move1=move1->hou; } cout<<"n"; cout<<"n"; cout<<" files "<parent==0) {now=now;} else { l=strstr(load,now->name); l2=l; for(i=1;iname); if (l2==0) break; } l[-1]=''; now=now->parent; } } else { move1=now->boy; while(move1!=0) { if((strcmp(name,move1->name)==0)&&(move1->way==-1)) break; move1=move1->hou; } if (move1==0) {cout<<"n Load error! n"; cout<<"n It is the wrong path! n";} else { now=move1; strcat(load,"\"); strcat(load,now->name); } } return ;}八、使用说明书登陆:login注册:register注销:logout创建目录:md xxx显示目录:dir改变目录:cd xxx返回前一目录:cd ..删除目录:rm xxx创建文件:creat xxx打开文件:open xxx关闭文件:cllose xxx写入文件:write xxx读出文件:read xxx删除文件:del xxx退出:exit帮助:hlp九、体会这个课程设计虽然只有一周的时间,但是我掌握的知识并不少,因为c++已经丢下了很久,所以刚开始的时候有点难度,要重新概览一下c++才能够编程,在学习操作系统文件管理的时候,并不是很了解,但是通过这个实验后,使我对操作系统有个新的认识,并且加强了自己的编程能力。十、参考文献王红《操作系统原理及应用(Linux)》中国水利水电出版社 2005陈莉君《.深入分析Linux内核源代码》.人民邮电出版社2001周伟明 著 多任务下的数据结构与算法 华中科技大学出版社 2006徐虹《操作系统实验指导——基于Linux内核》清华大学出版社.2004.

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《《操作系统原理与Linux》课程设计报告》
文章链接:https://www.skykkk.com/archives3346.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

相关推荐

  • 暂无文章