2024年5月10日发(作者:)

操作系统实验指导书

一、 实验说明

1、实验目的

实验是操作系统原理课程中不可缺少的重要教学环节,实验目的是使学生理论联系

实际,使学生在实践探索中去发现问题、去解决问题,提高了学生获取知识和应用技术

的能力,培养了学生分析和解决问题的能力。

《操作系统原理》要求理论与实践相结合,本门实验课程是对《操作系统原理》课堂教

学的一个重要补充,与理论学习起着相辅相成的作用,是实施《操作系统原理》教学的一个

重要组成部分。通过本实验课的实践学习,可以增强本专业的学生对系统实现的认识。对加

深理解和掌握操作系统相关原理有重要帮助。

2、 实验要求

进一步了解和掌握操作系统原理,提高系统设计的能力。对每一实验题目,应独立完成,

并要求:

·上机前,学生必须做好充分的实验准备工作,掌握与实验相关的背景知识,用任一种

高级语言编写程序。

·上机时,认真调试,并观察、记录程序运行过程中出现的现象和问题。

·上机后,分析实验结果并写出实验报告。

3、实验报告要求

每个实验(包括选做的)均应编写实验报告,学生实验后要写出严谨的、实事求是的、文

字通顺的、字迹公整的实验报告。实验报告应包括以下内容:

(1) 实验题目

(2) 实验目的

(3) 实验内容

 程序中使用的数据结构及符号说明

 流程图

 源程序清单并附上注释

(4) 实验结果及分析

 运行结果(必须是上面程序清单所对应输出的结果)

 对运行情况所作的分析以及本次调试程序所取得的经验。如果程序未能通过,

应分析其原因。

4、实验课时安排

序 号

实验名称

实验一

熟悉使用计算机系统

实验二

进程调度模拟

实验三

存储管理模拟

实验四

简单文件系统的实现

课 时

实验属性

2

4

6

6

验证

验证

验证

验证

必(选)做

必做

必做

必做

必做

1

二、实验内容

实验一 熟悉使用计算机系统

一、实验名称

熟悉使用计算机系统

二、实验目的与要求

通过对Windows操作系统的使用,熟悉Windows操作系统中的基本概念,如单用户、

多任务、进程和文件等,熟悉Windows中命令行方式下常用命令的使用方法;进一步熟悉

TC语言与开发环境,为以后的实验打好基础。

三、实验内容

1.开机后,熟悉Windows的界面(桌面、任务栏、开始按钮<点击后出现“开始”菜单>、

我的电脑图标、回收站、我的文档)。

2.双击“我的文档”后,出现一个新的窗口,在窗口上单击鼠标右键,选择新建->文

本文件,生成一个新的文本文件,双击这个文件,对这个文件进行编辑,完成后选择菜单

中的文件->保存,命名后确定,然后点击窗口右上角的“X”(关闭)按钮关闭窗口。

3.在“开始”按钮上单击鼠标右键,出现资源管理器窗口,点击左边的“我的文档”

文件夹,右边窗口出现刚才生成的文件。不要在该文件上,而是在该窗口其他空白的地方单

击鼠标右键,选择新建->文件夹,生成一个新的文件夹。然后左键点击文本文件,选择菜单

中的剪切,然后点击新建文件夹,选择菜单中的粘贴,是移动操作,若点击文本文件后, 选

择菜单中的复制,是复制操作。生成程序的一个新的副本。

4.熟悉Windows操作系统命令接口

单击“开始”按钮,弹出开始菜单,选择运行,在出现的组合框中输入命令cmd(Windows

2000)或command(Windows 9x),打开命令行窗口,熟悉以下命令:

winver 检查Windows版本

dxdiag 检查DirectX信息

显示内存使用情况

Sndvol32 音量控制程序

系统文件检查器

组策略

注册表

系统配置实用程序

磁盘检查

本地账户管理

drwtsn32 系统医生

cleanmgr 垃圾整理

ntbackup 系统备份和还原

taskmgr 任务管理器

设备管理器

NT的磁盘管理器

计算机管理

winchat 局域网聊天

nslookup 网络管理的工具,IP地址侦测器

wupdmgr WIDNOWS UPDATE

2

Clipbrd 剪贴板查看器

Odbcad32 ODBC数据源管理器

事件查看器

比较两个文件的不同

5.系统配置

