首页 > 解决方案 > 在文件系统模拟器中初始化磁盘

问题描述

我目前有两个正在使用的文件。我有以下代码,问题来自这个网站:https ://www.it2051229.com/filesystemsimulation.html

/* fs.h
* Various definitions for OSP Practical Case Study E
*/
#ifndef FS_H
#define FS_H
/* Prevent multiple inclusion */
#include<stdint.h>
/* The bitmap */
extern uint8_t bitmap[142];
/* 568Kb disk with 512b blocks-> 1136 bits for bitmap -> 142 bytes
*/
/* The directory entry */
struct entry
{
int8_t user;
int8_t name[9];
int8_t extension[4];
int16_t blockcount;
int16_t block[24];
};
/* The Directory */
extern struct entry directory[64];
/* extern means its defined in another
file, prevents multiple definition
errors
*/
int toggle_bit(int block);
/* Toggles the value of the bit ’block’, in
the external array ’bitmap’.
returns the current value of the bit
Does NOT validate ’block’!!!
*/
int block_status(int block);
/* Returns the status of ’block’,
in the external array bitmap
returns 0 if bitmap bit is 0,
not 0 if bitmap bit is 1
Does NOT validate block!!!
*/
#endif

另一个文件:

/* fs.c
Some useful functions for OSP Practical Case Study E
*/
#include"fs.h"
uint8_t bitmap[142];
struct entry directory[64];
int toggle_bit(int block)
{
int elem=block/8;
int pos=block%8;
int mask=1<<pos;
bitmap[elem]ˆ=mask;
return bitmap[elem]&mask;
}
int block_status(int block)
{
int elem=block/8;
int pos=block%8;
int mask=1<<pos;
return bitmap[elem]&mask;
}

在 main.c 中:

#include<stdio.h>
/* stdio.h will be found in the system path */
#include"fs.h"
/* fs.h will be found in the local path */
int main(int ac, char**av)
{
    //here i am going to intialize the disk
    return 0;
}

我总共有 7 项任务要做

  1. 初始化磁盘
  2. 列出目录中的文件
  3. 显示自由位图
  4. 打开/创建文件
  5. 读取文件
  6. 写文件
  7. 删除文件

我了解其余的任务,并且我相信我可以完成它们。我不知道哪个磁盘以及如何初始化磁盘。您可以查看链接以获得更好的理解。

标签: clinuxoperating-systemfilesystemsfilestream

解决方案


只需在您的真实文件系统上创建一个大小为 320kbyte(可能为 320kibibytes)的真实文件。这是你的磁盘。用常规打开文件fopen

初始化您的“虚拟”磁盘意味着“格式化”它。据说您的虚拟磁盘的块大小应为 4kibibytes,并且您的目录文件(某种自定义 MBR)应该只有 1 个块大,它应该是第一个块。1 个条目是 32 字节大,允许在您的目录块中存储 128 个条目。

初始化方法(格式化),只需确保前 4096 个字节,也就是磁盘映像的第一个块全部为零,然后连续复制 128 次struct entry,而char user每个条目中的变量的值为 '1'表示一个空闲的目录条目。

变量“1”以外的任何其他值都char user表示已使用的目录条目。

某种快速格式只会将每个条目变量设置char user为“1”。


推荐阅读