c - 如何创建由 256 字节组成的大小为 2^16 block 的数组

标签 c arrays operating-system nodes

所以我的标题说明了一切。我必须为我的操作系统类创建一个文件系统模拟。我对 C 仍然不太擅长,这就是我寻求帮助的原因。让我失望的一件事是我必须创建一个存储,它是一个由 2^16 block 组成的数组,每个 block 有 256 字节; block 只是一个普通的字节序列,直到它被结构覆盖(当然使用 union ):目录或文件元数据、 inode 或数据节点;还有一种节点类型:

这就是我所做的

#include "project1task3.h"

int main()
{

    createFileSystem();
}
/*
 * create the file system 
 * (i.e., allocate and initializing all structures and auxiliary data; 
 * create the superblock) 
 * 
 * 
 * */
void createFileSystem()
{
    // setting up file system "superblock"
    FS_NODE fileSystem;
    NODE typeNode;
    NODE* p; // pointer that will point to file or director
    //typeNode.type = DIR;
    int i;
    int j;
    size_t nodeSize;
     // allocate space for the fixed sie and the variable part (union)
    nodeSize = sizeof(FS_NODE) + sizeof(NODE);
    if ((memory = malloc(nodeSize)) == NULL)
    {
        for(i = 0; i < MEM;i++)
        {
            for(j = 0; j < BLOCK_SIZE;j++)
            {
                //not sure if this is how it should be
                memory->content.index[j] = 0;

            }
        }
    }




    strcpy(fileSystem.name,"\\");
    fileSystem.creat_t = 0; // set creation time to 0
    fileSystem.access = 400;  //the access should not allow for deletion or name change, 400 will allow only for read in
    fileSystem.owner = 000;// no permissions yet basically none
    fileSystem.size = 0;
    fileSystem.block_ref = BLOCK_SIZE;
    fileSystem.access_t =0;
    fileSystem.mod_t = 0;
    /*
     *A file-descriptor node holds meta-data information about a file or a directory
     *  such as size, access rights, creation time, access time, and modification time, along with a pointer to actual content of the file or director 
     * */
    typeNode.content.fd.creat_t =0;
    typeNode.content.fd.access = 400;
    typeNode.content.fd.size=0;
    typeNode.content.fd.mod_t = 0;
    typeNode.content.fd.access_t = 0;
    //if((memory= malloc(sizeof *memory + MEM)) != NULL)

    /*
     * 
     * In case of a node with the type opf directory, the size indicates the number of files in the directory, 
     * and the block reference points to the index block that holds indices to all files in the directory.
     *  Assume that a directory may hold directly up to 127 files or directories; each index of the index block points to a file or a directory descriptor.
     *  Of course, each of the subdirectries may hold another 127 files or directories, and so on.
     * 
     * 
     * */

     /*
      * *
      * 
      * In case of a file, the size in the file descriptor indicates the actual size of the file. 
      * The block reference either ponts to a single data block 
      * if the size of the file is less than 254, or to an index block with an array of references to data blocks for file larger than 254.
      *  Assume that the maximum size of a file is 127 * 254 (i.e., maximum allowed for a one-level indexing).
      * 
      * 
      * 
      * */



}
/*
 * 
 * create a file (i.e., allocate one block for meta-level information; set the size to 0, the times to the current time, and the access rights to some default)
create of a directory (just like a file creation, but with a different type)
delete a file (return blocks and clean up your supporting structures; e.g., reset the bits in the bit vector)
delete a directory (delete the files from the directory, and then delete the directory; clean up)
obtain file information (type - file or directory, size, times, access right, owner) 
 * 
 * 
 * */
void createAFile()
{
}

void createDirectory()
{
}


void deleteFile()
{
}

void obtainFileInfo()
{

}

这是标题

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#define BLOCK_SIZE 256
#define MAX_NAME_LENGTH 128
#define DATA_SIZE 254
#define INDEX_SIZE 127
#define MEM 65536 //2^16

typedef char data_t;
typedef unsigned short index_t;

typedef enum
{
   DIR,
   FILEE,
   INDEX,
   DATA
} NODE_TYPE;

typedef struct fs_node
{
   char name[MAX_NAME_LENGTH];
   time_t creat_t; // creation time
   time_t access_t; // last access
   time_t mod_t; // last modification
   mode_t access; // access rights for the file
   unsigned short owner; // owner ID
   unsigned short size;
   index_t block_ref; // reference to the data or index block
} FS_NODE;

typedef struct node
{
   NODE_TYPE type;
   union
   {
      FS_NODE fd;
      data_t data[DATA_SIZE];
      index_t index[INDEX_SIZE];
   } content;
} NODE;

// storage blocks

NODE *memory; // allocate 2^16 blocks (in init
void createFileSystem();
void createAFile();
void createDirectory();
void deleteFile();
void obtainFileInfo();

我不知道如果我设置正确,但我认为这就是我应该如何设置节点内存来创建一个由 2^16 block 组成的数组,每个 block 256 字节。任何帮助将不胜感激。

最佳答案

由于空间尺寸是固定的,您应该能够像这样声明内存:

char space[65536][256];

关于c - 如何创建由 256 字节组成的大小为 2^16 block 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40569102/

相关文章:

c - 对于 mmaped 结构,静态数组更改为动态

arrays - R 在多维数组中设置子集和赋值

c++ - 为什么 4 个进程比 4 个线程好?

c - TCP服务器不等待客户端响应?

C链表节点未添加

c - 父子进程间通信 - 保持管道打开可以吗?

c - 为什么在中断处理程序中使用自旋锁

c - 如何检测是否有新行然后忽略它(fgets)

java - 这段检查数字是否为质数的代码有什么问题?

c - 内核如何探测驱动程序