c - 在文件系统模拟器中初始化磁盘
问题描述
我目前有两个正在使用的文件。我有以下代码,问题来自这个网站: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 项任务要做
- 初始化磁盘
- 列出目录中的文件
- 显示自由位图
- 打开/创建文件
- 读取文件
- 写文件
- 删除文件
我了解其余的任务,并且我相信我可以完成它们。我不知道哪个磁盘以及如何初始化磁盘。您可以查看链接以获得更好的理解。
解决方案
只需在您的真实文件系统上创建一个大小为 320kbyte(可能为 320kibibytes)的真实文件。这是你的磁盘。用常规打开文件fopen
。
初始化您的“虚拟”磁盘意味着“格式化”它。据说您的虚拟磁盘的块大小应为 4kibibytes,并且您的目录文件(某种自定义 MBR)应该只有 1 个块大,它应该是第一个块。1 个条目是 32 字节大,允许在您的目录块中存储 128 个条目。
初始化方法(格式化),只需确保前 4096 个字节,也就是磁盘映像的第一个块全部为零,然后连续复制 128 次struct entry
,而char user
每个条目中的变量的值为 '1'表示一个空闲的目录条目。
变量“1”以外的任何其他值都char user
表示已使用的目录条目。
某种快速格式只会将每个条目变量设置char user
为“1”。
推荐阅读
- python - Python使用时加倍反斜杠;使用 open(C:\directory_name) 作为 file_object (PyCharm IDE)
- swift - 当我点击标记时,不会调用 Swift didTap 事件
- javascript - 从 DOM 和 JQuery 创建 JSON 数组
- sql - SQL teradata 在列中搜索字符串并创建新列
- vue.js - 在运行 Vue-Router 防护之前,如何读取和解析 cookie 并将结果存储在 Vuex 中
- c# - 实体类型
不是当前上下文模型的一部分 - git - 将元数据添加到 git commit 以获取有关如何测试的说明
- regex - 谷歌表格正则提取每个单词的第一个字母
- python - 枚举python中类的所有属性
- java - 如何使条形图透明