【OS课程设计二】模拟DOS系统文件的物理结构和管理使用
来源:互联网 发布:mysql递归函数 编辑:程序博客网 时间:2024/06/05 14:40
一、设计目的
1、模拟DOS系统文件的建立和使用情况,理解磁盘文件的物理结构
2、深入理解文件的物理结构与存取方法之间的关系
二、设计要求
1、模拟设计DOS系统中磁盘文件的存储结构
假定磁盘存储空间共有l00个物理块,设计一个FAT表。FAT表可用一个一维数组定义,其中每一个数组元素与一个物理块对应。当第i个元素为0时,表示第i盘块空闲;当第i个元素为-1时,其值表示一个文件结束标志;当第i个元素为其他数值时,其值表示该文件的下一物理块号。初始化时FAT标的所有元素均为0。另外,再设一个空闲块总数变量,记录系统还剩的空闲块数。它的值和FAT表中值为0的元素个数应该一致。
设计磁盘文件目录,每个文件保存:文件名、起始盘块号、长度(占盘块数)三个信息。
2、模拟设计文件存储时磁盘空间的分配过程
(1) 假定一个物理块的容量是1024B,要求设计一个程序,把文件的流式逻辑结构转换成链接物理结构:当用户要求将文件保存在磁盘上时,给出文件名及文件的长度,系统应能在磁盘上正确地为其分配存储空间,保存文件,改写FAT表和磁盘文件目录。这个程序形成一个键盘命令: write(文件名,文件长度)
(2) 要求设计另一个程序,当用户改写了文件后给出文件名及文件的长度,要求重新保存文件时,系统应能将文件原占有的盘块按其原有的逻辑顺序分配给该文件,保存文件。如果改写后的文件短于原文件,则回收它原来占有的倒数若干个盘块;如果改写后的文件长于原文件,则要为它增加分配若干个盘块。重新保存文件后要改写FAT表和磁盘文件目录。这个程序形成一个键盘命令: rwrite(文件名,文件长度)
3、模拟设计文件存储时磁盘空间的回收过程
要求设计第三个程序,当用户要求删除指定文件时,系统应能改写FAT表,回收该用户文件占用的磁盘空间,并删除该文件的目录信息。这个程序形成一个键盘命令: dele(文件名)
设计上述三条指令,在文件保存、删除后显示FAT表和磁盘文件目录。
在磁盘空间分配、回收的过程中,注意检查盘块使用情况的数据一致性。
Dir_Entry.h
- #ifndef _DIR_ENTRY_H_
- #define _DIR_ENTRY_H_
- #include <string>
- using std::string;
- class Dir_Entry
- {
- public:
- Dir_Entry(string fn,unsigned int fs,unsigned int sn):file_name(fn),first_sector(fs),sector_num(sn){}
- string get_File_Name() const
- {
- return file_name;
- }
- unsigned int get_First_Sector() const
- {
- return first_sector;
- }
- unsigned int get_Sector_Num() const
- {
- return sector_num;
- }
- void set_Sector_Num(unsigned int sn)
- {
- sector_num = sn;
- }
- private:
- string file_name;
- unsigned int first_sector;
- unsigned int sector_num;
- };
- #endif
Disk_Routine.h
- #ifndef _DISK_ROUTINE_H_
- #define _DISK_ROUTINE_H_
- #include <vector>
- #include <list>
- #include <string>
- #include "Dir_Entry.h"
- using std::string;
- using std::vector;
- using std::list;
- class Disk_Routine
- {
- public:
- Disk_Routine(unsigned int FAT_size):FAT(FAT_size){}
- void write(string file_name,unsigned int file_size);
- void rwirte(string file_name,unsigned int file_size);
- void del(string file_name);
- void disp_FAT();
- void disp_Dir_Table();
- private:
- unsigned int get_Avai_Sector_Num();
- vector<int> FAT;
- list<Dir_Entry> dir_entry_table;
- static const unsigned int SECTOR_SIZE;
- static const unsigned int FILE_NAME_MAX_SIZE;
- };
- #endif
Disk_Routine.cpp
- #include "Disk_Routine.h"
- #include <iostream>
- #include <iomanip.h>
- using std::vector;
- using std::list;
- using std::string;
- using std::cout;
- using std::cerr;
- using std::endl;
- const unsigned int Disk_Routine::SECTOR_SIZE = 1024;
- const unsigned int Disk_Routine::FILE_NAME_MAX_SIZE = 8;
- void Disk_Routine::write(string file_name,unsigned int file_size)
- {
- if(file_name.size() > FILE_NAME_MAX_SIZE)
- {
- cerr<<"the file name is too long,failed"<<endl;
- return;
- }
- unsigned int request_sector_num;
- if( file_size % SECTOR_SIZE == 0)
- {
- request_sector_num = file_size / SECTOR_SIZE;
- }
- else
- {
- request_sector_num = file_size / SECTOR_SIZE + 1;
- }
- if(request_sector_num > get_Avai_Sector_Num())
- {
- cerr<<"the file to long to storage in the disk,failed"<<endl;
- return;
- }
- list<Dir_Entry>::iterator det_it = dir_entry_table.begin();
- for(;det_it != dir_entry_table.end();det_it++)
- {
- if(det_it->get_File_Name() == file_name)
- {
- cerr<<"the file is already existed,failed"<<endl;
- return;
- }
- }
- vector<int>::iterator fat_it = FAT.begin();
- vector<int>::iterator loc_it;
- unsigned int index = 0;
- for(;fat_it != FAT.end();fat_it++,index++)
- {
- if(*fat_it == 0)
- {
- loc_it = fat_it;
- break;
- }
- }
- dir_entry_table.push_back(Dir_Entry(file_name,index,request_sector_num));
- fat_it = loc_it + 1;
- index++;
- for(;(request_sector_num != 0) && (fat_it != FAT.end());fat_it++,index++)
- {
- if(*fat_it == 0)
- {
- request_sector_num--;
- *loc_it = index;
- if(request_sector_num != 0)
- {
- loc_it = fat_it;
- }
- }
- }
- *loc_it = -1;
- cout<<"CREATE SUCCESS"<<endl;
- }
- void Disk_Routine::rwirte(string file_name,unsigned int file_size)
- {
- if(file_name.size() > FILE_NAME_MAX_SIZE)
- {
- cerr<<"the file name is too long,failed"<<endl;
- return;
- }
- list<Dir_Entry>::iterator det_it = dir_entry_table.begin();
- for(;det_it != dir_entry_table.end();det_it++)
- {
- if(det_it->get_File_Name() == file_name)
- {
- break;
- }
- }
- if(det_it == dir_entry_table.end())
- {
- cerr<<"the file doesn't exist"<<endl;
- return;
- }
- unsigned int request_sector_num;
- if(file_size % SECTOR_SIZE == 0)
- {
- request_sector_num = file_size / SECTOR_SIZE;
- }
- else
- {
- request_sector_num = file_size / SECTOR_SIZE + 1;
- }
- vector<int>::iterator fat_it;
- unsigned int ori_sector_num = det_it->get_Sector_Num();
- unsigned int i,diff;
- int tmp,cur_index,fat_index;
- if(request_sector_num > get_Avai_Sector_Num() - det_it->get_Sector_Num())
- {
- cerr<<"the file to long to storage in the disk,failed"<<endl;
- return;
- }
- else if(request_sector_num < ori_sector_num)
- {
- det_it->set_Sector_Num(request_sector_num);
- cur_index = det_it->get_First_Sector();
- for(i = 1;i <= ori_sector_num;i++)
- {
- if(i < request_sector_num)
- {
- cur_index = FAT.at(cur_index);
- }
- else if(i == request_sector_num)
- {
- tmp = FAT.at(cur_index);
- FAT.at(cur_index) = -1;
- cur_index = tmp;
- }
- else
- {
- tmp = FAT.at(cur_index);
- FAT.at(cur_index) = 0;
- cur_index = tmp;
- }
- }
- }
- else if(request_sector_num > ori_sector_num)
- {
- diff = request_sector_num - ori_sector_num;
- det_it->set_Sector_Num(request_sector_num);
- cur_index = det_it->get_First_Sector();
- for(i = 1;i <= ori_sector_num;i++)
- {
- if(i == ori_sector_num)
- {
- for(fat_it = FAT.begin(),fat_index = 0;fat_it != FAT.end();fat_it++,fat_index++)
- {
- if(*fat_it == 0)
- {
- if(!(diff--))
- {
- break;
- }
- FAT.at(cur_index) = fat_index;
- cur_index = fat_index;
- }
- }
- FAT.at(cur_index) = -1;
- }
- else
- {
- cur_index = FAT.at(cur_index);
- }
- }
- }
- else
- {
- cout<<"THE FILE SIZE IS THE SAME AS BEFORE";
- return;
- }
- cout<<"RECREATE SUCCESS"<<endl;
- }
- void Disk_Routine::del(string file_name)
- {
- list<Dir_Entry>::iterator det_it = dir_entry_table.begin();
- for(;det_it != dir_entry_table.end();det_it++)
- {
- if(det_it->get_File_Name() == file_name)
- {
- break;
- }
- }
- if(det_it == dir_entry_table.end())
- {
- cerr<<"the file doesn't exist"<<endl;
- return;
- }
- unsigned int cur_sector;
- unsigned int next_sector;
- unsigned int sector_num = det_it->get_Sector_Num();
- for(cur_sector = det_it->get_First_Sector();sector_num != 0;sector_num--)
- {
- next_sector = FAT.at(cur_sector);
- FAT.at(cur_sector) = 0;
- cur_sector = next_sector;
- }
- dir_entry_table.erase(det_it);
- cout<<"DELETE SUCCESS"<<endl;
- }
- void Disk_Routine::disp_FAT()
- {
- cout<<"------------------FAT-------------------"<<endl;
- vector<int>::iterator fat_it = FAT.begin();
- unsigned int index = 0;
- for(;fat_it != FAT.end();fat_it++,index++)
- {
- cout<<"sector_no:"<<right<<setw(3)<<index;
- cout<<" "<<"next_sector:"<<setw(3)<<*fat_it<<endl;
- }
- cout<<"---------------End of FAT---------------"<<endl;
- }
- void Disk_Routine::disp_Dir_Table()
- {
- cout<<"---------------File List----------------"<<endl;
- list<Dir_Entry>::iterator det_it = dir_entry_table.begin();
- unsigned index = 1;
- if(det_it == dir_entry_table.end())
- {
- cout<<"NO FILE"<<endl;
- }
- else
- {
- for(;det_it != dir_entry_table.end();index++)
- {
- cout<<"FILE "<<index<<endl;
- cout<<left<<setw(15)<<"file name:"<<setw(FILE_NAME_MAX_SIZE)<<det_it->get_File_Name()<<endl;;
- cout<<setw(15)<<"first sector:"<<setw(3)<<det_it->get_First_Sector()<<endl;
- cout<<setw(15)<<"sector number:"<<setw(3)<<det_it->get_Sector_Num()<<endl;
- if((++det_it) != dir_entry_table.end())
- {
- cout<<"****************************************"<<endl;
- }
- }
- }
- cout<<"****************************************"<<endl;
- cout<<setw(21)<<left<<"disk available space:"<<get_Avai_Sector_Num() * SECTOR_SIZE<<" bytes"<<endl;
- cout<<setw(21)<<"disk total space:"<<FAT.size() * SECTOR_SIZE<<" bytes"<<endl;
- cout<<"------------End of File List------------"<<endl;
- }
- unsigned int Disk_Routine::get_Avai_Sector_Num()
- {
- unsigned int avai_sector_num = 0;
- vector<int>::iterator fat_it = FAT.begin();
- for(;fat_it != FAT.end();fat_it++)
- {
- if(*fat_it == 0)
- {
- avai_sector_num++;
- }
- }
- return avai_sector_num;
- }
main.cpp
- #include <iostream>
- #include <string>
- #include <limits>
- #include "Disk_Routine.h"
- using std::string;
- using std::cerr;
- using std::cout;
- using std::cin;
- using std::endl;
- using std::numeric_limits;
- using std::streamsize;
- int main()
- {
- Disk_Routine dr(20);
- char c;
- bool flag = true;
- string file_name;
- unsigned int file_size;
- cout<<"OS BIG WORK NO 4 BY MARCUSXING 2009 12 9"<<endl;
- cout<<"****************************************"<<endl;
- cout<<"please select a number to do something"<<endl;
- cout<<"h:help message"<<endl;
- cout<<"q:quit"<<endl;
- cout<<"1:create a file"<<endl;
- cout<<"2:recreate a exist file"<<endl;
- cout<<"3:delete a file"<<endl;
- cout<<"4:display the FAT"<<endl;
- cout<<"5:display the File List"<<endl;
- while(flag)
- {
- cout<<"please enter your choice:";
- if(!cin)
- {
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- }
- cin>>c;
- switch(c)
- {
- case '1':
- case '2':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cout<<"please enter the file name:";
- cin>>file_name;
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cout<<"please enter the file size(unit:byte):";
- if(!(cin>>file_size))
- {
- cerr<<"your input has something wrong"<<endl;
- flag = false;
- break;
- }
- if(static_cast<int>(file_size) <= 0)
- {
- cerr<<"file size must be a positive number"<<endl;
- break;
- }
- if(c == '1')
- {
- dr.write(file_name,file_size);
- }
- else if(c == '2')
- {
- dr.rwirte(file_name,file_size);
- }
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- break;
- case '3':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cout<<"please enter the file name:";
- cin>>file_name;
- dr.del(file_name);
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- break;
- case '4':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- dr.disp_FAT();
- break;
- case '5':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- dr.disp_Dir_Table();
- break;
- case 'q':
- flag = false;
- break;
- case 'h':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cout<<"h:help message"<<endl;
- cout<<"q:quit"<<endl;
- cout<<"1:create a file"<<endl;
- cout<<"2:recreate a exist file"<<endl;
- cout<<"3:delete a file"<<endl;
- cout<<"4:display the FAT"<<endl;
- cout<<"5:display the File List"<<endl;
- break;
- default:
- cout<<"are you kidding me?"<<endl;
- return -1;
- break;
- }
- }
- return 0;
- }
- 【OS课程设计二】模拟DOS系统文件的物理结构和管理使用
- os课程设计:实现可变分区管理的模拟
- 数据结构课程设计--“模拟图书管理系统的开发”
- Java 模拟文件管理系统(仿DOS环境)
- 硬盘物理结构和FAT文件结构分析(二)
- 硬盘物理结构和FAT文件结构分析(二)
- OS-文件管理模拟程序
- 【OS课程设计一】动态分区式存储管理的存储分配和回收
- 关于文件管理系统的数据结构模拟
- 关于文件管理系统的数据结构模拟
- 脚本和物理系统的结合使用
- 操作系统课程设计 基于DOS的多任务系统的实现
- 操作系统的文件物理结构
- 【Visual C++】游戏开发笔记二十 游戏基础物理建模(二) 重力系统的模拟
- 【Visual C++】游戏开发笔记二十 游戏基础物理建模(二) 重力系统的模拟
- 【Visual C++】游戏开发笔记二十 游戏基础物理建模(二) 重力系统的模拟
- 【Visual C++】游戏开发笔记二十 游戏基础物理建模(二) 重力系统的模拟
- 课程设计之职工工资的管理系统
- Tomcat6.0连接池配置
- 日记
- 寒冰老师的LINUX教程
- 【OS课程设计一】动态分区式存储管理的存储分配和回收
- 看到的帖子
- 【OS课程设计二】模拟DOS系统文件的物理结构和管理使用
- JavaScript类型转换与运算符
- 今天发现了新大陆
- 梯形法求定积分(一)设计梯形法求积分的类模板,梯形法
- 忠言逆耳
- 梯形法求定积分(二)--设计一个函数模板,独立的非成员函
- 南昌大学学生学习服务中心标志
- JavaScript参数可变函数和动态函数
- 什么叫专业?