在Windows2000中没有msconfig,

无法通过它来配置系统,只要从Windows XP的安装光盘

的〈I386〉文件夹下找到和_文件,将它们拷贝到临时文件夹中,如〈F:TEMP〉

下,并打开DOS窗口,输入expand 回车,然后就试着双击释放出来的

文件即可。而且这个msconfig程序在Windows 2000中运行起来没有警告窗口,同时

功能也有所增加。你也可以把它拷贝到Windows 2000的安装文件夹下,以后就可以在“开始”→“运

行”窗口中输入msconfig来直接使用了。用它来去除系统自启动程序特别有效。

6.有关注册表的操作:

单击“开始”按钮,弹出开始菜单,选择运行,在出现的组合框中输入命令regedit

打开注册表编辑器。

 添加主键

1. 单击菜单“编辑”,打开“查找”对话框,输入要查找的注册表项software,

不需要打开注册项;

2. 右键单击要添加主键的文件夹:software;

3. 指向“新建”,然后单击“项”,新主键将显示在该文件夹的下面,并以“新建

#”为临时文件夹;

4. 键入新主键的名字:app,然后按回车;

5. 完成主键添加。

 重命名注册表项或值

1. 右键单击要重命名的注册表项app,然后单击“重命名”,或通过菜单项中的“编

辑”---“重命名”;

2. 键入新名称shelf;

 删除注册表项或值

1. 查找要删除的注册表项:shelf,但不需打开该注册表项

2. 右键单击注册表项shelf,然后单击删除项

 导出注册表

1. 单击“我的电脑”导出全部注册表,或单击某文件夹只导出该文件夹的内容;

2. 在“注册表”菜单上单击“导出注册表文件”

3. 浏览要保存导出文件的位置,然后键入新文件的名称All;

 导入注册表

1. 在“注册表”菜单上,单击“引入注册表文件”;

2. 浏览引入的文件,然后单击打开;

7.为了尽快地熟悉计算机系统,可编辑一个源程序,且对编辑好的源程序编译、运行、

显示运行结果等。要求:编制一个程序,要求输入5个学生的学号、2门课的成绩,然后输

出每个学生的学号、2门课成绩、平均成绩。

3

实验二 模拟进程调度中的高优先级优先调度算法

一、验名称

进程调度模拟

二、实验目的

在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。当就绪进程个数

大于处理器数时,就必须依照某种策略来决定哪些进程优先占用处理器。本实验模拟在单处

理器情况下的处理器调度,帮助学生加深了解处理器调度的工作。

三、实验内容

设计一个按优先数调度算法实现处理器调度的程序。

[提示]:

(1) 假定系统有五个进程,每一个进程用一个进程控制块PCB来代表,进程控制块的

格式为:

进程名

指针

要求运行时间

优先数

状态

其中,进程名——作为进程的标识,假设五个进程的进程名分别为P

1

,P

2

,P

3

,P

4

,P

5

指针——按优先数的大小把五个进程连成队列,用指针指出下一个进程的进程控制块的

首地址,最后一个进程中的指针为“0”。

要求运行时间——假设进程需要运行的单位时间数。

优先数——赋予进程的优先数,调度时总是选取优先数大的进程先执行。

状态——可假设有两种状态,“就绪”状态和“结束”状态。五个进程的初始状态都为

“就绪”,用“R”表示,当一个进程运行结束后,它的状态为“结束”,用“E”表示。

(2) 在每次运行你所设计的处理器调度程序之前,为每个进程任意确定它的“优先数”

和“要求运行时间”。

(3) 为了调度方便,把五个进程按给定的优先数从大到小连成队列。用一单元指出队首

进程,用指针指出队列的连接情况。例:

队首标志

K

2

K

1

P

1

0

2

1

R

PCB1

K

2

P

2

K

4

3

5

R

PCB2

K

3

P

3

K

5

1

3

R

PCB3

K

4

P

4

K

3

2

4

R

PCB4

K

5

P

5

K

1

4

2

R

PCB5

(4) 处理器调度总是选队首进程运行。采用动态改变优先数的办法,进程每运行一次优

先数就减“1”。由于本实验是模拟处理器调度,所以,对被选中的进程并不实际的启动运行,

而是执行:

优先数-1

要求运行时间-1

来模拟进程的一次运行。

