国开 操作系统 实验4:文件管理实验

合集下载

操作系统-第四次实验报告-文件系统

操作系统-第四次实验报告-文件系统
操作系统实验报告 文件系统
全部代码可执行
实验介绍
本实验要求在假设的I/O 系统之上开发一个简单的文件系统, 这样做既能让实验者对文 件系统有整体了解,又避免了涉及过多细节。用户通过create, open, read 等命令与文件系统 交互。文件系统把磁盘视为顺序编号的逻辑块序列,逻辑块的编号为0 至L-1。I/O 系统利 用内存中的数组模拟磁盘。 实际物理磁盘的结构是多维的:有柱面、磁道、扇区等概念。I/O 系统的任务是隐藏磁 盘的结构细节,把磁盘以逻辑块的面目呈现给文件系统。逻辑块顺序编号,编号取值范围为 0 至L -1,其中L 表示磁盘的存储块总数。实验中,我们可以利用字符数组ldisk[L][B] 构 建磁盘模型,其中B 表示每个存储块的长度。I/O 系统从文件系统接收命令,根据命令指定 的逻辑块号把磁盘块的内容读入命令指定的内存区域, 或者把命令指定的内存区域内容写入 磁盘块。内存区域内容写入磁盘块。
整体组织
注:我定义的文件系统中,磁盘分为两大部分:数据区和保留区。其中保留区中又包含位图区和文件 描述符区,数据区的首部是文件的目录项,也就是说,文件的目录项在文件创建时会创建相应的目录 项在数据区的文件首部;而位图区用于表征数据的占用情况,例如数据区的第 N 块被分配了,那么位 图区中也要做相应的改变。
struct filesign { int file_length; int filesign_flag; int file_block;
int file_block_ary[FILE_BLOCK_LENGTH]; }; struct contents { char filename[FILE_NAME_LENGTH];
文件的读 int read(int,int,int)

操作系统文件管理实验报告

操作系统文件管理实验报告

竭诚为您提供优质文档/双击可除操作系统文件管理实验报告篇一:操作系统实验报告文件管理昆明理工大学信息工程与自动化学院学生实验报告(201—201学年第二学期)课程名称:操作系统开课实验室:年月日一、实验目的用c或c++语言编写和调试一个简单的文件系统,模拟文件管理的基本功能。

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

二、实验原理及基本技术路线图(方框原理图)用c模拟实现文件系统的管理;要求设计一个多级目录结构的文件系统,能正确描述文件控制块,采用合理的外存分配方式,能实现基本的目录及文件的操作,包括创建、删除、重命名、复制、移动等功能,并对文件有一定的存取权限控制。

功能设计:helpdirexitcreate[文件名]cdir[目录名]read[文件名]close[文件名]edit[文件名]cd[目录名]attr[文件名]del[文件名]rename[文件名]显示命令帮助显示当前目录下的文件和文件夹退出系统创建文本文件创建文件夹读取一个文件最多可同时读取五个关闭一个文件编辑一个文件进子目录或者上级目录显示该文件的属性删除文件重命名编辑功能流程图删除文件流程图创建文件流程图核心算法:boolFormat(void);//格式化boolinstall(void);//装载虚拟硬盘的数据voidlogin(void);/用户登陆voidshowmenu(void);//显示功能菜单boolonAction(void);//用户选择功能并执行voidcreateFile(stringstr);//创建文件boolread(stringstr);//读取文件voideditFile(stringstr);//编辑文件voidDelete(stringstr);//删除一个文件数据结构:始地址/*inode结构体*/structinode{unsignedshortdi_tag;/*inode标识*/ /*关联文件数,当为0时表示删除文件,如一个目录至少包含两个文件:"."和".."*/constunsignedintAccounT_num=10;//用户数量/*---------常变量------*/constunsignedintbLocK_sIZe=512;//块长constunsignedintDATA_bLocK_num=512;//数据块数量constunsignedintDInoDe_sTART=4*bLocK_sIZe;//inode起始位置constunsignedintDInoDe_sIZe=512;constunsignedintDIn oDe_num=32;//inode大小//inode数量constunsignedintDATAsTART=(2+DInoDe_num)*bLocK_sIZe ;//数据区的开unsignedshortdi_number;unsignedshortdi_mode;/*存取模式:0为目录,1为文件*/unsignedshortdi_userID;/*当前inode所属用户0为根目录ID,一次下去是管理员目录、用户目录*/unsignedshortdi_access;/*访问权限0为不允许普通用户访问(公共目录),1为允许普通用户访问*/};unsignedshortdi_size;/*文件大小,目录没有大小,值为0*/unsignedshortdi_ctime;/*创建时间*/unsignedshortdi_mtime;/*最后一次修改时间*/unsignedshortdi_block[DATA_bLocK_num];/*数据块块地址编号*//**超级块***/structsuper_block{};/**账户信息**/structuser{};/**文件/目录结构**/structdirectory{unsignedshorts_inodes_count;/*文件系统中inode的总数*/unsignedshorts_blocks_count;/*数据块总数*/unsignedshorts_r_blocks_count;/*保留块总数*/unsignedshorts_free_blocks_count;//空闲块总数unsignedshorts_free_inodes_count;/*空闲的inode总数*/unsignedshorts_log_block_size;/*block的大小*/unsignedshortuser_id;//用户IDunsignedshortuser_access;//权限stringusername;stringpassword;//用户名//密码篇二:操作系统课程设计-文件管理实验报告操作系统课程实验报告20XX~20XX年度第1学期院系:学号:姓名:任课教师:成绩评定:实验一题目:文件管理完成日期:年月日1、实验目的了解文件管理的功能和任务,理解文件系统组成和特点,熟悉文件系统的访问和操作。

操作系统文件管理实验报告

操作系统文件管理实验报告

操作系统文件管理实验报告电大教师评语教师签字日期成绩学生姓名学号实验名称操作系统文件管理实验实验报告一、实验目的1、加深对文件、目录、文件系统等概念的理角2、掌握 Llnu_ 文件系统的目录结构3、掌握有关 Llnu_ 文件系统操作的常用命令4、了解有关文件案例性方面的知识二、实验要求:1、正确使用文件管理命令,能熟练地对文件进行浏览、拷贝、移动和删除。

2、能熟练地确定和更改工作目录,查看内容和文件属性,创建和删除目录。

3、正确地理解文件的权限,并能进行相应更改。

4、理解文件的类型及其表示形式。

5、理解文件名的表示形式,在模式匹配中能正确使用通配符。

三、实验步骤( 一) 浏览文件系统 1、运行 pwd 命令。

确定当前的工作目录。

2、文件列表。

使用 ls 命令显示当前工作目录的内容。

使用 ls-l 命令显示文件列表,对照屏幕上的显示信息,理解各字段的含义。

使用 ls-ai 命令显示文件列表,观察显示的信息。

注意列出的前两项,它们分别表示当前目录和父目录。

看一下,是否还有其他隐藏的文件? 运行不带-a 选项的 ls 命令。

注意,文件的 I 节点号不再出现。

运行不带-i 选项的 ls 命令,注意,怎么不再显示隐藏文件了。

3、目录操作。

使用 mkdir 命令建立一个子目录。

执行带 ls-d 命令,观察有关子目录的信息。

执行 cd/命令,将工作目录改到根目录(/)上。

使用绝对路径和相对路径两种方式,并执行 pwd 命令确认当前工作目录。

执行 ls/命令列出根目录的内容,再分别列出各子目录的内容。

体会各子目录的作用以及访问权限。

执行 ls-l /dev 命令列出/dev 的内容,注意各行第一个字符 b 和c 的含义以及规定的访问权限。

执行不带参数的命令 cd,然后用 pwd 命令确认当前的工作目录是什么。

执行命令 cd../...工作目录移到什么地方了? ( 二) 查看文件执行 cd 命令,将工作目录改到主目录上。

操作系统文件管理实验报告

操作系统文件管理实验报告

实验报告课程名称计算机操作系统实验名称文件管理班级学号姓名成绩指导教师赵安科实验日期2015年6月18日一.实验题目:文件管理二.实验内容:模拟文件存储空间的管理,采用空白文件目录法和空白块链法实施空间分配。

三.实验说明:文件存储空间管理是文件系统的重要内容。

常用的管理思想有空白文件目录法、空白块链法和位示图法。

本实验采用前两种方法进行空间分配。

(1)空白文件目录法进行空间分配时,需要建立相关的数据结构,记录目前空白区域和已使用区域,假设开始时全部区域空闲。

当有文件需要存储时,先检查空白文件目录,找到适合区域立即分配,并修改空白文件目录表和已使用区域分配表。

为此需建立两张表格,分别记录相关数据。

空白文件目录表(初始)序号首空白块号空白快个数物理块号备注1 0 100 0,1,2,……,98,99空白文件目录(中间)序号首空白块号空白块个数物理块号标志1 2 4 2,3,4,5 未分配2 93 9,10,11 未分配3 25 5 25,26,27,28,29 未分配4 39 2 39,40 未分配5 ………………未分配已使用区域表(中间)上述两张表的数据在系统运行中是发生变化的。

文件标识首块号文件块个数状态备注beta 0 2 占用Alpha 6 3 占用Toyota 12 13 占用Sony 30 9 占用Ford 50 4 占用……………………文件空闲区分配和释放算法如下图所示:图一文件空闲区分配算法图二文件空闲区回收算法(2)空白块链法进行空间分配时,需要建立链表数据结构,将空闲块按顺序加以组织,分配和回收时在链首完成,同时建立文件目录,记录文件占用空间情况。

文件标识首块号备注beta 0Alpha 6Toyota 12Sony 30Ford 50…………四.实验要求:(1)自拟模拟数据演示运行结果(假定系统可用空闲块数为100)。

为便于检查,建立和删除文件顺序如下:分配文件:F1,3分配文件:F2,5分配文件:F3,3分配文件:F4,8分配文件:F5,4分配文件:F6,2删除文件:F1删除文件:F2分配文件:F7,6删除文件:F3分配文件:F8,4删除文件:F5分配文件:F9,4……每完成一个文件的分配和删除后,显示空白文件目录当前内容。

操作系统文件管理系统模拟实验

操作系统文件管理系统模拟实验

操作系统文件管理系统模拟实验操作系统文件管理系统模拟实验一、实验目的本实验旨在通过模拟操作系统的文件管理系统,加深对操作系统文件管理的理解,锻炼操作系统的应用能力。

二、实验环境1、操作系统:Windows/Linux/MacOS2、编程语言:C/C++/Java/Python等三、实验内容1、初始化文件管理系统1.1 创建根目录,并初始化空文件目录1.2 初始化用户目录和权限设置2、文件操作2.1 创建文件2.1.1 检查文件名合法性2.1.2 检查文件是否已存在2.1.3 为新文件分配磁盘空间2.1.4 添加文件元数据信息2.2 打开文件2.2.1 检查文件是否存在2.2.2 检查用户权限2.3 读取文件内容2.3.1 读取文件权限检查2.3.2 读取文件内容2.4 写入文件内容2.4.1 写入文件权限检查2.4.2 写入文件内容2.5 删除文件2.5.1 检查文件是否存在2.5.2 检查用户权限2.5.3 释放文件占用的磁盘空间2.5.4 删除文件元数据信息3、目录操作3.1 创建子目录3.1.1 检查目录名合法性3.1.2 检查目录是否已存在3.1.3 添加目录元数据信息3.2 打开目录3.2.1 检查目录是否存在3.2.2 检查用户权限3.3 列出目录内容3.3.1 列出目录权限检查3.3.2 列出目录内容3.4 删除目录3.4.1 检查目录是否存在3.4.2 检查用户权限3.4.3 递归删除目录下所有文件和子目录3.4.4 删除目录元数据信息四、实验步骤1、根据实验环境的要求配置操作系统和编程语言环境。

2、初始化文件管理系统,创建根目录,并初始化用户目录和权限设置。

3、进行文件操作和目录操作。

五、实验结果分析根据实验步骤进行文件操作和目录操作,观察系统的运行情况并记录相关实验结果。

六、实验结论通过本实验,深入了解了操作系统中文件管理系统的相关原理和实现方式,并且通过实验操作进一步巩固了相应的应用能力。

实验四 文件管理实验

实验四  文件管理实验

实验四文件管理实验◆实验名称:文件管理实验◆仪器、设备:计算机◆参考资料:操作系统实验指导书◆实验目的:设计一个n个用户的文件系统,用户在一次运行中只能打开一个文件,有Create、delete、open、close、read、write等命令。

◆实验内容:为DOS系统设计一个简单的二级文件系统。

要求可以实现下列几条命令CREATE 创建文件DELETE 删除文件OPEN 打开文件CLOSE 关闭文件READ 读文件WRITE 写文件◆实验原理、数据(程序)记录:#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/#define MAXCHILD 50 /*the largest child*/#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/void CreateF() /*Create File*/{int fpaddrno,flag=1,i;char fname[MAXNAME],str[50],str1[50],strtext[255],a[25];char fmode[25];int FindPANo(); /*find out physical address num*/int WriteF1(); /*write file*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(dirname),strupr(username))!=0){printf("\nError. You must create file in your own dir.\n");wgetchar=1;}else{ printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));if (ExistF(fname)>=0){printf("\nError. Name \'%s\' has already existed.\n",fname);wgetchar=1;} else{printf("Please input FileMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):");gets(fmode);ltrim(rtrim(fmode));if((strcmp(fmode,"0")==0)||(strcmp(fmode,"1")==0)||(strcmp(fmode,"2")==0)||(strcmp(fmode ,"3")==0)){fpaddrno=FindPANo();if (fpaddrno>=0){i=ExistD(username);strcpy(ufd[i]->ufdfile[fcount[i]].fname,fname);ufd[i]->ufdfile[fcount[i]].fpaddr=fpaddrno;ufd[i]->ufdfile[fcount[i]].fmode=atoi(fmode);ifopen[i][fcount[i]].ifopen=0;ifopen[i][fcount[i]].openmode=4;strcpy(str,"c:\\osfile\\file\\file");itoa(fpaddrno,str1,10);strcat(str,str1);fp_file=fopen(str,"wb");fclose(fp_file);fcount[i]++;while(flag){printf("Input text now(Y/N):");gets(a);ltrim(rtrim(a));ufd[i]->ufdfile[fcount[i]-1].flength=0;if(strcmp(strupr(a),"Y")==0){fp_file=fopen(str,"wb+");ufd[i]->ufdfile[fcount[i]-1].flength=WriteF1();flag=0;} else if(strcmp(strupr(a),"N")==0){flag=0;wgetchar=1;}} printf("\n\'%s\' has been created successfully!\n",fname);} else{printf("\nFail!No Disk Space. Please format your disk.\n");wgetchar=1;}} else {printf("\nError. FileMode\'s Range is 0-3\n");wgetchar=1;}}}}int ExistF(char *filename) /*Whether FileName Exist,Exist-i,Not Exist-0*/{int i,j;int exist=0;int ExistD(char *dirname);j=ExistD(dirname);for(i=0;i<fcount[j];i++)if (strcmp(strupr(ufd[j]->ufdfile[i].fname),strupr(filename))==0){exist=1;break;}if (exist) return(i);else return(-1);}int FindPANo() /*find out physical address num*/{int i;for(i=0;i<MAX;i++)if (fpaddrno[i]==0) {fpaddrno[i]=1;break;}if (i<MAX) return(i);else return(-1);}int WriteF1() /*write file*/{int length=0;char c;printf("Please input text(\'#\' stands for end):\n");while((c=getchar())!='#'){fprintf(fp_file,"%c",c);if (c!='\n') length++;} fprintf(fp_file,"\n");fclose(fp_file);return(length);}******************************************************************************* void DeleteF() /*Delete File*/{char fname[MAXNAME];char str[50],str1[50];int i,j,k,flag=1;char a[25]; /*whether delete*/char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(dirname),strupr(username))!=0){printf("\nError. You can only delete file in your own dir.\n");wgetchar=1;}else{printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));i=ExistF(fname);if (i>=0){k=ExistD(username);if(ifopen[k][i].ifopen==1){printf("\nError. \'%s\' is in open status. Close it before delete.\n",fname);wgetchar=1;}else{while(flag){printf("\'%s\' will be deleted. Are you sure(Y/N):",fname);gets(a);ltrim(rtrim(a));if(strcmp(strupr(a),"Y")==0){fpaddrno[ufd[k]->ufdfile[i].fpaddr]=0;itoa(ufd[k]->ufdfile[i].fpaddr,str,10);for(j=i;j<fcount[k]-1;j++){strcpy(ufd[k]->ufdfile[j].fname,ufd[k]->ufdfile[j+1].fname);ufd[k]->ufdfile[j].fpaddr=ufd[k]->ufdfile[j+1].fpaddr;ufd[k]->ufdfile[j].flength=ufd[k]->ufdfile[j+1].flength;ufd[k]->ufdfile[j].fmode=ufd[k]->ufdfile[j+1].fmode;ifopen[k][j]=ifopen[k][j+1];}fcount[k]--;strcpy(str1,"c:\\osfile\\file\\file");strcat(str1,str);remove(str1);flag=0;printf("\n\'%s\' has been deleted successfully.\n",fname);wgetchar=1;}else if(strcmp(strupr(a),"N")==0){printf("\nError. \'%s\' hasn\'t been deleted.\n",fname);wgetchar=1;flag=0;}}}}else {printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}}******************************************************************************* void OpenF() /*Open File*/{char fname[MAXNAME];char str[25],str1[25],fmode[25];int i,k;char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0){printf("\nError. Please change to ufd dir before open.\n");wgetchar=1;return;}printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));i=ExistF(fname);if (i>=0){k=ExistD(dirname);if(!ifopen[k][i].ifopen){if (ufd[k]->ufdfile[i].fmode==3){printf("\nError. The file\'s mode is FORBID. Can not open.\n");wgetchar=1;}else{printf("Please input FileOpenMode(0-Read Only,1-Write Only,2-Read and Write):");gets(fmode);ltrim(rtrim(fmode));if((strcmp(fmode,"0")==0)||(strcmp(fmode,"1")==0)||(strcmp(fmode,"2")==0)){if(fmode[0]=='0') /*open file with read only mode*/{strcpy(str,"read only");if((ufd[k]->ufdfile[i].fmode==0)||(ufd[k]->ufdfile[i].fmode==2)) ifopen[k][i].ifopen=1;}else if(fmode[0]=='1') /*open file with write only mode*/{strcpy(str,"write only");if((ufd[k]->ufdfile[i].fmode==1)||(ufd[k]->ufdfile[i].fmode==2)) ifopen[k][i].ifopen=1;}else if(fmode[0]=='2') /*open file with read and write mode*/{strcpy(str,"read and write");if(ufd[k]->ufdfile[i].fmode==2) ifopen[k][i].ifopen=1;}if(ufd[k]->ufdfile[i].fmode==0) strcpy(str1,"read only"); /*FileMode*/else if(ufd[k]->ufdfile[i].fmode==1) strcpy(str1,"write only");else if(ufd[k]->ufdfile[i].fmode==2) strcpy(str1,"read and write");if(ifopen[k][i].ifopen==1){ifopen[k][i].openmode=atoi(fmode);if (ifopen[k][i].openmode==0) strcpy(str,"read only");else if(ifopen[k][i].openmode==1) strcpy(str,"write only");else if(ifopen[k][i].openmode==2) strcpy(str,"read and write");printf("\n\'%s\' has been opened. OpenMode is %s,FileModeis %s\n",fname,strupr(str),strupr(str1));wgetchar=1;} else{printf("\nError. \'%s\' hasn\'t been opened. OpenMode Error. OpenMode is %s,but FileMode is %s\n",fname,strupr(str),strupr(str1));wgetchar=1;}} else {printf("\nError. FileOpenMode\'s Range is 0-2\n");wgetchar=1;}}} else {printf("\nError. \'%s\' is in open status.\n",fname);wgetchar=1;}} else{printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}******************************************************************************* void CloseF() /*Close File*/{int i,k,n=0;char fname[MAXNAME];char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0){printf("\nError. Please convert to ufd dir before close.\n");wgetchar=1;return;}k=ExistD(dirname);printf("\nOpen File(s) In This Ufd:\n"); /*display openned file*/for(i=0;i<fcount[k];i++){if (ifopen[k][i].ifopen==1) {printf("%15s",ufd[k]->ufdfile[i].fname);n++;}if((n%4==0)&&(n!=0)) printf("\n");} printf("\n%d files openned.\n",n);if (n==0) wgetchar=1;if(n!=0){printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));i=ExistF(fname);if(i>=0){if(ifopen[k][i].ifopen==1){ifopen[k][i].ifopen=0;ifopen[k][i].openmode=4;printf("\n\'%s\' has been closed successfully.\n",fname);wgetchar=1;} else {printf("\nError.\'%s\' is in closing status.\n",fname);wgetchar=1;}} else {printf("\nError. \'%s\' is not exist.\n",fname);wgetchar=1;}}}******************************************************************************* void ReadF() /*Read File*/{int i,k,n=0;char fname[MAXNAME];char str[255],str1[255],c;char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) {printf("\nError.Please convert to ufd dir before read.\n");wgetchar=1;return;}printf("\nCaution:Open file first\n");printf("Opened File(s) List:\n");k=ExistD(dirname);for(i=0;i<fcount[k];i++){if (ifopen[k][i].ifopen==1)if ((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2)){printf("%15s",ufd[k]->ufdfile[i].fname);n++;}if((n%4==0)&&(n!=0)) printf("\n");}printf("\n%d files openned.\n",n);if (n==0) wgetchar=1;if(n!=0){printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));i=ExistF(fname);if(i>=0){if(ifopen[k][i].ifopen==1){if((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2)){itoa(ufd[k]->ufdfile[i].fpaddr,str,10);strcpy(str1,"file");strcat(str1,str);strcpy(str,"c:\\osfile\\file\\");strcat(str,str1);fp_file=fopen(str,"rb");fseek(fp_file,0,0);printf("\nThe text is:\n\n");printf(" ");while(fscanf(fp_file,"%c",&c)!=EOF)if (c=='\n') printf("\n ");else printf("%c",c);printf("\n\n%d Length.\n",ufd[k]->ufdfile[i].flength);fclose(fp_file);wgetchar=1;}else{printf("\nError.\'%s\' has been opened with WRITE ONLY mode. It isn\'tread.\n",fname);wgetchar=1;}}else {printf("\nError.\'%s\' is in closing status. Please open it beforeread\n",fname);wgetchar=1;}}else {printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}}******************************************************************************* void WriteF() /*Write File*/{int i,k,n=0;char fname[MAXNAME];char str[50],str1[50],a[50];char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);int WriteF1(); /*write file*/if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) {printf("\nError. Please convert to ufd dir before write.\n");wgetchar=1;return;}k=ExistD(dirname);printf("\nOpen File(s) with write only mode or read and write mode:\n");/*display openned files with writable mode*/for(i=0;i<fcount[k];i++){if (ifopen[k][i].ifopen==1)if ((ifopen[k][i].openmode==1) ||(ifopen[k][i].openmode==2)){printf("%15s",ufd[k]->ufdfile[i].fname);n++;}if((n%4==0)&&(n!=0)) printf("\n");}printf("\n%d files open.\n",n);if (n==0) wgetchar=1;if(n!=0){printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));i=ExistF(fname);if(i>=0){if(ifopen[k][i].ifopen==1){if((ifopen[k][i].openmode==1) ||(ifopen[k][i].openmode==2)){itoa(ufd[k]->ufdfile[i].fpaddr,str,10);strcpy(str1,"file");strcat(str1,str);strcpy(str,"c:\\osfile\\file\\");strcat(str,str1);if (ufd[k]->ufdfile[i].flength!=0){printf("\n\'%s\' has text. Overwrite or Append(O-overwrite,A-Append,else-not write):",fname);gets(a);ltrim(rtrim(a));if (fp_file!=NULL) fclose(fp_file);if (strcmp(strupr(a),"O")==0){printf("\nOverwrite\n");fp_file=fopen(str,"wb");ufd[k]->ufdfile[i].flength=0;ufd[k]->ufdfile[i].flength=WriteF1();}else if(strcmp(strupr(a),"A")==0){printf("\nAppend\n");fp_file=fopen(str,"ab");ufd[k]->ufdfile[i].flength=ufd[k]->ufdfile[i].flength+WriteF1();}else{printf("\nError.\'%s\' has not been written.\n",fname);fclose(fp_file);wgetchar=1;} }else{fp_file=fopen(str,"wb");ufd[k]->ufdfile[i].flength=WriteF1();}}else{printf("\nError. \'%s\' has been opened with read only mode.It isn\'twrited.\n",fname);wgetchar=1;}}else{printf("\nError. \'%s\' is in closing status. Please open it beforewrite\n",fname);wgetchar=1;}}else{printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}}◆实验结果及分析通过实验实现了有Create、delete、open、close、read、write等命令的简单的文件系统。

操作系统课程设计-文件管理实验报告

操作系统课程设计-文件管理实验报告

操作系统课程实验报告2013~2014年度第1学期院系:学号:姓名:任课教师:成绩评定:实验一题目:文件管理完成日期:年月日1、实验目的了解文件管理的功能和任务,理解文件系统组成和特点,熟悉文件系统的访问和操作。

实验要求用高级语言编写和调试一个简单的模拟文件管理程序。

加深理解有关盘块的分配与回收、目录管理等的具体实施策略。

2.、实验内容模拟一个资源管理器进行文件操作,包括建立和删除目录、建立和删除文件等基本文件操作。

建立相应的数据结构(如:位示图等),模拟盘块管理。

可以参照图6界面进行设计。

3、算法设计1)、定义主面板MainFrame,布局好各个控件,并初始化/** 往node节点下添加一个子节点obj;*/public void addChild(Object obj, DefaultMutableTreeNode node) {if (obj != null && node != null) {DefaultMutableTreeNode temp = new DefaultMutableTreeNode(obj);if (node.getAllowsChildren())node.add(temp);if(!((String) obj).equals("A:\\") && ((String) obj).length() <= 3)// 防止读取A软驱,会出现异常;用于初始用的;addChildren(cmd.listAll((String) obj), temp);}}/** 在node节点下添加数组children;*/public void addChildren(String[] children, DefaultMutableTreeNode node) { if (children != null && node != null) {for (int i = 0; i < children.length; i++) {addChild(children[i], node);}}}/** 对树的节点进行预提取;*/public void addPrefetchChildren(String path, DefaultMutableTreeNode node) { addChildren(cmd.listDirectory(path), node);}/** 对路径路径进行连接;(已经获得了所有的整个路径,需要量转化)*/public String toFilePath(String str) {// 先去掉头尾的[];String pa = str.substring(1, str.length() - 1);String[] temp = pa.split(", ");String path = "";for (int i = 1; i < temp.length; i++) {if (!path.endsWith("\\") && !path.equals(""))// 不为空是为去根节点;path += "\\";path += temp[i];}return path;}public String toPFilePath(String str) {// 先去掉头尾的[];String pa = str.substring(1, str.length() - 1);String[] temp = pa.split(", ");String path = "";for (int i = 1; i < temp.length - 1; i++) {if (!path.endsWith("\\") && !path.equals(""))// 不为空是为去根节点;path += "\\";path += temp[i];}return path;}public class ExpandListener implements TreeWillExpandListener {/** 树展开及收缩监听;*/private MainFrame mainFrame = null;public ExpandListener(MainFrame mainFrame) {this.mainFrame = mainFrame;}public void treeWillExpand(TreeExpansionEvent event) {// 对节点的路径进行转化String path = toFilePath(event.getPath().toString());TreePath treePath = event.getPath();DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();// System.out.println("所展开节点的路径:" + path);// System.out.println(treePath);if (node.getDepth() < 2) {Enumeration children = node.children();String filePath = "";while (children.hasMoreElements()) {DefaultMutableTreeNode temp = (DefaultMutableTreeNode) children.nextElement();filePath = "";filePath = path;if (!filePath.endsWith("\\"))filePath += "\\";filePath += temp.toString();// System.out.println("temp=" +filePath);mainFrame.addPrefetchChildren(filePath, temp);}}}2)、添加功能“添加文件(夹)addframe()”、“修改文件(夹)mvframe()”public void addframe() {JFrame addFrame = new JFrame();JLabel jlbl = new JLabel("请输入要添加的文件(夹)名:");addrs = new JLabel("");addrs.setBounds(180, 10, 100, 25);jlbl.setBounds(10, 10, 170, 25);addfile = new JTextField();addfile.setBounds(10, 40, 260, 25);btnaddf = new JButton("添加文件");btnaddd = new JButton("添加文件夹");btnaddf.setBounds(20, 80, 100, 25);btnaddd.setBounds(160, 80, 100, 25);btnaddf.addActionListener(this);btnaddd.addActionListener(this);addFrame.add(jlbl);addFrame.add(addrs);addFrame.add(addfile);addFrame.add(btnaddf);addFrame.add(btnaddd);addFrame.setBounds(400, 350, 300, 150);addFrame.setTitle("添加文件(夹)");addFrame.setLayout(null);addFrame.setVisible(true);}public void mvframe() {JFrame mvFrame = new JFrame();JLabel jlbl = new JLabel("请输入修改后的文件名:");mvrs = new JLabel("");mvrs.setBounds(160, 10, 140, 25);jlbl.setBounds(10, 10, 170, 25);mvfile = new JTextField();mvfile.setBounds(10, 40, 260, 25);btnmvf = new JButton("修改文件名");btnmvd = new JButton(" 修改文件夹名");btnmvf.setBounds(10, 80, 120, 25);btnmvd.setBounds(150, 80, 120, 25);btnmvf.addActionListener(this);btnmvd.addActionListener(this);mvFrame.add(jlbl);mvFrame.add(mvrs);mvFrame.add(mvfile);mvFrame.add(btnmvf);mvFrame.add(btnmvd);mvFrame.setBounds(400, 350, 300, 150);mvFrame.setTitle("修改文件(夹)名");mvFrame.setLayout(null);mvFrame.setVisible(true);}}3)显示文件* 显示系统中的所有盘符;public String[] ListDisks() {File roots[] = File.listRoots();// 根盘符;String disks[] = new String[roots.length];for (int i = 0; i < roots.length; i++) {disks[i] = roots[i].toString();}return disks;}* 获得路径path下的文件;public String[] listAll(String path) {try {File f = new File(path);String[] fileName;String tmp = null;mainFrame.fileshow.setText(null);mainFrame.filestyle.setText(null);if (f.isDirectory()) {fileName = f.list();// System.out.println("共有" + fileName.length + "个文件");for (int i = 0; i < fileName.length; i++) {mainFrame.fileshow.append(fileName[i] + '\n');tmp = path + '\\' + fileName[i];// System.out.println(tmp);if (listDirectory(tmp) != null) {mainFrame.filestyle.append("文件夹\n");} else {mainFrame.filestyle.append("文件\n");}}return fileName;} else if (f.isFile()) {System.out.println("这是一个文件");return null;} else {// System.out.println(path);return null;}} catch (Exception e) {return null;}}public String[] listDirectory(String path) {File f = new File(path);String[] fileName;if (f.isDirectory()) {fileName = f.list();return fileName;} else {// System.out.println(path + "是文件");return null;}}* 进行md操作;md <目录名> 功能: 创建新目录public void md(String directory) {if (!mainFrame.currentPath.equals("")) {String temp = mainFrame.currentPath + "\\" + directory;File newFile = new File(temp);if (!newFile.exists()) {try {if (newFile.isDirectory() == false) {newFile.mkdirs();System.out.println("文件夹创建成功!");} else {System.out.println("文件夹创建出错!");}} catch (Exception e) {System.out.println("出错信息:" + e.getMessage());}} else {System.out.println("文件夹已经存在");}}}* 进行rd操作;rd <目录名> 功能: 删除目录;public void del() {String temp = mainFrame.currentPath;File file = new File(temp);if (file.exists()) {if (file.delete()) {mainFrame.fileshow.setText("文件(夹)删除成功!");} else {mainFrame.fileshow.setText("文件(夹)删除操作出错!");}} else {mainFrame.fileshow.setText("文件(夹)不存在");}}/** 进行edit操作:edit <文件名> 功能: 新建文件*/public void edit(String file) {if (!mainFrame.currentPath.equals("")) {String temp = mainFrame.currentPath + "\\" + file;File newFile = new File(temp);if (newFile.exists()) {mainFrame.addrs.setText("文件已经存在!");System.out.println("文件已经存在!");} else {try {newFile.createNewFile();mainFrame.addrs.setText("文件创建成功!");System.out.println("文件创建成功!");} catch (Exception e) {System.out.println("文件创建失败:" + e.getMessage());}}}}public void mvf(String file){if (!mainFrame.PPath.equals("")) {String temp = mainFrame.PPath + "\\" + file;File newFile = new File(mainFrame.currentPath);if (newFile.exists()) {if(newFile.renameTo(new File(temp))==true){mainFrame.mvrs.setText("修改文件名成功!");}else{mainFrame.mvrs.setText("存在同名文件!");}}}}public void mvd(String dir){if (!mainFrame.PPath.equals("")) {String temp = mainFrame.PPath + "\\" + dir;File newFile = new File(mainFrame.currentPath);if (newFile.exists()) {if(newFile.renameTo(new File(temp))==true){mainFrame.mvrs.setText("修改文件夹名成功!");}else{mainFrame.mvrs.setText("存在同名文件夹!");}}}}}4、运行与测试运行时,弹出主界面双击文件盘C在E盘路径添加文件夹操作系统,再添加文件操作系统如果文件已存在会显示可以更改文件名称删除文件,不过有个BUG,要文件一层层删,才可以删除。

实验四: 文件管理

实验四: 文件管理

实验四:文件管理一、实验目的1、掌握linux文件保护的原理2、掌握linux 符号链接、软链接的不同二.实验环境一台已经安装好Linux操作系统的主机/或安装过vmware的windows系统。

三.实验内容内容一:Linux操作系统下帐号管理命令及文件介绍1、添加用户2、为用户添加密码3、锁定账号4、解锁账号5、设置账号的过期时间6、添加组账号7、指定用户的所属组8、修改用户的所属组9、删除组内容二:Linux操作系统下文件、目录权限管理1、为用户创建的所有新文件生成一个默认的文件权限 755。

2、改变/opt/local /book/及其子目录下的所有文件的属组为book3.把文件shiyan.c的所有者改为wan4.把目录/hi及其下的所有文件和子目录的属主改成wan,属组改成users。

5.新建一个文件,并将它的权限修改为 rwxrw-r--, 最少列举两种方法:内容三:硬链接、符号连接Linux系统中连接文件分为硬连接和符号连接。

硬连接:只是在某一目录下的块多写入一个关联数据,不会用掉incode 与磁盘空间(只有当目录的块被用完,才可能会增加一个块来完成,从而导致磁盘空间发生变化,这样的可能性非常小)。

符号连接:我们可以理解成如Windows的快捷方式。

符号连接是一个独立的新文件,所以占inode与块。

(注:在ext2文件系统中,文件由文件属性块(即indoe talbe)和文件内容块两区域)实验步骤:1、 /显示当前目录占用的容量,和磁盘空间大小命令:结果:2、使用vi建立一个测试文件test。

内容为“Hello everyone!”3、显示当前目录占用的容量,和磁盘空间大小结果:4、为test创建硬连接文件testh命令:5、查看建立硬连接文件后的目录容量和磁盘大小,注意:目录容量磁盘大小是否改变。

结果:6、为test创建一个符号连接文件testo文件命令:7、查看建立符号连接文件后的目录容量和磁盘大小,注意:目录容量磁盘大小是否改变。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验4:文件管理实验
一、目的:
1、加深对文件、目录、文件系统等概念的理解。

2、掌握Linux 文件系统的目录结构。

3、掌握有关Linux 文件系统操作的常用命令。

4、了解有关文件安全性方面的知识。

二、条件:
需要一个Linux的环境
1、在Win10系统启用Linux子系统
2、在Win10应用商店安装Ubuntu应用
3、
三、过程:
1、浏览文件系统
用到pwd、ls、mkdir、cd命令
不带参数的cd命令,工作目录回到了用户的默认目录
cd ../.. 执行后,工作目录回到了/根目录
2、查看文件
用到data、head、tail、ls、man、date、cp、mv、rm命令
man date
建立链接后第2个字段从1变成2
3、文件查找和目录
用到了find、grep命令
4、修改文件存取权限
chmod命令
四、总结:Linux是一个多用户的现代操作系统,提供了丰富和强大的文件管理的命令。

掌握这些命令对于我们深入Linux学习是必需的。

相关文档
最新文档