024
操作系统课程设计_模拟UNIX文件系统的设计及实现样本
(样本只提供该系统的基本情况介绍,若需要完整的设计和论文,建议您购买本系统,凡是购买本站系统的,本站均根据您的要求,把系统上的开发信息,题目等修改成符合您的要求)
本设计包含内容:源代码+毕业论文
论文大概:
一、设计思想说明
本系统是模拟实现多用户多目录的文件系统(八个用户),在系统出现登录后
,输入用户与口令,在用户登录系统后,可建立文件卷,将用户输入的文件保存在指定的文件中。系统的命令与其命令的具体实现:
(1) login:用户登录;
(2) logout:
用户退出系统
(3) dir:列出当前目录下的所有文件及子目录
(4) cd:显示或改变当前的目录名
(5) mkdir:新建一个子目录
(6) create:新建一个文件
(7) del:删除一个文件
(8) open:查看一个文件
此模拟系统共提供了上述命令,并根据命令的含义与要求,用C++6.0编程来完成所有具体操作。该系统可以模拟完成用户的登陆和验证,列出文件和目录,新建目录,改变目录,创立和编写文件,删除文件和退出系统等功能。
二、系统结构说明
用户结构:登录
typedef struct User
//用户结构
{
char
name[15];
char
password[15];
}User;
本系统有8个默认的用户名,前面是用户名,后面为密码,用户登陆时只要输入正确便可进入系统,否则提示失败要求重新输入。
User
UserAndPass[8] =
"me","1111",
"usr1","0000",
"usr2","0000",
"usr3","0000",
"usr4","0000",
"usr5","0000",
"usr6","0000",
"usr7","0000",
三、数据结构说明
1、用户结构
typedef
struct User //用户结构
{
char name[
]; // 姓名
char password[
]; // 密码
}User;
2、系统结构
struct sys //系统结构
bool checkuser(char *user,
char *psw);
sys *createfile();
struct Files *create();
void
display(Files *file);
void insert(struct Files *file,struct Files
*newfile);
void deletef(Files *file,char name[20]);
void
cd(Files *file,char dect[20]);
int searchfile(sys *myfile,char
filename[30]);
void getlen(sys *myfile,char
filename[30]);
void read(sys *myfile,char filename[30]);
void
write(struct sys *myfile,struct sys *wrfile);
void
help(void);
四、各模块流程图
用户登录
验证
否
是
建立目录 建立文件
建立、删除 显示、删除
退出
五、程序清单
//***********************************************
//**
evaluating file system: Lch.cpp
**
//***********************************************
#include "LCH.h"
//默认的用户名和密码------------------------------------
User
UserAndPass[8]
=
{
"me","1111",
"usr1","0000",
"usr2","0000",
"usr3","0000",
"usr4","0000",
"usr5","0000",
"usr6","0000",
"usr7","0000",
};
//全局变量===================================================================
User *user;
int
nUserNo=0;
Files *file;
int no;
int
fnode=0;
sys *myfile;
char
current[20];
char predect[20];
char
nowdir[200];
int used;
//判断用户是否合法------------------------------------
bool
checkuser(char *user, char *psw)
{
int
i;
for(i=0; i<8; i++)
{
if(
(strcmp(user,UserAndPass[i].name)==0)
&&
(strcmp(psw,UserAndPass[i].password)==0)
)
return
true;
}
return false;
}
sys
*createfile()
{
int L=0;
sys *ps1;
sys
*pend1;
ps1=new
sys;
ps1->i=0;
strcpy(ps1->name,"root");
myfile=NULL;
pend1=ps1;
while(L!=1)
{
L=L+1;
if
(myfile==NULL)
myfile=ps1;
else
pend1->next
=ps1;
pend1=ps1;
ps1=new
sys;
ps1->i =1;
strcpy(ps1->name
,"lin");
}
pend1->next=NULL;
delete
ps1;
return(myfile);
}
//帮助信息------------------------------------
void
help(void)
{
cout<<"****"<<
endl;
cout<<"Help
Message:"<<endl;
cout<<"---"<<endl;
cout<<"dir:
list the files and directories in the current
directory"<<endl;
cout<<"create:
create and edit
File"<<endl;
cout<<"cd:
display current
directory"<<endl;
cout<<"mkdir:
create a new directory
"<<endl;
cout<<"open:
show file
content"<<endl;
cout<<"del:
delete a
File"<<endl;
cout<<"logout:
exit the system"<<endl;
cout<<
"__"<<endl;
}
//获取长度------------------------------------
long
filelength=0;
void getlen(sys *myfile,char
filename[30])
{
sys *dect=myfile;
filelength=0;
while(myfile)
{
if
((strcmp(myfile->name,filename)==0)&&(myfile->i>1)&&(strcmp(myfile->dect,current)==0))
{
filelength=filelength+strlen(myfile->memo);
}
myfile=myfile->next
;
}
}
//创建新文件------------------------------------
struct Files
*create()
{
int n=0;
Files *ps;
Files
*pend;
ps=new
Files;
ps->num=0;
strcpy(ps->name,"..");
file=NULL;
pend=ps;
while(n!=1)
{
n=n+1;
if
(file==NULL)
file=ps;
else
pend->next
=ps;
pend=ps;
ps=new
Files;
ps->num
=1;
strcpy(ps->name
,"..");
}
pend->next=NULL;
delete
ps;
return(file);
}
//读文件------------------------------------
void read(sys
*myfile,char filename[30])
{
sys
*dect=myfile;
filelength=0;
while(myfile)
{
if
((strcmp(myfile->name,filename)==0)&&(myfile->i>1)&&(strcmp(myfile->dect,current)==0))
{
cout<<myfile->memo<<endl;
filelength=filelength+strlen(myfile->memo);
}
myfile=myfile->next
;
}
cout<<endl<<endl;
}
//写文件------------------------------------
void write(struct
sys *myfile,struct sys *wrfile)
{
if
(myfile==NULL)
{
myfile=wrfile;
wrfile->next=NULL;
return;
}
if
(myfile->i >wrfile->i
)
{
wrfile->next
=myfile;
myfile=wrfile;
return;
}
struct
sys
*p=myfile;
while(p->next)
{
p=p->next
;
}
p->next=wrfile;
wrfile->next
=NULL;
}
//查找文件------------------------------------
int
searchfile(sys *myfile,char
filename[30])
{
while(myfile)
{
if
(strcmp(myfile->name,filename)==0)
{
return
1;
}
myfile=myfile->next
;
}
return 0;
}
//显示===================================================================
void
display(Files *file)
{
int fno=0;
int
dno=0;
int fcount=0;
Files
*dect=file;
cout<<endl;
while(file)
{
if
(file->num!=0)
{
if
(file->p==0&&strcmp(file->fadect,predect)==0)
{
getlen(myfile,file->name);
cout<<setiosflags(ios::left)<<setw(12)<<file->name<<setiosflags(ios::left)<<setw(20)<<":File"<<endl;
fcount=fcount+filelength;
fno=fno+1;
}
}
file=file->next;
}
while(dect)
{
if
((dect->num!=0)&&(strcmp(dect->fadect,predect)==0))
{
if
(dect->p==1)
{
cout<<setiosflags(ios::left)<<setw(12)<<dect->name<<setiosflags(ios::left)<<setw(20)<<":DIR"<<endl;
dno=dno+1;
}
}
dect=dect->next;
}
int
free;
free =32256 -
used;
cout<<endl;
cout<<setiosflags(ios::left)<<setw(25)<<"File
Number:"<<setiosflags(ios::left)<<setw(20)<<fno
<<used<<"
bytes"<<endl;
cout<<setiosflags(ios::left)<<setw(25)<<"Directorie
Number:"<<setiosflags(ios::left)<<setw(20)<<dno
<<free<<"
bytes free"<<endl;
}
//加入文件节点------------------------------------
void
insert(struct Files *file,struct Files *newfile)
{
if
(file==NULL)
{
file=newfile;
newfile->next=NULL;
return;
}
if
(file->num > newfile->num
)
{
newfile->next
=file;
file=newfile;
return;
}
struct
Files
*p=file;
while(p->next)
{
p=p->next
;
}
p->next=newfile;
newfile->next
=NULL;
}
//删除文件------------------------------------
void
deletef(Files *file,char name[20])
{
Files
*p;
if(!file)
{
cout<<"ERROR:
can not delete a dectory!
"<<endl<<endl;return;
}
if
(strcmp(file->name,name)==0)
{
if
(file->p
==0)
{
p=file;
file=file->next
;
used -=
file->length;
cout<<endl;
delete
p;
return;
}
}
for(Files
*pg=file;pg->next ;pg=pg->next )
{
if
(strcmp(pg->next->name
,name)==0)
{
used -=
pg->next->length;
if (pg->next->p
==0)
{
p=pg->next
;
pg->next =p->next
;
cout<<endl;
delete
p;
return;
}
}
}
cout<<"ERROR:
file not found. "<<endl<<endl;
}
//改变目录------------------------------------
void cd(Files
*file,char
dect[20])
{
while(file)
{
if((strcmp(file->name,dect)==0)&&(file->p==1))
{
strcpy(predect,current);
strcpy(current,dect);
strcat(nowdir,
dect);
strcat(nowdir,
"/");
return;
}
file=file->next
;
}
}
//主函数===================================================================
void
main()
{
cout<<"|-----------------------------------------------------------------|"<<endl;
cout<<"|
Welcome to use Evaluating unix file
system!
|"<<endl;
cout<<"|
--Operating system course design__
|"<<endl;
cout<<"|
|"<<endl;
cout<<"|
You can input Login: 'me' and Pass: '1111' to
login
|"<<endl;
cout<<"|_________________________________________________________________|"<<endl;
cout<<endl;
char luser[20];
char lpsw[20];
while(1)
{
cout<<"Login:
";
cin>>luser;
cout<<"Pass:
";
cin>>lpsw;
if(checkuser(luser,
lpsw))
{
break;
}
else
{
cout<<"Wrong
UserName or
PassWord!"<<endl;
}
}
struct
Files *addfile;
cout<<"welcome!! Login
Success!! "<<endl;
cout<<"You can type
'help' for help."<<endl;
cout<<"
"<<endl;
file=create();
myfile=createfile();
strcpy(current,"/");
strcpy(predect,"..");
strcpy(nowdir,"/");
used
=0;
begin:
char
command[10];
cout<<"["<<luser
<<"]:"<<nowdir
<<">";
cin>>command;
//列出文件和目录
if
(strcmp(command,"dir")==0)
{
display(file);
goto
begin;
}
//显示------------------------------------
if (strcmp(command,"open")==0)
{
char
filename[30];
cin>>filename;
int
r=searchfile(myfile,filename);
if(r==1)
{
read(myfile,filename);
}
else
{
cout<<"No
such
file!"<<endl;
}
goto
begin;
}
//帮助------------------------------------
if
(strcmp(command,"help")==0)
{
help();
goto
begin;
}
//删除文件------------------------------------
if (strcmp(command,"del")==0)
{
char
delname[20];
cin>>delname;
deletef(file,delname);
goto
begin;
}
//改变目录------------------------------------
char
dect[20];
if
(strcmp(command,"cd")==0)
{
cin>>dect;
if
(strcmp(dect,"/")==0)
{
strcpy(current,"/");
strcpy(predect,"..");
strcpy(nowdir,"/");
goto
begin;
}
else
{
cd(file,dect);
goto
begin;
}
}
//建立目录------------------------------------
if
(strcmp(command,"mkdir")==0)
{
no=no+1;
addfile=new
Files;
addfile->num
=no;
cin>>addfile->name;
addfile->p
=1;
addfile->length
=strlen(addfile->name);
strcpy(addfile->fadect,predect);
insert(file,addfile);
cout<<"Make
Directory Successful!"<<endl;
goto
begin;
}
//写文件内容------------------------------------
if
(strcmp(command,"create")==0)
{
fnode=fnode+1;
sys
*alterfile;
alterfile=new
sys;
alterfile->i
=fnode;
cin>>alterfile->name
;
int
r;
r=searchfile(myfile,alterfile->name);
if
(r!=1)
{
no=no+1;
addfile=new
Files;
char
filen[30];
addfile->num
=no;
strcpy(addfile->mode,"rw-r-");
strcpy(addfile->name
,alterfile->name);
strcpy(filen,addfile->name);
addfile->p
=0;
addfile->length
=0;
strcpy(addfile->fadect,predect);
sys
*appfile;
appfile=new
sys;
fnode=fnode+1;
appfile->i
=fnode;
strcpy(appfile->name,addfile->name);
strcpy(appfile->dect,current);
cin>>appfile->memo
;
write(myfile,appfile);
addfile->length
=strlen(appfile->memo);
used +=
addfile->length;
insert(file,addfile);
goto
begin;
}
cin>>alterfile->memo
;
used +=
strlen(alterfile->memo);
strcpy(alterfile->dect,current);
write(myfile,alterfile);
cout<<"File
Successful!"<<endl;
goto
begin;
}
//结束------------------------------------
if
(strcmp(command,"logout")==0)
{
return;
}
cout<<"Bad
Command! "<<endl<<endl;
goto
begin;
return;
}
六、程序使用说明书
1、 请把软盘中的lch.exe拷贝到电脑中,运行即可;
2、 进入程序请选择已有用户me,
password:1111,还有其它用户名:usr1——usr7,密码都为0000,只要输入到Login和Pass后便可进入。如下图:
3、 进入之后可以输入
“help”,根据帮助目录输入响应命令即可。如下图:
以下是各命令的说明:
[*]为当前用户名,上图用户为me,显示[me]:/>;如果用户为usr1,则显示为[usr1]:/>。
操作的命令如下:mkdir为建立目录命令,如要建立一个目录a,只需输入mkdir
a,进入目录a后显示为:[me]:/a/>,a表示当前目录;
dir 显示当前的目录和文件命令;
cd
为改变目录路径命令,要进入下一目录请输入cd +[目录],要返回到根目录请输入cd /
就可以,logout命令为退出系统命令。
如下图为建立多级子目录:
4、建立新文件:create建立并打开编辑目标文件命令,open打开并显示目标文件内容命令,del删除目标文件命令。在系统上建立并编辑一个文件,格式为create+[文件名],编辑完自动关掉文件。open+[文件名]为显示文件内容命令。del+[文件名]令为删除目标文件命令,如果要删除的目标文件不存在,系统将会提示该文件不存在。运行如下图所示: