操作系统实验五-文件管理

给出一个磁盘块序列: 1、2、3 …… 500,初始状态所有块为空的,每块的大小为 2k ,选择使用空闲表、空闲盘区链、位示图三种算法之一来管理空闲块,对于基于块的索引分配执行以下步骤:

  1. 随机生成 2k - 10k 的文件 50 个,文件名为 1.txt, 2.txt, ..., 50.txt, 按照上述算法存储到模拟磁盘中
  2. 删除 奇数.txt1.txt, 3.txt, ..., 49.txt) 文件
  3. 新创建 5 个文件(A.txt, B.txt, C.txt, D.txt, E.txt),大小为 7k, 5k, 2k, 9k, 3.5k,按照与步骤 1 相同的算法存储到模拟磁盘中
  4. 给出文件 A.txt, B.txt, C.txt, D.txt, E.txt 的盘块存储状态和所有空闲区块的状态

首先考虑如何根据文件名索引文件,这里模拟了 iNode 方法,简单实现了对其查、删、打印的操作,代码如下:

typedef struct {
  string name;
  int size;
  int num_block;
  int address[100];
} iNode;

vector<iNode> FCB;

iNode search(string name) {
  for (int i = 0; i < FCB.size(); i++) {
    if (FCB[i].name == name) {
      return FCB[i];
    }
  }
}

void remove(string name) {
  for (int i = 0; i < FCB.size(); i++) {
    if (FCB[i].name == name) {
      FCB.erase(FCB.begin() + i);
      break;
    }
  }
}

void Print() {
  for (int i = 0; i < FCB.size(); i++) {
    cout << FCB[i].name << " " ;
    for (int j = 0; j < FCB[i].num_block; j++) cout << FCB[i].address[j] << " ";
    cout << endl;
  }
}

能够对文件进行索引,算法的实现就容易了,这里仅展示简单的空闲链表法,其余方法参见文章底部链接

#include 
#include 
#include 
#include 
#include 
#define randint(start, end) (rand()%(end-start) + start)
#define BLOCK_NUM 500
#define BLOCK_SIZE 2000

typedef struct node_ {
  int address;
  struct node_ *next;
} Node, *List;

List FLLM_init();
void FLLM_create(string name, int size, List L);
void FLLM_remove(string name, List L);
void FLLM_print(List L);
void FLLM_free(List L);

int main(int argc, char* argv[]){
  if (argc < 2) {
    cout << "Please input file management algorithm and retry" << endl;
    return 1;
  }
  string mode = argv[1];
  cout << mode << endl;
  srand(1);

  List L = FLLM_init();

  for (int i = 0; i < 50; i++) {
    int size = randint(2000, 5001);
    stringstream namestream;
    string name;
    namestream << i+1 << ".txt";
    name = string(namestream.str());
    FLLM_create(name, size, L);
  }

  for (int i = 1; i < 50; i += 2) {
    stringstream namestream;
    string name;
    namestream << i << ".txt";
    name = string(namestream.str());
    FLLM_remove(name, L);
  }

  string names[5] = {"A.txt", "B.txt", "C.txt", "D.txt", "E.txt"};
  int sizes[5] = {7000, 5000, 2000, 9000, 3500};
  for (int i = 0; i < 5; i++) {
    FLLM_create(names[i], sizes[i], L);
  }

  Print();
  FLLM_print(L);

  FLLM_free(L);
  return 0;
}

List FLLM_init() {
  List head = new Node;
  head->address = BLOCK_NUM;
  List rear = head;
  for (int i = 0; i < BLOCK_NUM; i++) {
    List node = new Node;
    node->address = i;
    rear->next = node;
    rear = node;
  }
  rear->next = NULL;
  return head;
}

void FLLM_create(string name, int size, List L) {
  int block_num = ceil(1.0 * size / BLOCK_SIZE);
  if (L->address < block_num) return;
  List rear = L;

  iNode i_node;
  i_node.name = name;
  i_node.size = size;
  i_node.num_block = block_num;

  for (int i = 0; i < block_num; i++) {
    List tmp = rear->next;
    rear->next = rear->next->next;
    i_node.address[i] = tmp->address;
    L->address--;
    delete tmp;
  }
  FCB.push_back(i_node);
}

void FLLM_remove(string name, List L) {
  iNode i_node = search(name);
  remove(name);

  List rear = L;
  while (rear->next) rear = rear->next;
  for (int i = 0; i < i_node.num_block; i++) {
    List node = new Node;
    node->address = i_node.address[i];
    rear->next = node;
    rear = node;
    L->address++;
  }
  rear->next = NULL;
}

void FLLM_print(List L) {
  List rear = L->next;
  int cnt = 0;
  while (rear) {
    printf("%3d ", rear->address);
    cnt++;
    if (cnt % 20 == 0) cout << endl;
    rear = rear->next;
  }
  cout << endl << "free block number:" << L->address << endl;
}

void FLLM_free(List L) {
  List rear = L->next;
  while (L) {
    delete L;
    L = rear;
    if (rear) rear = rear->next;
  }
}

完整代码见 Github

你可能感兴趣的:(OS)