提醒注意的是:在实际的系统中,当一个进程被选中运行时,必须恢复进程的现场,让

4

它占有处理器运行,直到出现等待事件或运行结束。在这里省去了这些工作。

(5) 进程运行一次后,若要求运行时间0,则再将它加入队列(按优先数大小插入,且

置队首标志);若要求运行时间=0,则把它的状态修改成“结束”(E),且退出队列。

(6) 若“就绪”状态的进程队列不为空,则重复上面(4)和(5)的步骤,直到所有进

程都成为“结束”状态。

(7) 在所设计的程序中应有显示或打印语句,能显示或打印每次被选中进程的进程名以

及运行一次后进程队列的变化。

(8) 为五个进程任意确定一组“优先数”和“要求运行时间”,启动所设计的处理器调

度程序,显示或打印逐次被选中进程的进程名以及进程控制块的动态变化过程。

5

实验三

Windows系统内存管理

一、实验名称

:存储

管理模拟

二、实验目的

在TC、VB、Delphi、C++Builder等语言与开发环境中,模拟操作系统的内存管理;通

过程序运行所显示的内存使用的各项指标,加深对内存管理的理解。

三、实验内容

实现主存储器空间的分配和回收。本实验有两个题,学生可选择其中的一题做实验。

第一题:在固定分区管理方式下实现主存分配和回收。

第二题:在可变分区管理方式下采用最先适应算法实现主存分配和实现主存回收。

[提示]:

可变分区方式是按作业需要的主存空间大小来分割分区的。当要装入一个作业时,根据

作业需要的主存量查看是否有足够的空闲空间,若有,则按需要量分割一个分区分配给该作

业;若无,则作业不能装入。随着作业的装入、撤离,主存空间被分成许多个分区,有的分

区被作业占用,而有的分区是空闲的。例如:

0

5k

10k

14k

26k

32k

128k

操作系统

作业1

作业3

空闲区

作业2

空闲区

为了说明哪些区是空闲的,可以用来装入新作业,必须要有一张空闲区说明表,格式如下:

6

第一栏

第二栏

起 址

14 K

32 K

其中,起址——指出一个空闲区的主存起始地址。

长度——指出从起始地址开始的一个连续空闲的长度。

状态——有两种状态,一种是“未分配”状态,指出对应的由起址指出的某个长度

的区域是空闲区;另一种是“空表目”状态,表示表中对应的登记项目是空白(无效),可

用来登记新的空闲区(例如,作业撤离后,它所占的区域就成了空闲区,应找一个“空表目”

栏登记归还区的起址和长度且修改状态)。由于分区的个数不定,所以空闲区说明表中应有

适量的状态为“空表目”的登记栏目,否则造成表格“溢出”无法登记。

空闲区表的定义为:

#define m 10 /*假定系统允许的空闲区表最大为m*/

struct

{ float address; /*起始地址*/

float length; /*长度*/

int flag; /*标志,用“0”表示空栏目,用“1”表示未分配*/

}free_table[m]; /*空闲区表*/

上述的这张说明表的登记情况是按提示(1)中的例所装入的三个作业占用的主存区域

后填写的。

长 度

12 K

96 K

状 态

未 分 配

未 分 配

空 表 目

空 表 目

7

(2) 当有一个新作业要求装入主存时,必须查空闲区说明表,从中找出一个足够大的空

闲区。有时找到的空闲区可能大于作业需要量,这时应把原来的空闲区变成两部分:一部分

分给作业占用;另一部分又成为一个较小的空闲区。为了尽量减少由于分割造成的空闲区,

而尽量保存高地址部分有较大的连续空闲区域,以利于大型作业的装入。为此,在空闲区说

明表中,把每个空闲区按其地址顺序登记,即每个后继的空闲区其起始地址总是比前者大。

为了方便查找还可使表格“紧缩”,总是让“空表目”栏集中在表格的后部。

(3) 采用最先适应算法(顺序分配算法)分配主存空间。

按照作业的需要量,查空闲区说明表,顺序查看登记栏,找到第一个能满足要求的空闲

区。当空闲区大于需要量时,一部分用来装入作业,另一部分仍为空闲区登记在空闲区说明

表中。

由于本实验是模拟主存的分配,所以把主存区分配给作业后并不实际启动装入程序装入

作业,而用输出“分配情况”来代替。最先适应分配算法如图3-1。

