当前位置:文档之家› 空闲磁盘存储空间的管理:位示图法

空闲磁盘存储空间的管理:位示图法

空闲磁盘存储的管理:位示图法
1:建立相应的数据结构
2:磁盘上建立一个文件,文件长度设为10MB,为该文件来模拟一个磁盘,磁盘的物理块大小为512字节
3:显示每次磁盘的请求和空间释放后的位示图状态
4显示每次磁盘的请求和空间释放后的全磁盘的状态
5:模拟文件的创建和删除,从而产生磁盘潘快请求和释放,验证以上设计
代码:
// OS暑?期ú课?程ì设Θ?计?.cpp : 定¨义?控?制??应畖用?程ì序ò的?入?口ú点?。


//
#include"stdafx.h"
#include<iostream>
#include<string>
#include<math.h>
using namespace std ;
//文?件t类え?
class file
{
public:
string name ;
int occupy ;
int grade_block ;
int start ;
};
#define MAX_LINE 3
#define MAX_COLUMN 32
int BIT[32][1000];
int byte[MAX_LINE] ;
int file_count;
int judge[32] ;
int judge2[32];
int cycle ;
file f[1000];
void init(int line ,int column);
void show() ;
void set(int now_location, int occupy );
void clear(int now_location, int occupy);
void bitset(int index ,int temp_block_quantity);
void create_file(string temp_name, int temp_occupy );
void delete_file(string name);
bool byte_judge(int num,int i);
int _tmain(int argc, _TCHAR* argv[])
{
string cmd;
string file_name ;
int file_occupy ;
init( MAX_LINE, MAX_COLUMN ) ; //初?始?化ˉwhile ( cin >> cmd ) //
{
if( "q" == cmd || "Q" == cmd )
{
exit(0) ;
return 0 ;
}
cin >> file_name ;
if("create" == cmd)
{
cin >>file_occupy ;
create_file(file_name,file_occupy);
}
else
{
delete_file(file_name) ;
}
}
return 0;
}
void show()
{
for(int i = 0 ; i < MAX_LINE ; i ++)
{
for(int j = 0 ; j < MAX_COLUMN ; j ++)
{
//cout<<BIT[i][j] <<" ";
cout<<byte_judge(byte[i],j)<<" ";
}
cout<< endl ;
}
}
void set(int now_location, int occupy )
{
int line ;
int column ;
for(int i = 0 ; i < occupy ; i ++ )
{
line = (now_location - i) / MAX_COLUMN ;
column = (now_location - i ) % MAX_COLUMN ;
//BIT[line][column] = 1 ;
byte[line] |= judge[column];
}
}
//清?零?
void clear(int now_location, int occupy)
{
int line ;
int column ;
for(int i = 0 ; i < occupy ; i ++)
{
line = (now_location + i) / MAX_COLUMN ;
column = (now_location + i ) % MAX_COLUMN ;
//BIT[line][column] = 0 ;
byte[line] &= judge2[column];
}
}
void bitset(int index ,int temp_block_quantity)
{
int block_quantity = temp_block_quantity ;
int free_quantity = 0 ;
int line, column ;
int sign = cycle - 1 ;
for (;; cycle++ )
{
line = cycle / MAX_COLUMN ;
column = cycle % MAX_COLUMN ;
if( 0 == byte_judge(byte[line],column))
{
free_quantity ++ ;
}
else
{
free_quantity = 0 ;
}
if(free_quantity >= block_quantity )
{
f[index].start = cycle - block_quantity + 1;
set(cycle, block_quantity);
cycle ++;
cout<<"create file success"<<endl;
show();
return ;
}
if(cycle >= MAX_LINE * MAX_COLUMN-1)
{
cycle = -1;
free_quantity = 0;
}
if(sign == cycle )
{
break ;
}
}
cout<<"error: create file fail"<<endl;
}
void create_file(string temp_name, int temp_occupy )
{
int block_quantity ;
f[file_count].name = temp_name ;
block_quantity = temp_occupy / 512 + 1 ;
f[file_count].occupy=block_quantity ;
bitset(file_count ,block_quantity );
file_count ++ ;
}
//删?除y文?件t
void delete_file(string name)
{
for(int i = 0 ; i < file_count ; i ++)
{
if(name == f[i].name)
{
clear(f[i].start,f[i].occupy);
for(int j = i; j < file_count - 1; j ++)
{
f[j] = f[ j + 1 ];
}
file_count -- ;
cout<<"delete file success"<<endl;
show();
return ;
}
}
cout<<"ERROR THE FILE NOT EXIST"<< endl ;
}
//初?始?化ˉ
void init(int line ,int column)
{
int con = 1 ;
file_count = 0 ;
cycle = 0 ;
for(int i = 0; i < 32 ; i ++)
{
judge[i] = con ;
judge2[i]=judge[i];
judge2[i] ^= 0xffffffff;
con*=2;
}
for(int i =0 ; i<MAX_LINE ; i++)
byte[i]= 0;
}
bool byte_judge(int num,int i)
{
bool result ;
result = num & judge[i];
return result;
}。

相关主题