操作系统课程设计-模拟文件系统

目录

第1章 需求分析……...….........……………………………………1 第2章 概要设计……...….........……………………………………1 2.1 系统的主要功能……...….........…………………………….1 2.2系统模块功能结构……...….........…………………….. ……1 2.3运行环境要求……...….........…………………………….. …2 2.4数据结构设计……...….........………………………….. ……2 第3章 详细设计……...….........……………………………………3

3.1模块设计……...….........………………………………….. …3 3.2算法流程图……...….........………………………….. ………3 第4章 系统源代码……...….........…………………………………4 第5章 系统测试及调试……...….........……………………………4 5.1运行结果及分析……...….........…………………….. ………4 5.2系统测试结论……...….........………………………….. ……5 第6章 总结与体会……...….........…………………………………6 第7章 参考文献……...….........……………………………………6 附录……...….........………………………………………………. ….7

第1章 需求分析

通过模拟文件系统的实现,深入理解操作系统中文件系统的理论知识, 加深对教材中的重要算法的理解。同时通过编程实现这些算法, 更好地掌握操作系统的原理及实现方法, 提高综合运用各专业课知识的能力;掌握操作系统结构、实现机理和各种典型算法,系统地了解操作系统的设计和实现思路,并了解操作系统的发展动向和趋势。

模拟二级文件管理系统的课程设计目的是通过研究Linux 的文件系统结构,模拟设计一个简单的二级文件系统,第一级为主目录文件,第二级为用户文件。

第2章 概要设计

2.1 系统的主要功能

1) 系统运行时根据输入的用户数目创建主目录 2) 能够实现下列命令: L ogin 用户登录

Create 建立文件 Read 读取文件

Write 写入文件 D elete 删除文件

Mkdir 建立目录 Cd 切换目录 Logout 退出登录

2.2系统模块功能结构

2.3运行环境要求

操作系统windows xp ,开发工具vc++6.0 2.4数据结构设计

用户结构:账号与密码结构 typedef struct users {

char name[8]; char pwd[10]; }users;

本系统有8个默认的用户名, 前面是用户名, 后面为密码, 用户登陆时只要输入正确便可进入系统, 否则提示失败要求重新输入。

users usrarray[8] = {

"usr1","usr1", "usr2","usr2", "usr3","usr3", "usr4","usr4", "usr5","usr5", "usr6","usr6", "usr7","usr7", "usr8","usr8", };

(3)数据结构说明 a) 文件结构链表 struct fnode {

char filename[FILENAME_LENGTH]; int isdir; int isopen;

char content[255]; fnode *parent; fnode *child; fnode *prev; fnode *next; };

b) 函数介绍

fnode *initfile(char filename[],int isdir);//初始化文件或目录 void createroot();//建立系统根目录 int run();系统运行

int findpara(char *topara);对参数进行处理

bool chklogin(char *users, char *pwd);检查账号与口令 void help();命令列表 int mkdir();建立目录 int create();建立文件

int read();读取文件 int write();写入文件 int del();删除文件 int cd();切换目录

int dir();文件与目录列表

第3章 详细设计

3.1模块设计

此课程设计把文本作为研究对象来模拟操作系统的文件系统工作过程。所以用一个字符串数组来模拟磁盘空间,顾名思义,模拟磁盘提供字符的存储服务。

所有用户构成一个数组,每个数组元素是一个结构体,每个结构体包括三部分,用户的用户名、用户密码和文件链表(由于模拟文件系统的文件数量不多,故文件表采用线性链表来存储。线性表每个结点放置一个文件的FCB ,其中存储一个文件的信息,文件名、长度、类型、创建时间等);

磁盘空间分配表,采用链表结构,每个节点保存模拟磁盘的一个逻辑块的信息,包括块的最大长度,文件占用长度,占用标志。如果占用标志为0,即该空间可分配给文件。初始化磁盘空间分配表链表,首先把整个模拟磁盘作来一块,并置占用位为0。当有进程申请磁盘空间时,从头开始遍历,检查占用位,如果该块为可分配,则检查块大小,若块长度大于或等于申请空间大小,则把块的前一部分(等于申请大小)分配给文件,并置标志位为占用。剩下的大小作来一个新块,作来一个新节点插入到原节点的后边,标志位为可用。这样就实现了模拟磁盘的线性分配。

3.2

第4章 系统源代码

见附录

第5章 系统测试及调试

5.1运行结果及分析

5.2系统测试结论

从运行结果截图中可以看到,程序分别执行了它所包含的7个功能,并且每个功能都能正确的执行。若程序执行开始,三次都未输入正确的帐号和密码,程

序会退出不再执行。

第6章 总结与体会