图3-1 最先适应分配模拟算法

(4) 当一个作业执行结束撤离时,作业所占的区域应该归还,归还的区域如果与其它空

闲区相邻,则应合成一个较大的空闲区,登记在空闲区说明表中。例如,在提示(1)中列

举的情况下,如果作业2撤离,归还所占主存区域时,应与上、下相邻的空闲区一起合成一

个大的空闲区登记在空闲区说明表中。归还主存时的回收算法如图3-2。

8

图3—2 主存回收算法

(5) 请按最先适应算法设计主存分配和回收的程序。然后按(1)中假设主存中已装入

三个作业,且形成两个空闲区,确定空闲区说明表的初值。现有一个需要主存量为6K的作

业4申请装入主存;然后作业3撤离;再作业2撤离。请你为它们进行主存分配和回收,把

空闲区说明表的初值以及每次分配或回收后的变化显示出来或打印出来。

实验四

文件操作

一、实验名称

:简单文件系统的实现

二、实验目的要求

在TC、VB、Delphi、C++Builder等语言与开发环境中,实现对文件的各项操作(复制、

打开、保存、录入、查找、显示属性等);使学生进一步了解文件的主要操作。

9

三、实验内容

为DOS系统设计一个简单的二级文件系统。要求做到以下几点:

①可以实现下列几条命令

LOGIN 用户登陆

DIR 列文件目录

CREATE 创建文件

DELETE 删除文件

OPEN 打开文件

CLOSE 关闭文件

READ 读文件

WRITE 写文件

MODIFY 修改文件内容

②列目录时要列出文件名,物理地址,保护码和文件长度。

③源文件可以进行读写保护。

四、实验提示

①首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录

和子目录都以文件的形式存放于磁盘,这样便于查找和修改。

②用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2„并

以编号作为物理地址,在目录中进行登记。

五、实验运行结果

参考程序见下:

#include "stdio.h"

#include "string.h"

#include "conio.h"

#include "stdlib.h"

#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/

#define MAXCHILD 50 /*the largest child*/

#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/

typedef struct /*the structure of OSFILE*/

{int fpaddr; /*file physical address*/

int flength; /*file length*/

int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write(default);*/

char fname[MAXNAME]; /*file name*/

} OSFILE;

10

typedef struct /*the structure of OSUFD*/

{char ufdname[MAXNAME]; /*ufd name*/

OSFILE ufdfile[MAXCHILD]; /*ufd own file*/

}OSUFD;

typedef struct /*the structure of OSUFD'LOGIN*/

{char ufdname[MAXNAME]; /*ufd name*/

char ufdpword[8]; /*ufd password*/

} OSUFD_LOGIN;

typedef struct /*file open mode*/

{int ifopen; /*ifopen:0-close,1-open*/

int openmode; /*0-read only,1-write only,2-read and write,3-initial*/

}OSUFD_OPENMODE;

OSUFD *ufd[MAXCHILD]; /*ufd and ufd own files*/

OSUFD_LOGIN ufd_lp;

int ucount=0; /*the count of mfd's ufds*/

int fcount[MAXCHILD]; /*the count of ufd's files*/

int loginsuc=0; /*whether login successfully*/

char username[MAXNAME]; /*record login user's name22*/

char dirname[MAXNAME];/*record current directory*/

int fpaddrno[MAX]; /*record file physical address num*/

OSUFD_OPENMODE ifopen[MAXCHILD][MAXCHILD]; /*record file open/close*/

int wgetchar; /*whether getchar()*/

FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file;

void main()

