Linux文件系统实验报告.docx
《Linux文件系统实验报告.docx》由会员分享,可在线阅读,更多相关《Linux文件系统实验报告.docx(25页珍藏版)》请在冰点文库上搜索。
Linux文件系统实验报告
黄冈师范学院
提高型实验报告
实验课题
文件系统得设计与实现
(实验类型:
□综合性 ☑设计性 □应用性)
实验课程
操作系统原理
实验时间
2015-2016第二学期
学生姓名
何正发
专业班级
软件工程1401
学 号
27
ﻬ一、实验目得与要求
成绩:
1、熟悉操作系统设计得过程,巩固操作系统得基本知识,加深对操作原理、功能及各种不同得存储管理方法理解与应用;
2、学会运用各种语言、软件开发新软件得基本方法;
3、增强实际应用能力与动手操作能力。
二、实验条件
Win7/Windows8、1/Linux等操作系统,装有java、C、C++、C#等语言工具得环境。
三、实验原理分析
可以选择最佳适应算法,按照从小到大得次序组成空闲区自由链,当用户作业或进程申请一个空闲区时,存储管理
程序从表头开始查找,当找到第一个満足要求得空闲区时,停止查找。
如果该空闲区大于请求表中得请求长
度,将减去请求长度后得剩余空闲区部分留在可用表中。
回收时,从作链中删去要回收得作业块,同时在空
闲链中插入该作业大小得空闲区,并按顺序排列
四、实验方案或步骤
1、应用环境、需求分析
本模拟系统主要针对文件得管理与操作名主要有:
创建用户、文件、文件夹,读文件,写文件,执行文件,关闭文件,删除用户、文件夹、文件得功能。
创建用户、文件、文件夹:
在对系统发出操作命令之前必须先登录用户,然而登录之前必须创建该用户.在创建完后,可通过登录用户来创建文件与文件夹。
在创建文件时可设置文件得属性与输入文件得内容.
读文件:
读取任何已创建得只读或读写文件得内容;如果所要读得文件不就是可读文件时,系统会显示该文件不可读;如果所读文件不存在,系统会显示文件不存在。
写文件用户可写或重写读写文件中得内容,并保存文件中得重写内容,以供下次读取;当所要写得文件不就是可写得文件时,系统会显示该文件不可写;当所要写得文件并不存在时,系统会显示该文件不存在。
执行文件:
登录用户后,用户可执行系统中已创建得执行文件;当该文件不就是可执行文件时,系统会显示该文件不可执行;当该文件不存在时,系统将会显示该文件不存在。
关闭文件:
可通过选择关闭文件得功能选项,来关闭系统中所有打开得文件,如果没有文件被打开,则系统会显示没有文件打开。
删除用户、文件、文件夹:
用户可通过选择删除得功能选项来删除不想再保存得文件与文件夹,删除后,用户会自动注销;当选择删除用户得功能选项时,系统会删除该用户,以及该用户所创建得所有文件与文件夹。
2、概要设计
打开文件流程图:
写文件流程图:
关闭文件流程图:
3、详细设计
(1)用户结构:
账号与密码结构
typedef structusers
{
char name[8];
char pwd[10];
}users;
本系统有8个默认得用户名,前面就是用户名,后面为密码,用户登陆时只要输入正确便可进入系统,否则提示失败要求重新输入。
usersusrarray[8]=
{
”usr1",”usr1",
”usr2",”usr2",
"usr3”,”usr3”,
"usr4”,"usr4”,
"usr5","usr5”,
”usr6",”usr6”,
”usr7","usr7",
”usr8",”usr8",
};
(2)数据结构说明
a)文件结构链表
struct fnode
{
char[];
int isdir;
int isopen;
charcontent[255];
fnode*parent;
fnode *child;
fnode *prev;
fnode*next;
};
b)函数介绍
fnode*init[],intisdir);//初始化文件或目录
voidcreateroot();//建立系统根目录
intrun();系统运行
intfindpara(char *topara);对参数进行处理
boolchklogin(char *users,char*pwd);检查账号与口令
voidhelp();命令列表
int mkdir();建立目录
int create();建立文件
intread();读取文件
intwrite();写入文件
int del();删除文件
int cd();切换目录
intdir();文件与目录列表
4、代码清单
#include"stdio、h"
#include”iostream、h"
#include"string、h”
#include "iomanip、h”
#define 10//文件名称长度
#defineMAND_LENGTH10//命令行长度
#definePARA_LENGTH30 //参数长度
//账号结构
typedefstructusers
{
char name[8];
char pwd[10];
}users;
//文件结构
structfnode
{
char[];
int isdir;
intisopen;
charcontent[255];
fnode*parent;
fnode*child;
fnode*prev;
fnode*next;
};
//账号
usersusrarray[8]=
{
”usr1","usr1",
"usr2",”usr2",
”usr3”,"usr3”,
"usr4”,”usr4",
"usr5”,"usr5",
"usr6”,”usr6",
”usr7",”usr7",
”usr8”,"usr8”,
};
fnode *init [],intisdir);
voidcreateroot();
intrun();
int findpara(char*topara);
bool chklogin(char*users, char*pwd);
voidhelp();
int mkdir();
intcreate();
int read();
intwrite();
int del();
intcd();
intdir();
fnode*root,*recent,*temp,*ttemp;
charpara[PARA_LENGTH],mand[MAND_LENGTH],temppara[PARA_LENGTH],recentpara[PARA_LENGTH];
//创建文件与目录结点
fnode*init[],intisdir)
{
fnode*node=newfnode;
strcpy(node—>);
node-〉isdir=isdir;
node-〉isopen=0;
node—>parent=NULL;
node—〉child=NULL;
node->prev=NULL;
node—〉next=NULL;
return node;
}
//创建文件存储结点
voidcreateroot()
{
recent=root=initfile(”/”,1);
root->parent=NULL;
root->child=NULL;
root—>prev=root—>next=NULL;
strcpy(para,”/");
}
intmkdir()
{
temp=initfile("",1);
cin〉>temp—>;
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->〉)==0&&ttemp->isdir==1)
{
printf(”对不起,目录已存在!
");
return1;
}
}
ttemp-〉next=temp;
temp-〉parent=NULL;
temp->child=NULL;
temp->prev=ttemp;
temp—〉next=NULL;
}
return1;
}
int create()
{
temp=initfile(" ",0);
cin>〉temp—〉;
gets(temp—>content);
//cin>>temp-〉content;
if(recent->child==NULL)
{
temp->parent=recent;
temp—>child=NULL;
recent->child=temp;
temp->prev=temp—>next=NULL;
cout<<”文件建立成功!
"〈〈endl;
}
else
{
ttemp=recent->child;
while(ttemp->next)
{
ttemp=ttemp-〉next;
if(strcmp(ttemp-〉〉)==0&&ttemp->isdir==0)
{
printf(”对不起,文件已存在!
”);
return1;
}
}
ttemp-〉next=temp;
temp-〉parent=NULL;
temp->child=NULL;
temp-〉prev=ttemp;
temp->next=NULL;
cout<<”文件建立成功!
"<<endl;
}
return1;
}
int dir()
{
int i=0,j=0;
temp=new fnode;
temp=recent;
if(temp!
=root)
{cout〈<” <DIR> ”<〈”、、"<<endl;i++;}
if(temp->child==NULL)
{
cout〈〈"Total:
”<〈"directors ”<<i〈<" files "〈< j〈return 1;
}
temp=temp->child;
while(temp)
{
if(temp->isdir)
{cout〈<" <DIR〉 "〈else
{cout<<" 〈FILE> ”〈〈temp-><〈endl;j++;}
temp=temp—〉next;
}
cout<<"Total:
”〈〈”directors " <
}
int read()
{
char[];
cin〉〉;
if(recent-〉child==NULL)
{
cout〈〈"文件不存在!
"<〈endl;
return1;
}
if(strcmp(recent->child—>)==0)
{
cout〈〈recent—〉child—>content〈<endl;
return1;
}
else
{
temp=recent->child;
while(temp—〉next)
{
if(strcmp(temp-〉next-〉)==0)
{cout〈〈temp—>next—>content〈 return 1;}
}
cout〈〈”文件不存在!
”〈 }
}
int write()
{
char[];
cin〉〉;
if(recent—>child==NULL)
{
cout〈<"文件不存在!
”〈〈endl;
return1;
}
if(strcmp(recent—〉child-〉)==0)
{
recent-〉child-〉isopen=1;//设置文件标记为打开
cin>>recent—〉child—〉content;
recent->child->isopen=0;//设置文件标记为关闭
cout〈〈"文件写入成功!
”〈<endl;
return1;
}
else
{
temp=recent—>child;
while(temp->next)
{
if(strcmp(temp-〉next—>)==0)
{
recent—>child->isopen=1;//设置文件标记为打开
cin>>temp-〉next-〉content;
recent->child—>isopen=0;//设置文件标记为关闭
cout<〈"文件写入成功!
"<〈endl;
return1;}
}
cout<〈”文件不存在!
"〈 }
}
intcd()
{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);
}
return1;
}
int findpara(char*topara)
{
inti=0;
intsign=1;
if(strcmp(topara,"/”)==0)
{
recent=root;
strcpy(para,"/”);
return1;
}
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〈=strlen(topara)&&recent)
{
intj=0;
if(topara[i]==’/’&& recent-〉child)
{
i++;
if(recent-〉isdir)
recent=recent-〉child;
else
{printf("路径错误\n”);
return0;
}
strcat(para,”/");
}
while(topara[i]!
='/’&&i〈=strlen(topara))
{
recentpara[j]=topara[i];
i++;j++;
}
recentpara[j]=’\0’;
while((strcmp(recent—>)!
=0||(recent—>isdir!
=1))&&recent—〉next!
=NULL)
{
recent=recent—>next;
}
if(strcmp(recent-〉)==0)
{
if(recent->isdir==0)
{strcpy(para,temppara);
recent=temp;
printf(”就是文件不就是目录.\n");
return0;
}
strcat(para,recent->);
}
if(strcmp(recent-〉)!
=0|| recent==NULL)
{
strcpy(para,temppara);
recent=temp;
printf("输入路径错误\n");
return 0;
}
}
return1;
}
int del()
{
char[];
cin>〉;
temp=newfnode;
if(recent—〉child)
{
temp=recent—>child;
while(temp—〉next&&(strcmp(temp—〉)!
=0||temp->isdir!
=0))
temp=temp->next;
if(strcmp(temp-〉)!
=0)
{
cout〈〈"不存在该文件!
”< return0;
}
}
else
{
cout<〈"不存在该文件!
”<〈endl;
return0;
}
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;
}
deletetemp;
cout<<”文件已删除!
”〈〈endl;
}
boolchklogin(char*users,char*pwd)
{
inti;
for(i=0; i〈8; i++)
{
if((strcmp(users,usrarray[i]、name)==0)&&(strcmp(pwd,usrarray[i]、pwd)==0))
return true;
}
returnfalse;
}
void help(void)
{
cout<<” 命令一 览 "〈cout<〈endl;
cout<<”create:
建立文件。
”〈〈endl;
cout〈〈”read:
读取文件。
”〈〈endl;
cout<<"write:
写入文件,支持多线程 "< cout〈<”del:
删除文件。
”<<endl;
cout〈<"mkdir:
建立目录。
"〈<endl;
cout<<"cd:
切换目录。
”〈〈endl;
cout〈〈”logout:
退出登录。
”<〈endl;
}
int run()
{
cout〈<”linux:
”〈<para〈〈"〉";
cin〉〉mand;
if(strcmp(mand,"mkdir")==0)
mkdir();
elseif(strcmp(mand,"dir")==0)
dir();
elseif(strcmp(mand,"cd")==0)
cd();
elseif(strcmp(mand,”create”)==0)
create();
else if(strcmp(mand,"read")==0)
read();
elseif(strcmp(mand,"write”)==0)
write();
elseif(strcmp(mand,"del")==0)
del();
elseif(strcmp(mand,”help”)==0)
help();
elseif(strcmp(mand,”logout")==0)
return0;
else
cout<〈"请参考help提供得命令列表!
"〈〈endl;
}
int main()
{
inti=0;
bool in=false;
charusers[8],pwd[12];
cout<<"***************************************************************"〈〈endl;
cout〈〈"* 简单得二级Linux文件系统 *"〈cout〈<"* 账号:
usr1-usr8密码:
usr1-usr8 *"〈cout<〈”* 您只有三次机会来试验账号 *"〈cout〈<”* 键入help可以获取帮助 *"<〈endl;
cout〈<”***************************************************************"<〈endl;
cout〈〈endl;
while(i〈3)
{
cout<<”Login:
”;
cin>〉users;
cout<〈"Pass:
";
cin〉>pwd;
if(chklogin(users,pwd))
{in=true;break;}
i++;
}
createroot();
while(in)
{
if(!
run())
break;
}
}
五、实验结果与分析
能够很成功得对文件进行读写操作,意见保存与删除
六、讨论总结
通过这次提高型实验,我明白了做一个系统得难度之大,首先要进行需求分析,然后通过ER图来清晰自己得思路,