当前位置:文档之家› 操作系统 文件系统 实验程序

操作系统 文件系统 实验程序

一 . 实验目的用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程。

从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。

二、实验要求要求设计一个n个用户的文件系统,每次用户可保存m个文件,用户在一次运行中只能打开一个文件,对文件必须设置保护措施,且至少有Create、delete、open、close、read、write等命令。

程序采用二级文件目录(即设置主目录[MFD])和用户文件目录(UFD)。

另外,为打开文件设置了运行文件目录(AFD)。

在执行读写命令时,需改读写指针。

因系统小,文件目录的检索使用了简单的线性搜索。

文件保护简单使用了三位保护码:允许读写执行、对应位为1,对应位为0,则表示不允许读写、执行。

程序中使用的主要设计结构如下:主文件目录和用户文件目录(MFD、UFD)、打开文件目录(AFD)(即运行文件目录)文件系统算法的流程图如下:#include<stdio.h>#include<string.h>#define N 10#define L 10#define S 6struct mfd_type{char uname[20];int uaddr;};struct ufd_type{char fname[20];char fattr[10];int recordl;int addrf;};struct uof_type{char fname[20];char fattr[10];int recordl;char fstatue;//1:建立0:打开int readp;int writep;};int fdph[N*L];//存放文件在磁盘上的第一个空间编号int fdpt[N*L];//存放文件在磁盘上的最末一个空间编号int disk[400];//存放磁盘空闲块号int diskt,diskh;//空闲块的尾、首指针最末struct mfd_type mfd[N];struct ufd_type ufd[N *L];struct uof_type uof[N *S];int temp,mi,ni,uno ,ucounter;void create(char cfname[20],int crecordl,char cfattr[10]){int cltemp1=uno*L,cltemp2=(uno+1)*L,frd;while ((cltemp1<cltemp2)&&(strcmp(ufd[cltemp1].fname,cfname)!=0))cltemp1++;if(strcmp(ufd[cltemp1].fname,cfname)!=0){cltemp1=uno*L;while ((cltemp1<cltemp2)&&(strcmp(ufd[cltemp1].fname,"")!=0)) cltemp1++;if(strcmp(ufd[cltemp1].fname,"")==0){frd=cltemp1;cltemp1=uno*S;cltemp2=(uno+1)*S;while ((cltemp1<cltemp2)&&(strcmp(uof[cltemp1].fname,"")!=0)) cltemp1++;if(strcmp(uof[cltemp1].fname,"")==0){if (disk[diskh]!=-1){strcpy(uof[cltemp1].fname,cfname);strcpy(uof[cltemp1].fattr,cfattr);uof[cltemp1].recordl=crecordl;uof[cltemp1].fstatue=1;uof[cltemp1].readp=disk[diskh];uof[cltemp1].writep=disk[diskh];fdph[frd]=disk[diskh];fdpt[frd]=disk[diskh];strcpy(ufd[frd].fname,cfname);strcpy(ufd[frd].fattr,cfattr);ufd[frd].recordl=crecordl;ufd[frd].addrf=disk[diskh];diskh=disk[diskh];printf("%s\n","文件建立成功!");}else printf("%s\n","磁盘没有空间,不能建文件!");}else printf("%s\n","没有空的登记拦1,不能建文件");}else printf("%s\n","没有空的登记拦2,不能建文件");}else printf("%s\n","同名文件不能建立!");};void open(char pfname[20],char ooptype[10]){int cltemp1=uno*L,cltemp2=(uno+1)*L;int cltemp3=uno*S,cltemp4=(uno+1)*S;while ((cltemp1<cltemp2)&&(strcmp(ufd[cltemp1].fname,pfname)!=0)) cltemp1++;if(strcmp(ufd[cltemp1].fname,pfname)==0){while ((cltemp3<cltemp4)&&(strcmp(uof[cltemp3].fname,pfname)!=0)) cltemp3++;if(strcmp(uof[cltemp3].fname,pfname)==0)//有文件{if(uof[cltemp3].fstatue==0) printf("%s\n","文件已打开!");else printf("%s\n","此文件正在建立,不能打开!");}else//无文件{if(strcmp(ufd[cltemp1].fattr,ooptype)!=0) printf("%s\n","操作不合法,不能打开!");else{cltemp3=uno*S;while ((cltemp3<cltemp4)&&(strcmp(uof[cltemp3].fname,"")!=0)) cltemp3++;if(strcmp(uof[cltemp3].fname,""))printf("%s\n","在已开表中没有空拦,不能打开文件!");else{strcpy(uof[cltemp3].fname,pfname);uof[cltemp3].recordl=ufd[cltemp1].recordl;strcpy(uof[cltemp3].fattr,ufd[cltemp1].fattr);uof[cltemp3].readp=ufd[cltemp1].addrf;uof[cltemp3].writep=ufd[cltemp1].addrf;uof[cltemp3].fstatue=0;printf("%s\n","打开文件成功!");}}}}elseprintf("%s\n","此文件已不存在,不能打开!");};void write(char wfname[20],int wrecordno){int cltemp1=uno*L,cltemp2=(uno+1)*L;int cltemp3=uno*S,cltemp4=(uno+1)*S;while ((cltemp3<cltemp4)&&(strcmp(uof[cltemp3].fname,wfname)!=0)) cltemp3++;if(strcmp(uof[cltemp3].fname,wfname)) printf("文件没有建立或打开,不能写");//在已开文件中没有次文件,不能写else//文件建立或打开{if(uof[cltemp3].fstatue)//建立{printf("%s%d%s","写第",uof[cltemp3].writep,"块空间!");if(disk[diskt]==-1)printf("%s\n","没有空闲块不能写!");else{uof[cltemp3].writep=disk[diskt];diskt=disk[diskt];printf("%s\n","写文件成功!");}}else//打开{if(strcmp(uof[cltemp3].fattr,"r")==0)printf("%s\n","操作不合法,不能写!");else{if(disk[diskt]==-1)printf("%s\n","没有空闲块不能写!");else{printf("%s%d%s\n","写第",uof[cltemp3].writep,"块空间!");uof[cltemp3].writep=disk[uof[cltemp3].writep];printf("%s\n","写文件成功!");}}}}};void read(char rfname[20],int rreadl){int cltemp1=uno*S,cltemp2=(uno+1)*S,frd;while ((cltemp1<cltemp2)&&(strcmp(uof[cltemp1].fname,rfname)!=0)) cltemp1++;if(strcmp(uof[cltemp1].fname,rfname)==0){printf("%s%d%s\n"," 第",uof[cltemp1].readp,"块读");frd=fdph[uno*L];while ((cltemp2!=disk[frd])&&(disk[frd]!=-1)) frd=disk[frd];uof[cltemp1].readp=disk[frd];printf("%s\n","文件读成功"); }else printf("%s\n","文件未打开不能读");};void close(char cofname[20]){int cltemp1=uno*S,cltemp2=(uno+1)*S;while ((cltemp1<cltemp2)&&(strcmp(uof[cltemp1].fname,cofname)!=0)) cltemp1++;if(strcmp(uof[cltemp1].fname,cofname)==0){printf("uof[cltemp1].fname=%s,cofname%s\n",uof[cltemp1].fname,cofname);cltemp2=uof[cltemp1].fstatue;printf("uof[cltemp1].fstatue=%d\n",cltemp2);switch (cltemp2){case 1:printf("%s\n","写文件结束符。

相关主题