{int i,j,choice1;

char choice[50]; /*choice

operation:dir,create,delete,open,delete,modify,read,write*/

int choiceend=1; /*whether choice end*/

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

void LoginF(); /*LOGIN FileSystem*/

void DirF(); /*Dir FileSystem*/

void CdF(); /*Change Dir*/

void CreateF(); /*Create File*/

void DeleteF(); /*Delete File*/

void ModifyFM(); /*Modify FileMode*/

void OpenF(); /*Open File*/

11

void CloseF(); /*Close File*/

void ReadF(); /*Read File*/

void WriteF(); /*Write File*/

void QuitF(); /*Quit FileSystem*/

void help();

if((fp_mfd=fopen("c:osfilemfd","rb"))==NULL)

{fp_mfd=fopen("c:osfilemfd","wb");

for(i=0;i

textattr(BLACK*16|WHITE);

clrscr(); /*clear screen*/

LoginF(); /*user login*/

clrscr();

if(loginsuc==1) /*Login Successfully*/

{while (1)

{wgetchar=0;

if (choiceend==1)

{printf("nnC:%s>",strupr(dirname));}

else printf("Bad command or file :%s>",strupr(username));

gets(choice);

strcpy(choice,ltrim(rtrim(strlwr(choice))));

if (strcmp(choice,"dir")==0) choice1=1;

else if(strcmp(choice,"creat")==0) choice1=2;

else if(strcmp(choice,"delete")==0) choice1=3;

else if(strcmp(choice,"attrib")==0) choice1=4;

else if(strcmp(choice,"open")==0) choice1=5;

else if(strcmp(choice,"close")==0) choice1=6;

else if(strcmp(choice,"read")==0) choice1=7;

else if(strcmp(choice,"modify")==0) choice1=8;

else if(strcmp(choice,"exit")==0) choice1=9;

else if(strcmp(choice,"cls")==0) choice1=10;

else if(strcmp(choice,"cd")==0) choice1=11;

else if(strcmp(choice,"help")==0) choice1=20;

else choice1=12;

switch(choice1)

{case 1:DirF();choiceend=1;break;

case 2:CreateF();choiceend=1;if(!wgetchar) getchar();break;

case 3:DeleteF();choiceend=1;if(!wgetchar)getchar();break;

case 4:ModifyFM();choiceend=1;if(!wgetchar) getchar();break;

case 5:choiceend=1;OpenF();if (!wgetchar) getchar();break;

case 6:choiceend=1;CloseF();if (!wgetchar) getchar();break;

fclose(fp_mfd);

}

12

case 7:choiceend=1;ReadF();if (!wgetchar) getchar();break;

case 8:choiceend=1;WriteF();if (!wgetchar) getchar();break;

case 9:printf("nYou have exited this system.");

QuitF();exit(0);break;

case 10:choiceend=1;clrscr();break;

case 11:CdF();choiceend=1;break;

case 20:help();choiceend=1;break;

default:choiceend=0;

}

}

}

else printf("nAccess denied.");

}

void help(void)

{

printf("nThe Command Listn");

printf("nCd Attrib Creat Modify Read Open Cls Delete Exit Closen");

}

char *rtrim(char *str) /*remove the trailing blanks.*/

{int n=strlen(str)-1;

while(n>=0)

{if(*(str+n)!=' ')

{*(str+n+1)='0';

break;

}

else n--;

}

if (n<0) str[0]='0';

return str;

}

char *ltrim(char *str) /*remove the heading blanks.*/

{char *rtrim(char *str);

strrev(str);

rtrim(str);

strrev(str);

return str;

}

void LoginF() /*LOGIN FileSystem*/

{char loginame[MAXNAME],loginpw[9],logincpw[9],str[50];

int i,j,flag=1;

char a[25];

13

int findout; /*login user not exist*/

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

void InputPW(char *password); /*input password,use '*' replace*/

void SetPANo(int RorW); /*Set physical address num*/

while(1)

{findout=0;

printf("nnLogin Name:");

gets(loginame);

ltrim(rtrim(loginame));

fp_mfd=fopen("c:osfile","rb");

for(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i++)

if (strcmp(strupr(ufd_e),strupr(loginame))==0)

{findout=1;

strcpy(logincpw,ufd_rd);

}

fclose(fp_mfd);

if (findout==1) /*user exist*/

{printf("Login Password:");

InputPW(loginpw); /*input password,use '*' replace*/

if (strcmp(loginpw,logincpw)==0)

{strcpy(username,strupr(loginame));

strcpy(dirname,username);

fp_mfd=fopen("c:osfile","rb");

for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++)

{strcpy(str,"c:osfile");

strcat(str,ufd_e);

ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));

strcpy(ufd[j]->ufdname,strupr(ufd_e));

fp_ufd=fopen(str,"rb");

fcount[j]=0;

{ifopen[j][i].ifopen=0;

ifopen[j][i].openmode=4;}

fclose(fp_ufd);}

for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++)

fclose(fp_mfd);

ucount=j;

SetPANo(0);

printf("nnLogin successful! Welcome to this FileSystemnn");

loginsuc=1;

return;}