虽然我们做过很多次课程设计了,但是感觉自己还有好多需要学习的地方,接到题目要求时,设计大体的框架,考虑好所使用的数据结构,然后用高级编程语言分模块的把架子的思路编写出来,调试,运行,再看看是不是符合题目的要求,上网找些资料,看看想想是不是要提高要求,才可以满足实际的需要,最后把收集的劳动成果组合起来,一个小程序终于成型了,虽然每次的过程差不多都一样,但是每次都会有不同的体会。

通过本次的课程设计,使我能够正确运用操作系统课程中所学的基本理论和知识,加深了对文件系统基本概念的理解,以及磁盘文件系统的文件操作。在设计过程中,查询了不少相关资料,不断的发现问题、提出问题、解决问题。在对自己所编写的源程序段的纠错的过程中,使我更好的理解了操作系统中文件系统的理论知识,同时在编程时用到了模块化的设计思想,这种编程方法可以使我们的编程变的更简单,可以使我们的查错与纠错变的更方便。总的来说通过这次的设计的学习使我学到了很多在平时的学习中学不到的很多东西,通过这次课程设计,使我对操作系统和编程产生兴趣,我想我会在这条路上继续前进下去。我相信,只要不断的严格要求自己,注意培养自己的思维能力,就一定会有更大更辉煌的发展和提高。

第7章 参考文献

《操作系统原理实验教程》,清华大学出版社 胡峰松主编 《操作系统实验教程》,清华大学出版社 张丽芬等 编著 《计算机操作系统实验教程》,清华大学出版社 颜彬等编著

附录

系统的主要源代码

#include "stdio.h" #include "iostream.h" #include "string.h" #include "iomanip.h"

#define FILENAME_LENGTH 10 //文件名称长度 #define COMMAND_LENGTH 10 //命令行长度 #define PARA_LENGTH 30 //参数长度

//账号结构

typedef struct users {

char name[8]; char pwd[10]; }users; //文件结构 struct fnode {

char filename[FILENAME_LENGTH]; int isdir; int isopen;

char content[255]; fnode *parent; fnode *child; fnode *prev; fnode *next; }; //账号

users usrarray[8] = {

"usr1","usr1", "usr2","usr2", "usr3","usr3", "usr4","usr4", "usr5","usr5", "usr6","usr6", "usr7","usr7", "usr8","usr8", };

fnode *initfile(char filename[],int isdir); void createroot(); int run();

int findpara(char *topara);

bool chklogin(char *users, char *pwd); void help(); int mkdir(); int create(); int read(); int write(); int del(); int cd(); int dir();

fnode *root,*recent,*temp,*ttemp; char

para[PARA_LENGTH],command[COMMAND_LENGTH],temppara[PARA_LENGTH],recentpara[PARA_LENGTH];

//创建文件与目录结点

fnode* initfile(char filename[],int isdir) {

fnode *node=new fnode;

strcpy(node->filename,filename); node->isdir=isdir; node->isopen=0;

node->parent=NULL; node->child=NULL; node->prev=NULL; node->next=NULL; return node; }

//创建文件存储结点 void createroot () {

recent=root=initfile("/",1); root->parent=NULL; root->child=NULL;

root->prev=root->next=NULL; strcpy(para,"/"); }

int mkdir() {

temp=initfile(" ",1); cin>>temp->filename;

if(recent->child==NULL) {

temp->parent=recent; temp->child=NULL; recent->child=temp;

temp->prev=temp->next=NULL; } else {

ttemp=recent->child; while(ttemp->next) {

ttemp=ttemp->next;

if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==1) {

printf("对不起, 目录已存在!"); return 1; } }

ttemp->next=temp; temp->parent=NULL; temp->child=NULL; temp->prev=ttemp; temp->next=NULL; }

return 1; }

int create() {

temp=initfile(" ",0); cin>>temp->filename; cin>>temp->content; if(recent->child==NULL) {

temp->parent=recent; temp->child=NULL; recent->child=temp;

temp->prev=temp->next=NULL; cout

ttemp=recent->child;

while(ttemp->next)

{

ttemp=ttemp->next;

if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==0) {

printf("对不起, 文件已存在!");

return 1;

}

}

ttemp->next=temp;

temp->parent=NULL;

temp->child=NULL;

temp->prev=ttemp;

temp->next=NULL;

cout

}

return 1;

}

int dir()

