File physical structure and storage space management

Disk structure:

Disks are also partitioned as memory, and the block size and memory block size are the same for data exchange.

1. Physical Structure of Files

1. Continuous allocation

Files are continuously allocated on blocks of disks, with the most efficient searching and the fastest head movement, but with the most fragmentation, they are not easy to expand.

The following sequential allocation logic is implemented in Python

class Category: #File directory
    def __init__(self,size):
        self.table=[]
        self.disk = Disk(size) #Analog Disk

    def append(self,file_name,length):
        t=[None]*2
        t[0] = file_name
        t[1] = self.disk.Distribution(length)
        self.table.append(t)


class Disk:
    def __init__(self,size):
        self.table=[None]*size
    def Distribution(self,length):
        for index,v in enumerate(self.table):
            if v==None and len(self.table)-length>0:
                for j in range(index,index+length):
                    self.table[j] = True
                return index

        print("Disk is full")
        return -1
if __name__ == '__main__':
    category = Category(100)
    category.append("one",20)
    category.append("three",30)
    category.append("tow",15)

    print(category.table)
    print(category.disk.table)

2. Link Assignment

(1) Explicit links (random access is supported)

File catalog table FCB:

file name .... Starting block number
a.txt 0

FAT file allocation table (long in memory, easy to find):

One disk, one FAT table

Physical Block Number Next physical block number (-1 means no)
0 3
1 2
2 -1
3 6
4 -1
6 -1

The above a.txt file is stored in 0->3->6 blocks in turn

(2) Implicit Links

Only sequential access is supported, there are contiguous blocks, each block also has a pointer to the next block

FCB

file name .... Starting block number End block number
a.txt 0 6

The following is a Python simulation

class Category:
    def __init__(self,fat_size):
        self.table=[]
        self.fat = FAT(fat_size)

    def append(self,file_name,size):
        file =[None]*3
        file[0] = file_name
        file[1] = size
        file[2] = self.fat.FindEmpty()
        if file[2]==-1:
            print("No free blocks")
            return
        self.fat.Distribution(file[2],file[1])
        self.table.append(file)






class FAT:
    def __init__(self,size):
        self.table=[]
        for i in range(size):
            t = list()
            t.append(i)
            t.append(None)
            self.table.append(t)





    def Distribution(self,cur,size):
        self.table[cur][1] = -1
        for c in range(size):
            self.table[cur][1] = self.FindEmpty()
            cur = self.table[cur][1]
        self.table[cur][1] = -1

    def FindEmpty(self):
        for i in self.table:
            if i[1]==None:
                return i[0]
        return -1


if __name__ == '__main__':
    fat_size = 100
    category = Category(fat_size)
    #
    category.append("one",4)
    category.append("tow",2)

    category.append("three",20)


    print(category.table)
    for w in category.fat.table:
        print(w[0],w[1])



3. Index Allocation

Support random access

FCB

file name ..... Index block number
a.txt 13

Index tables (each file has an index table, and the FCB holds the physical block number of the index table)

Logical Block Physical Block
0 6
1 98
2 5
3 12
4 4

The file above has a block number of 6->98->5->12->4. One index table is not enough to link another physical block to create another index table

This chain is slow and typically uses a multilevel index.



2. File Storage Space Management

partition

A disk is divided into several file volumes or zones, A disk B disk D disk, etc. These are logical partitions.

Usually a disk is divided into several logical partitions, or several disks can be divided into one logical partition

Partition structure

A partition is divided into a directory area and a file area, which stores basic directory information and indexes, and a file area for specific data.

File Free Space Management

1. Free Zone Chain List Method

Link consecutive free blocks into a chained list, find the best free area to use, delete the free chained list nodes, and add the tail of the link when recycling

2. Bitmap

0 1 2 3 4 5
1 1 1 0 1 1

Bit 1 represents a non-idle block, calculates the location of the bit based on the disk block number, and then manages it

Tags: Linux Python

Posted on Wed, 11 Mar 2020 22:46:45 -0400 by slamMan