链表在操作系统的文件系统中有多种运用,其中一些包括:
- 目录结构: 文件系统通常使用链表结构来组织目录。每个目录都可以看作是一个包含文件名和指向文件或其他子目录的指针的链表。这样的结构使得文件系统可以轻松地管理文件和目录的增删改查操作。
代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct File {
char name[50];
struct File *next;
} File;
File *directory = NULL;
void addFile(const char *name) {
File *newFile = (File*)malloc(sizeof(File));
strcpy(newFile->name, name);
newFile->next = directory;
directory = newFile;
}
void listFiles() {
File *current = directory;
while (current != NULL) {
printf("%s\n", current->name);
current = current->next;
}
}
int main() {
addFile("file1.txt");
addFile("file2.txt");
addFile("file3.txt");
listFiles();
return 0;
}
2.空闲块链表: 文件系统需要跟踪磁盘上哪些块是可用的以存储新文件或修改现有文件。通常使用。空闲块链表来管理这些可用的块,以便快速地分配和释放磁盘空间。
代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct FreeBlock {
int blockNumber;
struct FreeBlock *next;
} FreeBlock;
FreeBlock *freeList = NULL;
void addFreeBlock(int blockNumber) {
FreeBlock *newBlock = (FreeBlock*)malloc(sizeof(FreeBlock));
newBlock->blockNumber = blockNumber;
newBlock->next = freeList;
freeList = newBlock;
}
void printFreeBlocks() {
FreeBlock *current = freeList;
while (current != NULL) {
printf("%d\n", current->blockNumber);
current = current->next;
}
}
int main() {
addFreeBlock(1);
addFreeBlock(2);
addFreeBlock(3);
printFreeBlocks();
return 0;
}
3.文件分配表(File Allocation Table,FAT): FAT文件系统使用链表来记录文件的存储位置。每个文件都有一个文件分配表,其中包含指向文件数据块的链表。这种结构使得文件可以在磁盘上非连续地存储,从而更高效地利用磁盘空间。
#include <stdio.h>
#include <stdlib.h>
#define NUM_BLOCKS 10
typedef struct FATEntry {
int blockNumber;
struct FATEntry *next;
} FATEntry;
FATEntry *fileAllocationTable[NUM_BLOCKS];
void initializeFAT() {
for (int i = 0; i < NUM_BLOCKS; i++) {
fileAllocationTable[i] = NULL;
}
}
void addBlockToFAT(int fileIndex, int blockNumber) {
FATEntry *newEntry = (FATEntry*)malloc(sizeof(FATEntry));
newEntry->blockNumber = blockNumber;
newEntry->next = fileAllocationTable[fileIndex];
fileAllocationTable[fileIndex] = newEntry;
}
void printFAT() {
for (int i = 0; i < NUM_BLOCKS; i++) {
printf("File %d: ", i);
FATEntry *current = fileAllocationTable[i];
while (current != NULL) {
printf("%d -> ", current->blockNumber);
current = current->next;
}
printf("NULL\n");
}
}
int main() {
initializeFAT();
addBlockToFAT(0, 1);
addBlockToFAT(0, 2);
addBlockToFAT(0, 3);
addBlockToFAT(1, 4);
addBlockToFAT(1, 5);
printFAT();
return 0;
}
4.索引节点(inode)链表: 类Unix文件系统(如ext文件系统)使用inode来存储文件的元数据(如文件权限、所有者、大小等)。文件系统可以使用inode链表来快速访问文件的元数据,而不必每次都扫描整个磁盘。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILES 10
typedef struct Inode {
int inodeNumber;
char fileName[50];
// 其他元数据...
struct Inode *next;
} Inode;
Inode *inodeTable[MAX_FILES];
void initializeInodeTable() {
for (int i = 0; i < MAX_FILES; i++) {
inodeTable[i] = NULL;
}
}
void addInode(int inodeNumber, const char *fileName) {
Inode *newInode = (Inode*)malloc(sizeof(Inode));
newInode->inodeNumber = inodeNumber;
strcpy(newInode->fileName, fileName);
newInode->next = NULL;
// 在索引节点表中添加新的索引节点
int index = inodeNumber % MAX_FILES;
if (inodeTable[index] == NULL) {
inodeTable[index] = newInode;
} else {
Inode *current = inodeTable[index];
while (current->next != NULL) {
current = current->next;
}
current->next = newInode;
}
}
void printInodeTable() {
for (int i = 0; i < MAX_FILES; i++) {
printf("Inode %d: ", i);
Inode *current = inodeTable[i];
while (current != NULL) {
printf("(%s, %d) -> ", current->fileName, current->inodeNumber);
current = current->next;
}
printf("NULL\n");
}
}
int main() {
initializeInodeTable();
addInode(0, "file1.txt");
addInode(1, "file2.txt");
addInode(10, "file3.txt");
printInodeTable();
return 0;
}
5.日志结构: 一些现代文件系统(如日志结构文件系统)使用链表来管理日志条目。日志记录文件系统的操作,以确保文件系统在意外断电或系统崩溃时能够快速恢复一致的状态。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct LogEntry {
int operation; // 1: Create, 2: Delete, 3: Modify
char fileName[50];
// 其他操作相关信息...
struct LogEntry *next;
} LogEntry;
LogEntry *logHead = NULL;
void addLogEntry(int operation, const char *fileName) {
LogEntry *newEntry = (LogEntry*)malloc(sizeof(LogEntry));
newEntry->operation = operation;
strcpy(newEntry->fileName, fileName);
newEntry->next = NULL;
if (logHead == NULL) {
logHead = newEntry;
} else {
LogEntry *current = logHead;
while (current->next != NULL) {
current = current->next;
}
current->next = newEntry;
}
}
void printLog() {
LogEntry *current = logHead;
while (current != NULL) {
printf("Operation: %d, File: %s\n", current->operation, current->fileName);
current = current->next;
}
}
int main() {
addLogEntry(1, "file1.txt"); // 创建文件
addLogEntry(3, "file2.txt"); // 修改文件
addLogEntry(2, "file1.txt"); // 删除文件
printLog();
return 0;
}
这些都是文件系统中链表的常见运用方式,它们使得文件系统能够高效地组织和管理大量文件和目录,并提供了快速的访问和恢复机制。