二级文件系统_第1页
二级文件系统_第2页
二级文件系统_第3页
二级文件系统_第4页
二级文件系统_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《计算机操作系统》课程设计题 目:二级文件系统专 业:计算机科学与技术班 级:姓名:学号:指导教师:时 间:2011.6.01---2011.6.102011年

一、实验内容为Linux系统设计一个简单的二级文件系统。要求做到以下几点:1.可以实现下列几条命令: login用户登录 dir列目录 create创建文件 delete删除文件 open打开文件 close关闭文件 read读文件 write写文件2.列目录时要列出文件名,物理地址,保护码和文件长度二、实验目的通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。三、开发环境Windows操作系统

MicrosoftVisualC++四、分析设计实验原理通过程序模拟Linux文件系统,用一个二进制文件(FileSystem.disk)来模拟磁盘.设计一个多用户的二级文件系经统、实现一般的创建文件、目录,删除文件、目录,切换目录,打开、关闭文件、读写文件等操作。

文件系统,包含格式化,显示文件(目录),创建文件等几个简单命令的实现,而且能完成超级块的读写,节点的读写等过程.本文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户账号下的文件。另外,为了简单本文件系统未考虑文件共享、文件系统安全以及管道文件与设备文件等特殊内容。1.程序执行流程图:开始开始初始化初始化 选择程序选择程序删除目录、文件查看目录、文件进入指定目录返回上一目录级查询创建目录、文件删除目录、文件查看目录、文件进入指定目录返回上一目录级查询创建目录、文件格式化格式化结束结束2.数据块的分配和回收开始开始系统是否有空块?返回当前空闲块地址;超级块空闲指针加1开启新的块组,将其地址信息读入超级块;返回该块组首地址结束超级块中是否有空块? 设计FileSystem类负责管理磁盘空间和磁盘内存I节点,负责对磁盘空间和磁盘数据进行优化管理。并提代接口言方法供用户或程序调用。内存内存System用户11磁盘用户21用户31用户n1五、打印的源程序及附上的注释#include"xd.h"//文件管理voidcreateFile(charfileName[],intlength,charfileKind[]);//创建文件voidfileWrite(charfileName[]);//写文件voidfileCat(charfileName[]);//读文件voidfileRen(charfileName[],charrename[]);//重命名文件voidfileClose(charfileName[]);//关闭已打开的文件voiddelFile(charfileName[]);//删除文件intrequestDist(int&startPostion,intmaxLength);//磁盘分配查询voidinitDisk();//初始化磁盘voidfreeDisk(intstartPostion);//磁盘空间释放//用户管理voiduserCreate();intlogin();intuserID=-1;//用户登录的ID号,值为-1时表示没有用户登录//用户注册voiduserCreate(){ charc; charuserName[10]; inti; if(used<MaxUser) { cout<<"请输入用户名:"; for(i=0;c=getch();i++) { if(c==13)break; else userName[i]=c; printf("%c",c); } userName[i]='\0'; for(i=0;i<used;i++) { if(!strcmp(userTable[i].userName,userName)) { cout<<"\n"; cout<<"该用户名已存在,创建用户失败\n"; //system("pause"); return; } } strcpy(userTable[used].userName,userName); cout<<"\n"; cout<<"请输入密码:"; for(i=0;c=getch();i++) { if(c==13)break; else userTable[used].password[i]=c; printf("*"); } userTable[used].password[i]='\0'; cout<<"\n"; cout<<"创建用户成功\n"; used++; //system("pause"); } else { cout<<"创建用户失败,用户已达到上限\n"; //system("pause"); } fflush(stdin);}//登录intlogin(){ charname[10],psw[10]; charc; inti,times; cout<<"请输入用户名:"; for(i=0;c=getch();i++) { if(c==13)break; else name[i]=c; printf("%c",c); } name[i]='\0'; for(i=0;i<used;i++) { if(!strcmp(userTable[i].userName,name)) break; } if(i==used) { cout<<"\n您输入的用户名不存在\n"; //system("pause"); return-1; } for(times=0;times<3;times++) { memset(psw,'\0',sizeof(psw)); cout<<"\n请输入密码:"; for(i=0;c=getch();i++) { if(c==13)break; else psw[i]=c; cout<<"*"; } printf("\n"); for(i=0;i<used;i++) { if(!strcmp(psw,userTable[i].password)) { printf("用户登录成功\n"); //system("pause"); break; } } if(i==used) { printf("您输入的密码错误,您还有%d次输入机会\n",2-times); if(times==2)exit(0); } elsebreak; } returni;}//磁盘初始化voidinitDisk(){ diskHead=(diskNode*)malloc(sizeof(diskNode)); diskHead->maxlength=MaxDisk; diskHead->useFlag=0; diskHead->start=0; diskHead->next=NULL;}//分配磁盘intrequestDist(int&startPostion,intmaxLength){ intflag=0;//标记是否分配成功 diskNode*p,*q,*temp; p=diskHead; while(p) { if(p->useFlag==0)//磁盘块未被使用,且剩余长度大于要创建文件的长度 { startPostion=p->start; q=(diskNode*)malloc(sizeof(diskNode)); q->start=p->start; q->maxlength=maxLength; q->useFlag=1; q->next=NULL; diskHead->start=p->start+maxLength;//剩余磁盘空间头指针后移 diskHead->maxlength=p->maxlength-maxLength; //剩余磁盘空间长度减少 flag=1; temp=p; if(diskHead->next==NULL)diskHead->next=q; else { while(temp->next)temp=temp->next; temp->next=q; } break; } p=p->next; } returnflag;}//创建文件voidcreateFile(charfileName[],intlength){ //inti,j; time_trawtime; intstartPos; UFD*fileNode,*p; for(p=userTable[userID].user->next;p!=NULL;p=p->next) { if(!strcmp(p->file->fileName,fileName)) { printf("文件重名,创建文件失败\n"); //system("pause"); return; } } if(requestDist(startPos,length)) { fileNode=(UFD*)malloc(sizeof(UFD)); fileNode->file=(fileTable*)malloc(sizeof(fileTable));//这一步必不可少,因为fileNode里面的指针也需要申请地址,否则fileNode->file指向会出错 strcpy(fileNode->file->fileName,fileName); fileNode->file->maxlength=length; fileNode->file->strat=startPos; fileNode->file->openFlag=false; time(&rawtime);//读取系统当前时间 fileNode->file->timeinfo=localtime(&rawtime); fileNode->next=NULL; if(userTable[userID].user->next==NULL)//用户当前还没有创建文件时 userTable[userID].user->next=fileNode; else//已创建过文件,找到最后一个的next指针 { p=userTable[userID].user->next; while(p->next)p=p->next; p->next=fileNode; } printf("创建文件成功\n"); //system("pause"); } else { printf("磁盘空间已满或所创建文件超出磁盘空闲容量,磁盘空间分配失败\n"); //system("pause"); }}//释放磁盘voidfreeDisk(intstartPostion){ diskNode*p; for(p=diskHead;p!=NULL;p=p->next) { if(p->start==startPostion)//找到startPostion位置的指针 break; } p->useFlag=false;}//查看文件voidfileCat(charfileName[]){ intstartPos,length; intk=0; UFD*p,*q; q=userTable[userID].user; for(p=q->next;p!=NULL;p=p->next) { if(!strcmp(p->file->fileName,fileName)) break; } if(p) { startPos=p->file->strat; length=p->file->length; p->file->openFlag=true;//文件打开标记 printf("--------------------------------------------------------------\n"); for(inti=startPos;k<length;i++,k++) { if(i%50==0)printf("\n");//一行大于50个字符换行 printf("%c",disk[i]); } printf("\n\n----------------------------------------------------------\n"); } else { printf("没有找到该文件,请检查输入的文件名是否正确\n"); //system("pause"); } }//覆盖写入voidfileWrite(charfileName[]){ UFD*p,*q; q=userTable[userID].user; inti,k,startPos; for(p=q->next;p!=NULL;p=p->next) { if(!strcmp(p->file->fileName,fileName)) break; } if(p) { if(!strcmp(p->file->fileKind,"r"))//判断文件类型 { printf("该文件是只读文件,写入失败\n"); //system("pause"); return; } charstr[500]; printf("pleaseinputcontent:\n"); gets(str); startPos=p->file->strat; p->file->openFlag=true;//文件打开标记 p->file->length=strlen(str); if(p->file->length>p->file->maxlength) { printf("写入字符串长度大于该文件的总长度,写入失败\n"); //system("pause"); return; } for(i=startPos,k=0;k<(int)strlen(str);i++,k++) disk[i]=str[k]; printf("文件写入成功\n"); } else { printf("没有找到该文件,请检查输入的文件名是否正确\n"); // system("pause"); } if(p) { p->file->openFlag=false; printf("%s文件已关闭\n",p->file->fileName); // system("pause"); }}//重命名voidfileRen(charfileName[],charname[]){ UFD*p,*q; q=userTable[userID].user; for(p=q->next;p!=NULL;p=p->next) { if(!strcmp(p->file->fileName,fileName)) break; } if(p) { while(q->next) { if(!strcmp(q->next->file->fileName,name)) { printf("您输入的文件名已存在,重命名失败\n"); //system("pause"); return; } q=q->next; } strcpy(p->file->fileName,name); printf("重命名成功\n"); //system("pause"); } else { printf("没有找到该文件,请检查输入的文件名是否正确\n"); //system("pause"); }}//关闭文件voidfileClose(charfileName[]){ UFD*p,*q; q=userTable[userID].user; for(p=q->next;p!=NULL;p=p->next) { if(!strcmp(p->file->fileName,fileName)) break; } if(p) { p->file->openFlag=false; printf("%s文件已关闭\n",p->file->fileName); //system("pause"); } else { printf("没有找到该文件,请检查输入的文件名是否正确\n"); //system("pause"); }}//查看文件详细信息voidfileLength(charfileName[]){ intstartPos,length;//charkind[3]; intk=0; UFD*p,*q; q=userTable[userID].user; for(p=q->next;p!=NULL;p=p->next) { if(!strcmp(p->file->fileName,fileName)) break; } if(p) { startPos=p->file->strat; length=p->file->length;//获取文件长度 printf("文件长度为%d\n",length); //system("pause"); } else { printf("没有找到该文件,请检查输入的文件名是否正确\n"); //system("pause"); }}//检查文件是否关闭voidifClose(charfileName[]){ boolopen;UFD*p,*q; q=userTable[userID].user; for(p=q->next;p!=NULL;p=p->next) { if(!strcmp(p->file->fileName,fileName)) break; } if(p) { open=p->file->openFlag; if(open==true) printf("%s文件未关闭\n",p->file->fileName); else//open=true printf("%s文件已关闭\n",p->file->fileName); //system("pause"); } else { printf("没有找到该文件,请检查输入的文件名是否正确\n"); //system("pause"); }}voidprint(){cout<<"create创建文件\n"; cout<<"length查看文件长度\n"; cout<<"cat查看文件内容\n"; cout<<"write覆盖写入\n"; cout<<"ren重命名\n"; cout<<"del删除文件\n"; cout<<"close关闭文件\n"; cout<<"return退出用户,返回登录界面\n"; cout<<"exit退出程序\n"; cout<<"ifclose检查文件是否关闭\n";}//删除文件voiddelFile(charfileName[]){ UFD*p,*q,*temp; q=userTable[userID].user; p=q->next; while(p) { if(!strcmp(p->file->fileName,fileName))break;//文件不存在 else { p=p->next; q=q->next; } } if(p) { if(p->file->openFlag!=true)//先判断是否有进程打开该文件,如果文件没被打开 { temp=p; q->next=p->next; freeDisk(temp->file->strat);//磁盘空间回收 free(temp); printf("文件删除成功\n"); //system("pause"); } else { printf("该文件已被进程打开,删除失败\n");//文件被打开 //system("pause"); } } else { printf("没有找到该文件,请检查输入的文件名是否正确\n");//文件不存在 //system("pause"); }}intmain(){ charorder[commandAmount][10]; strcpy(order[0],"create"); strcpy(order[1],"cat"); strcpy(order[2],"write"); strcpy(order[3],"ren");strcpy(order[4],"del"); strcpy(order[5],"close"); strcpy(order[6],"return"); strcpy(order[7],"exit"); strcpy(order[8],"awrite"); strcpy(order[9],"length"); strcpy(order[10],"ifclose"); charcommand[50],command_str1[10],command_str2[10],command_str3[5];//,command_str4[3]; inti,j,k; intlength; initDisk();//初始化磁盘 for(i=0;i<MaxUser;i++)//初始化用户UFD目录文件的头指针 { userTable[i].user=(UFD*)malloc(sizeof(UFD)); userTable[i].user->next=NULL; } cout<<"----------------------------------------------------------\n"; cout<<"请先进入用户管理\n\n"; cout<<"Creatuser创建新用户请输入1\n"; cout<<"login用户登录请输入2\n"; cout<<"----------------------------------------------------------\n\n"; while(1) { cout<<"Pleasechoosethefunctionkey:>"; intchoice; scanf("%d",&choice); if(choice==1)userCreate(); elseif(choice==2) { userID=login(); print(); } elseprintf("您的输入有误,请重新选择\n"); while(userID!=-1) { fflush(stdin); cout<<"pleaseinputyourcommand:>"; gets(command); intselect; for(i=0;command[i]!=''&&command[i]!='\0';i++)//command_str1字符串存储命令的操作类型 command_str1[i]=command[i]; k=i; command_str1[k]='\0'; for(i=0;i<commandAmount;i++) { if(!strcmp(command_str1,order[i])) { select=i; break; } } if(i==commandAmount) { cout<<"您输入的命令有误,请重新输入\n"; continue; } for(i=k+1,k=0;command[i]!=''&&command[i]!='\0';i++,k++)//com

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论