操作系统课程设计模拟一个简单二级文件管理系统.doc

上传人:文库蛋蛋多 文档编号:4099415 上传时间:2023-04-04 格式:DOC 页数:17 大小:62.50KB
返回 下载 相关 举报
操作系统课程设计模拟一个简单二级文件管理系统.doc_第1页
第1页 / 共17页
操作系统课程设计模拟一个简单二级文件管理系统.doc_第2页
第2页 / 共17页
操作系统课程设计模拟一个简单二级文件管理系统.doc_第3页
第3页 / 共17页
操作系统课程设计模拟一个简单二级文件管理系统.doc_第4页
第4页 / 共17页
操作系统课程设计模拟一个简单二级文件管理系统.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《操作系统课程设计模拟一个简单二级文件管理系统.doc》由会员分享,可在线阅读,更多相关《操作系统课程设计模拟一个简单二级文件管理系统.doc(17页珍藏版)》请在三一办公上搜索。

1、模拟一个简单二级文件管理系统设计目的:通过具体的文件存储空间的管理、文件的物理结构、目录结构和文件操作的实现,加深对文件系统内部功能和实现过程的理解。设计内容:模拟一个简单二级文件管理系统 一、实验内容描述1 实验目标本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现.2 实验要求为DOS系统设计一个简单的二级文件系统.要求做到以下几点:可以实现下列命令:login 用户登录dir 列文件目录create 创建文件delete 删除文件open 打开文件close 关闭文件read 读文件write 写文件列目录时要列出文件名、物理地址、保护码和文件长度.源文

2、件可以进行读写保护.二、程序主要内容1设计思路程序中要求每个用户在登陆后才可对其拥有的文件进行操作,用户对于其他用户的文件无操作权.文件操作包括浏览、创建、删除、打开、关闭、阅读、写入、修改模式.其他操作包括新建用户、帮助、用户登入、用户登出、退出系统.在程序文件夹下有个名为“file”的系统根目录,此目录下包括:一个名为“mfd”的文件,记录所有注册过的帐号及密码;用户文件,以用户名作为文件名,内容为其拥有的文件名及属性;一个名为“keiji”的文件夹.“keiji”文件夹中包括:“file.p”指针文件,记录所有已用的物理地址;一些以物理地址为名的文件,内容为文件内容.2 数据结构file

3、结构体系统文件数据结构:fpaddrint,文件的物理地址、flengthint,文件长度、fmodeint,文件模式 0.只读;1.可写;2.可读写;3.保护、 fnamechar,文件名;filemode结构体文件状态数据结构:isopenint,文件当前状态,0.关闭;1.打开、modeint,文件模式 0.只读;1.可写;2.可读写;3.初始化;user结构体用户信息数据结构:unamechar,用户名、upasswordchar,用户密码;userfile结构体用户文件数据结构:unamechar,用户名、ufilefile,用户拥有的文件数组.代码:#include #includ

4、e #include #include #include #define MaxUser 100 /定义最大MDF主目录文件 #define MaxDisk 512*1024 /模拟最大磁盘空间 #define commandAmount 12 /对文件操作的指令数 /存储空间管理有关结构体和变量 char diskMaxDisk; /模拟512K的磁盘存储空间 typedef struct distTable /磁盘块结构体 int maxlength; int start; int useFlag; distTable *next; diskNode; diskNode *diskHead

5、; struct fileTable /文件块结构体 char fileName10; int strat; /文件在磁盘存储空间的起始地址 int length; /文件内容长度 int maxlength; /文件的最大长度 char fileKind3; /文件的属性读写方式 struct tm *timeinfo; bool openFlag; /判断是否有进程打开了该文件 /fileTable *next; ; /两级目录结构体 typedef struct user_file_directory /用户文件目录文件UFD /char fileName10; fileTable *f

6、ile; user_file_directory *next; UFD; /UFD *headFile; typedef struct master_file_directory /主文件目录MFD char userName10; char password10; UFD *user; MFD; MFD userTableMaxUser; int used=0; /定义MFD目录中用已有的用户数 /文件管理 void fileCreate(char fileName,int length,char fileKind); /创建文件 void fileWrite(char fileName);

7、 /写文件 void fileCat(char fileName); /读文件 void fileRen(char fileName,char rename); /重命名文件 void fileFine(char fileName); /查询文件 void fileDir(char UserName); /显示某一用户的所有文件 void fileClose(char fileName); /关闭已打开的文件 void fileDel(char fileName); /删除文件 void chmod(char fileName,char kind); /修改文件的读写方式 int reques

8、tDist(int &startPostion,int maxLength); /磁盘分配查询 void initDisk(); /初始化磁盘 void freeDisk(int startPostion); /磁盘空间释放 void diskShow(); /显示磁盘使用情况 /用户管理 void userCreate(); int login(); int userID=-1; /用户登录的ID号,值为-1时表示没有用户登录 int main() char ordercommandAmount10; strcpy(order0,create); strcpy(order1,rm); str

9、cpy(order2,cat); strcpy(order3,write); strcpy(order4,fine); strcpy(order5,chmod); strcpy(order6,ren); strcpy(order7,dir); strcpy(order8,close); strcpy(order9,return); strcpy(order10,exit); strcpy(order11,df); char command50,command_str110,command_str210,command_str35,command_str43; int i,k,j; int le