{

int i=0,j=0;

temp=new fnode;

temp=recent;

if(temp!=root)

{cout "

if(temp->child==NULL)

{

cout

return 1;

}

temp=temp->child;

while(temp)

{

if(temp->isdir)

{cout\ "filename

else

{cout "filename

temp=temp->next;

}

cout

}

int read()

char filename[FILENAME_LENGTH];

cin>>filename;

if(recent->child==NULL)

{

cout

return 1;

}

if(strcmp(recent->child->filename,filename)==0)

{

coutchild->content

return 1;

}

else

{

temp=recent->child;

while(temp->next)

{

if(strcmp(temp->next->filename,filename)==0)

{coutnext->content

return 1;}

}

cout

}

}

int write()

{

char filename[FILENAME_LENGTH];

cin>>filename;

if(recent->child==NULL)

{

cout

return 1;

}

if(strcmp(recent->child->filename,filename)==0)

{

recent->child->isopen=1;//设置文件标记为打开

cin>>recent->child->content;

recent->child->isopen=0;//设置文件标记为关闭

cout

return 1;

}

else

temp=recent->child;

while(temp->next)

{

if(strcmp(temp->next->filename,filename)==0)

{

recent->child->isopen=1;//设置文件标记为打开

cin>>temp->next->content;

recent->child->isopen=0;//设置文件标记为关闭

cout

return 1;}

}

cout

}

}

int cd()

{ char topara[PARA_LENGTH];

cin>>topara;

if(strcmp(topara,"..")==0)

{

int i;

while(recent->prev)

recent=recent->prev;

if(recent->parent)

{

recent=recent->parent;

}

i=strlen(para);

while(para[i]!='/' && i>0) i--;

if(i!=0)

para[i]='\0';

else

para[i+1]='\0';

}

else

{

findpara(topara);

}

return 1;

}

int findpara(char *topara)

{

int i=0;

int sign=1;

if(strcmp(topara,"/")==0)

{

recent=root;

strcpy(para,"/");

return 1;

}

temp=recent;

strcpy(temppara,para);

if(topara[0]=='/')

{

recent=root->child;

i++;

strcpy(para,"/");

}

else

{

if(recent!=NULL && recent!=root)

strcat(para,"/");

if(recent && recent->child)

{

if(recent->isdir)

recent=recent->child;

else

{

printf("路径错误!\n");

return 1;

}

}

}

while(i

{

int j=0;

if(topara[i]=='/' && recent->child)

{

i++;

if(recent->isdir)

recent=recent->child;

else

{printf("路径错误\n");

return 0;

}

strcat(para,"/");

}

while(topara[i]!='/' && i

recentpara[j]=topara[i];

i++;j++;

}

recentpara[j]='\0';

while((strcmp(recent->filename,recentpara)!=0 || (recent->isdir!=1)) && recent->next!=NULL)

{

recent=recent->next;

}

if(strcmp(recent->filename,recentpara)==0)

{

if(recent->isdir==0)

{strcpy(para,temppara);

recent=temp;

printf("是文件不是目录。\n");

return 0;

}

strcat(para,recent->filename);

}

if(strcmp(recent->filename,recentpara)!=0 || recent==NULL)

{

strcpy(para,temppara);

recent=temp;

printf("输入路径错误\n");

return 0;

}

}

return 1;

}

int del()

{

char filename[FILENAME_LENGTH];

cin>>filename;

temp=new fnode;

if(recent->child)

{

temp=recent->child;

while(temp->next && (strcmp(temp->filename,filename)!=0 || temp->isdir!=0)) temp=temp->next;

if(strcmp(temp->filename,filename)!=0)

{

cout

return 0;

}

else

{

cout

return 0;

}

if(temp->parent==NULL)

{

temp->prev->next=temp->next;

if(temp->next)

temp->next->prev=temp->prev;

temp->prev=temp->next=NULL;

}

else

{

if(temp->next)

temp->next->parent=temp->parent;

temp->parent->child=temp->next;

}

delete temp;

cout

}

bool chklogin(char *users, char *pwd)

{

int i;

for(i=0; i

{

if( (strcmp(users,usrarray[i].name)==0) && (strcmp(pwd,usrarray[i].pwd)==0)) return true;

}

return false;

}

void help(void)

{

cout

cout

int run()

{

cout";

cin>>command;

if(strcmp(command,"mkdir")==0)

mkdir();

else if(strcmp(command,"dir")==0)

dir();

else if(strcmp(command,"cd")==0)

cd();

else if(strcmp(command,"create")==0)

create();

else if(strcmp(command,"read")==0)

read();

else if(strcmp(command,"write")==0)

write();

else if(strcmp(command,"del")==0)

del();

else if(strcmp(command,"help")==0)

help();

else if(strcmp(command,"logout")==0)

return 0;

}

int main()

{

int i=0;

bool in=false;

char users[8],pwd[12];

cout

while(i

{

cout

cin>>users;

cout

cin>>pwd;

if(chklogin(users,pwd))

{in=true;break;}

i++;

help();

createroot(); while(in) {

if(!run()) break; } }


© 2024 实用范文网 | 联系我们: webmaster# 6400.net.cn