else

{printf("nn");

14

flag=1;

while(flag)

{printf("Login Failed! Password Error. Try Again(Y/N):");

gets(a);

ltrim(rtrim(a));

if (strcmp(strupr(a),"Y")==0) {loginsuc=0;flag=0;}

else if(strcmp(strupr(a),"N")==0){loginsuc=0;flag=0;return;}

}

}

}

{printf("New Password(<=8):");

InputPW(loginpw); /*input new password,use '*' replace*/

printf("nConfirm Password(<=8):"); /*input new password,use '*' replace*/

InputPW(logincpw);

if (strcmp(loginpw,logincpw)==0)

{strcpy(ufd_e,strupr(loginame));

strcpy(ufd_rd,loginpw);

fp_mfd=fopen("c:osfile","ab");

fwrite(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd);

fclose(fp_mfd);

strcpy(username,strupr(loginame));

strcpy(dirname,loginame);

strcpy(str,"c:osfile");

strcat(str,username);

if((fp_ufd=fopen(str,"rb"))==NULL)

{fp_ufd=fopen(str,"wb");

fclose(fp_ufd);

}

else

fp_mfd=fopen("c:osfile","rb");

for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++)

{strcpy(str,"c:osfile");

strcat(str,ufd_e);

ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));

strcpy(ufd[j]->ufdname,strupr(ufd_e));

fp_ufd=fopen(str,"rb");

{ifopen[j][i].ifopen=0;

ifopen[j][i].openmode=4;}

fclose(fp_ufd);}

for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++)

fclose(fp_mfd);

ucount=j;

SetPANo(0);

15

}

}

}

printf("nnLogin Successful! Welcome to this Systemnn");

loginsuc=1;

return;

}

else

{printf("nn");

flag=1;

while(flag)

{printf("Login Failed! Password Error. Try Again(Y/N):");

gets(a);

ltrim(rtrim(a));

if (strcmp(strupr(a),"Y")==0) {loginsuc=0;flag=0;}

else if(strcmp(strupr(a),"N")==0){loginsuc=0;flag=0;return;}

}

}

void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/

{int i,j;

if (RorW==0)

}

void InputPW(char *password) /*input password,use '*' replace*/

{if((fp_file_p=fopen("c:osfilefilefile_p","rb"))==NULL)

{fp_file_p=fopen("c:osfilefilefile_p","wb");

fclose(fp_file_p);

}

fp_file_p=fopen("c:osfilefilefile_p","rb");

for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i++)

fpaddrno[j]=1;

/*for(i=1;i

if ((i%13)==0) fpaddrno[i]=1;*/

}