10、ngth; initDisk(); /初始化磁盘 for(i=0;inext=NULL; while(1) printf(*n); printf( 1、Creat usern); printf( 2、loginn); printf(*n); printf(Please chooce the function key:); int choice; scanf(%d,&choice); if(choice=1) userCreate(); else if(choice=2) userID=login(); else printf(您的输入有误,请重新选择n); while(userID!=-1)

11、fflush(stdin); printf(n); printf( create-创建 格式:create a1 1000 rw,将创建名为a1,长度为1000字节可读可写的文件n); printf( rm-删除 格式:rm a1,将删除名为a1的文件n); printf( cat-查看文件内容 格式:cat a1,显示a1的内容n); printf( write-写入 格式:write a1n); printf( fine-查询 格式:fine a1 ,将显示文件 a1的属性n); printf( chmod-修改 格式:chmod a1 r,将文件a1的权限改为只读方式n); printf

12、( ren-重命名 格式:ren a1 b1 ,将a1改名为b1n); printf( dir-显示文件 格式:dir aaa,将显示aaa用户的所有文件n); printf( df-显示磁盘空间使用情况 格式:dfn); printf( close-关闭文件 格式:close a1,将关闭文件a1n); printf( return-退出用户,返回登录界面n); printf( exit-退出程序n); printf(n); printf(please imput your command:); gets(command); int select; for(i=0;commandi!= &c

13、ommandi!=0;i+) /command_str1字符串存储命令的操作类型 command_str1i=commandi; k=i; command_str1k=0; for(i=0;i=0;i-) /把字符串转换为十进制 length+=(command_str3i-48)*j; j*=10; for(i=k+1,k=0;commandi!= &commandi!=0;i+,k+) command_str4k=commandi; command_str4k=0; fileCreate(command_str2,length,command_str4);break; case 1:fil

14、eDel(command_str2);break; case 2:fileCat(command_str2);break; case 3: fileWrite(command_str2);break; case 4:fileFine(command_str2);break; case 5:for(i=k+1,k=0;commandi!= &commandi!=0;i+,k+) command_str3k=commandi; command_str3k=0; chmod(command_str2,command_str3);break; case 6:for(i=k+1,k=0;commandi

15、!=0;i+,k+) command_str3k=commandi; command_str3k=0; fileRen(command_str2,command_str3);break; case 7:fileDir(command_str2);break; case 8:fileClose(command_str2);break; case 9:UFD *p; for(p=userTableuserID.user-next;p!=NULL;p=p-next) /退出用户之前关闭所有打的文件 if(p-file-openFlag) p-file-openFlag=false; system(c

16、ls); userID=-1;break; case 10:exit(0);break; case 11:diskShow();break; return 0; void userCreate() char c; char userName10; int i; if(usedMaxUser) printf(请输入用户名:); for(i=0;c=getch();i+) if(c=13) break; else userNamei=c; printf(%c,c); userNamei=0; for(i=0;iused;i+) if(!strcmp(userTablei.userName,user

17、Name) printf(n); printf(该用户名已存在,创建用户失败n); system(pause); return; strcpy(userTableused.userName,userName); printf(n); printf(请输入密码:); for(i=0;c=getch();i+) if(c=13) break; else userTableused.passwordi=c; printf(*); userTableuserID.passwordi=0; printf(n); printf(创建用户成功n); used+; system(pause); else pr

18、intf(创建用户失败,用户已达到上限n); system(pause); fflush(stdin); int login() char name10,psw10; char c; int i,times; printf(请输入用户名:); for(i=0;c=getch();i+) if(c=13) break; else namei=c; printf(%c,c); namei=0; for(i=0;iused;i+) if(!strcmp(userTablei.userName,name) break; if(i=used) printf(n您输入的用户名不存在n); system(p

19、ause); return -1; for(times=0;times3;times+) memset(psw,0,sizeof(psw); printf(n请输入密码:); for(i=0;c=getch();i+) if(c=13) break; else pswi=c; printf(*); printf(n); for(i=0;imaxlength=MaxDisk; diskHead-useFlag=0; diskHead-start=0; diskHead-next=NULL; int requestDist(int &startPostion,int maxLength) int

20、flag=0; /标记是否分配成功 diskNode *p,*q,*temp; p=diskHead; while(p) if(p-useFlag=0&p-maxlengthmaxLength) 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;

21、 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; return flag; void fileCreate(char fileName,int length,char fileKind) /int i,j; time_t rawtime; int startPos; UFD *fileNode,*p; for(p=userTableuserID.user-next;p!=NULL;p=p-next

22、) 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); s

23、trcpy(fileNode-file-fileKind,fileKind); fileNode-file-maxlength=length; fileNode-file-strat=startPos; fileNode-file-openFlag=false; time(&rawtime); fileNode-file-timeinfo=localtime(&rawtime); fileNode-next=NULL; if(userTableuserID.user-next=NULL) userTableuserID.user-next=fileNode; else p=userTableu

24、serID.user-next; while(p-next) p=p-next; p-next=fileNode; printf(创建文件成功n); system(pause); else printf(磁盘空间已满或所创建文件超出磁盘空闲容量,磁盘空间分配失败n); system(pause); void freeDisk(int startPostion) diskNode *p; for(p=diskHead;p!=NULL;p=p-next) if(p-start=startPostion) break; p-useFlag=false; void fileDel(char fileN

25、ame) UFD *p,*q,*temp; q=userTableuserID.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); void fileCat(char fileName) int startPos,length; int k=0; UFD *p,*q; q=userTableuserID.user; for(p=q-next;p!=NULL;p=p-next) if(!strcmp(p-fil

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号