{fp_file_p=fopen("c:osfilefilefile_p","wb");

/*for(i=1;i

if((i%13)==0) fpaddrno[i]=0;*/

for(i=0;i

if (fpaddrno[i]==1)

}

fwrite(&i,sizeof(int),1,fp_file_p);

else

fclose(fp_file_p);

16

{int j;

for(j=0;j<=7;j++)

}

void DirF() /*Dir FileSystem*/

{int i,j,count=0;

char sfmode[25],sfpaddr[25],str[25];

int ExistD(char *dirname); /*Whether DirName Exist,Exist-i,Not Exist-0*/

clrscr();

if (strcmp(strupr(ltrim(rtrim(dirname))),"")!=0)

{printf("nnC:%s>dirn",dirname);

printf("n%14s%16s%14s%10s%18sn","FileName","FileAddress","FileLength","Type","Fil

eMode");

j=ExistD(dirname);

for(i=0;i

{if ((i%16==0)&&(i!=0))

{printf("nPress any key to continue..");

getch();

clrscr();

{password[j]=getch();

if ((int)(password[j])!=13)

{if((int)(password[j])!=8)

putchar('*');

else

{if (j>0)

{j--;j--;

putchar('b');putchar(' ');putchar('b');

}

else j--;

}

}

else

{password[j]='0';

break;

}

}

password[j]='0';

printf("n%14s%16s%14s%10s%18sn","FileName","FileAddress","FileLength","Type","Fil

eMode");

}

itoa(ufd[j]->ufdfile[i].fpaddr,str,10);

strcpy(sfpaddr,"file");

17

strcat(sfpaddr,str);

if (ufd[j]->ufdfile[i].fmode==0) strcpy(sfmode,"Read Only");

else if(ufd[j]->ufdfile[i].fmode==1) strcpy(sfmode,"Write Only");

else if(ufd[j]->ufdfile[i].fmode==2)strcpy(sfmode,"Read And Write");

else strcpy(sfmode,"Protect");

printf("%14s%16s%14d%10s%18sn",ufd[j]->ufdfile[i].fname,sfpaddr,ufd[j]->ufdfi

le[i].flength,"",sfmode);

}

printf("n %3d file(s)n",fcount[j]);}

else

{printf("nnC:>dirn");

printf("n%14s%18s%8sn","DirName","OwnFileCount","Type");

for(i=0;i

{if ((i%16==0)&&(i!=0))

{printf("nPress any key ");

getch();

clrscr();

printf("n%14s%18s%8sn","DirName","OwnFileCount","Type");

}

printf("%14s%18d%8sn",ufd[i]->ufdname,fcount[i],"");

count=count+fcount[i];

}

printf("n %3d dir(s),%5d file(s)n",ucount,count);

}

}

int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/

{int i;

int exist=0;

for(i=0;i

if (strcmp(strupr(ufd[i]->ufdname),strupr(dirname))==0)

{exist=1;

break;

}

if (exist) return(i);

else return(-1);

}

void CdF() /*Exchange Dir*/

{char dname[MAXNAME];

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int ExistD(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

printf("nPlease input DirName (cd..-Previous dir; DirNAME-cd [DirNAME]):");

18

gets(dname);

ltrim(rtrim(dname));

if (ExistD(dname)>=0) strcpy(dirname,strupr(dname));

else if(strcmp(strupr(dname),"CD..")==0) strcpy(ltrim(rtrim(dirname)),"");

else printf("nError.'%s' does not exist.n",dname);

}

void CreateF() /*Create File*/

{int fpaddrno,flag=1,i;

char fname[MAXNAME],str[50],str1[50],strtext[255],a[25];

char fmode[25];

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

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)||(st

{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;

rcmp(fmode,"3")==0))

19

}

strcpy(str,"c:osfilefilefile");

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-3n");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

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

20

if (fpaddrno[i]==0) {fpaddrno[i]=1;break;}

if (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

else

{

while(flag)

{printf("'%s' will be deleted. Are you sure(Y/N):",fname);

gets(a);

delete.n",fname);wgetchar=1;}

21

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

{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:osfilefilefile");

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 ModifyFM() /*Modify FileMode*/

{char fname[MAXNAME],str[50];

int i,j,k,flag;

char fmode[25]; /*whether delete*/

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

void InputPW(char *password); /*input password,use '*' replace*/

void SetPANo(int RorW); /*Set physical address num*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

int ExistD(char *dirname);

if (strcmp(strupr(dirname),strupr(username))!=0) {printf(" can only

modify filemode in yourself dir.n");wgetchar=1;}

else

{ printf("nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

22

i=ExistF(fname);

if (i>=0)

printf("'%s' filemode is %s.n",fname,strupr(str));

printf("Modify to(0-read only,1-write only,2-read and write,3-Protect):");

gets(fmode);

ltrim(rtrim(fmode));

if(strcmp(fmode,"0")==0)

{ufd[k]->ufdfile[i].fmode=0;

printf("n'%s' has been modified to READ ONLY mode successfully.n",fname);

wgetchar=1;

}

else if(strcmp(fmode,"1")==0)

{ufd[k]->ufdfile[i].fmode=1;

printf("n'%s' has been modified to WRITE ONLY mode successfully.n",fname);

wgetchar=1;

}

else if(strcmp(fmode,"2")==0)

{ufd[k]->ufdfile[i].fmode=2;

printf("n'%s' has been modified to READ AND WRITE mode successfully.n",fname);

wgetchar=1;

{k=ExistD(username);

if(ifopen[k][i].ifopen==1)

{printf("nError.'%s' is in open status. Close it before

else

{

if(ufd[k]->ufdfile[i].fmode==0) strcpy(str,"read only"); /*FileMode*/

else if(ufd[k]->ufdfile[i].fmode==1) strcpy(str,"write only");

else if(ufd[k]->ufdfile[i].fmode==2) strcpy(str,"read and write");

else strcpy(str,"Protect");

modify.n",fname);wgetchar=1;}

}

else if(strcmp(fmode,"3")==0)

{ufd[k]->ufdfile[i].fmode=3;

printf("n'%s' has been modified to FORBID mode successfully.n",fname);

wgetchar=1;

}

else {printf("nError.'%s' is not modified.n",fname);wgetchar=1;}

}

}

else

{printf("nError. '%s' dose not exist.n",fname);wgetchar=1;}}

}

23

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))

}

{strcpy(str,"write only");

if((ufd[k]->ufdfile[i].fmode==1)||(ufd[k]->ufdfile[i].fmode==2))

}

{strcpy(str,"read and write");

if(ufd[k]->ufdfile[i].fmode==2) ifopen[k][i].ifopen=1;

}

ifopen[k][i].ifopen=1;

else if(fmode[0]=='1') /*open file with write only mode*/

ifopen[k][i].ifopen=1;

else if(fmode[0]=='2') /*open file with read and write mode*/

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);

24

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,FileMode

wgetchar=1;

}

{printf("nError. '%s' hasn't been opened. OpenMode Error. OpenMode is %s,but

is %sn",fname,strupr(str),strupr(str1));

else

FileMode is %sn",fname,strupr(str),strupr(str1));wgetchar=1;}

}

else {printf("nError. FileOpenMode's Range is 0-2n");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

{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;

25

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(" convert to

ufd dir before read.n");wgetchar=1;return;}

printf("nCaution:Open file firstn");

printf("Opened File(s) List:n");

k=ExistD(dirname);

for(i=0;i

{if (ifopen[k][i].ifopen==1)

if ((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2))

if((n%4==0)&&(n!=0)) printf("n");

{printf("%15s",ufd[k]->ufdfile[i].fname);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:osfilefile");

strcat(str,str1);

26

fp_file=fopen(str,"rb");

fseek(fp_file,0,0);

printf("nThe text is:nn");

printf(" ");

while(fscanf(fp_file,"%c",&c)!=EOF)

if (c=='n') printf("n ");

else printf("%c",c);

printf("nn%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't

read.n",fname);wgetchar=1;}

}

else {printf("nError.'%s' is in closing status. Please open it before

readn",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

{if (ifopen[k][i].ifopen==1)

if ((ifopen[k][i].openmode==1) ||(ifopen[k][i].openmode==2))

if((n%4==0)&&(n!=0)) printf("n");

{printf("%15s",ufd[k]->ufdfile[i].fname);n++;}

}

printf("n%d files open.n",n);

if (n==0) wgetchar=1;

27

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:osfilefile");

strcat(str,str1);

if (ufd[k]->ufdfile[i].flength!=0)

{printf("n'%s' has text. Overwrite or Append(O-overwrite,A-Append,else-not

gets(a);

ltrim(rtrim(a));

if (fp_file!=NULL) fclose(fp_file);

if (strcmp(strupr(a),"O")==0)

{printf("nOverwriten");

fp_file=fopen(str,"wb");

ufd[k]->ufdfile[i].flength=0;

ufd[k]->ufdfile[i].flength=WriteF1();

write):",fname);

}

else if(strcmp(strupr(a),"A")==0)

{printf("nAppendn");

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);

}

{fp_file=fopen(str,"wb");

ufd[k]->ufdfile[i].flength=WriteF1();

}

fclose(fp_file);

wgetchar=1;

}

else

}

else

{printf("nError. '%s' has been opened with read only isn't

writed.n",fname);wgetchar=1;}

28

}

else

{printf("nError. '%s' is in closing status. Please open it before

writen",fname);wgetchar=1;}

}

else

{printf("nError. '%s' does not exist.n",fname);wgetchar=1;}

}

}

void QuitF() /*Quit FileSystem*/

{int i,j;

char str[50];

void SetPANo(int RorW); /*Set physical address num,0-read,1-write*/

SetPANo(1);

if (fp_mfd!=NULL) fclose(fp_mfd);

if (fp_ufd!=NULL) fclose(fp_ufd);

if (fp_file!=NULL) fclose(fp_file);

for(j=0;j

{strcpy(str,"c:osfile");

strcat(str,ufd[j]->ufdname);

ltrim(rtrim(str));

fp_ufd=fopen(str,"wb");

fclose(fp_ufd);

fp_ufd=fopen(str,"ab");

for(i=0;i

fwrite(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd);

fclose(fp_ufd);}

}

29