UNIX文件模拟系统实现 - C语言代码详解
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h>
int physic[300]; /* 文件地址缓冲区 / int style=1; / 文件的类型 */
char cur_dir[10]="c"; /* 当前目录 */
struct command { char com[10]; }cmd[12];
struct block { int n; /* 空闲的盘快的个数 / int free[50]; / 存放空闲盘快的地址 / int a; / 模拟盘快是否被占用 */ }memory[200];
struct block_super { int n; /* 空闲的盘快的个数 / int free[50]; / 存放进入栈中的空闲块 / int stack[50]; / 存放下一组空闲盘快的地址 */ }super_block;
struct node /* i结点信息 / { int file_style; / i结点 文件类型 / int file_length; / i结点 文件长度 / int file_address[100]; / i结点 文件的物理地址 */ } i_node[64];
struct dir /* 目录项信息 / { char file_name[10]; / 文件名 / int i_num; / 文件的结点号 / char dir_name[10]; / 文件所在的目录 */ } c[64];
void format() /* 格式化 / { int i,j,k; super_block.n=50; for(i=0;i<50;i++) / 超级块初始化 / { super_block.free[i]=i; / 存放进入栈中的空闲块 / super_block.stack[i]=50+i; / 存放下一组的盘块 */ }
for(i=0;i<64;i++) /* 信息初始化 / { for(j=0;j<100;j++) { i_node[i].file_address[j]=-1;/ 文件地址 / } i_node[i].file_length=-1; / 文件长度 / i_node[i].file_style=-1; / 文件类型 */ }
for(i=0;i<64;i++) /* 根目录区信息初始化 /
{
strcpy(c[i].file_name,"");
c[i].i_num=-1;
strcpy(c[i].dir_name,"");
}
for(i=0;i<200;i++) / 存储空间初始化 /
{
memory[i].n=0; / 必须有这个 /
memory[i].a=0;
for(j=0;j<50;j++)
{
memory[i].free[j]=-1;
}
}
for(i=0;i<200;i++) / 将空闲块的信息用成组链接的方法写进每组的最后一个块中 /
{
/ 存储空间初始化 */
if((i+1)%50==0)
{
k=i+1;
for(j=0;j<50;j++)
{
if(k<200)
{
memory[i].free[j]=k;/* 下一组空闲地址 /
memory[i].n++; / 下一组空闲个数 注意在memory[i].n++之前要给其赋初值 /
k++;
}
else
{
memory[i].free[j]=-1;
}
}
memory[i].a=0; / 标记为没有使用 /
continue; / 处理完用于存储下一组盘块信息的特殊盘块后,跳过本次循环 */
}
for(j=0;j<50;j++)
{
memory[i].free[j]=-1;
}
memory[i].n=0;
}
printf("已经初始化完毕\n");
printf("欢迎进入UNIX文件模拟系统!!!\n\n");
}
void write_file(FILE fp) / 将信息读入系统文件中 */ { int i; fp=fopen("system","wb"); for(i=0;i<200;i++) { fwrite(&memory[i],sizeof(struct block),1,fp); } fwrite(&super_block,sizeof(struct block_super),1,fp);
for(i=0;i<64;i++) { fwrite(&i_node[i],sizeof(struct node),1,fp); } for(i=0;i<64;i++) { fwrite(&c[i],sizeof(struct dir),1,fp); } fclose(fp); }
void read_file(FILE fp) / 读出系统文件的信息 */ { int i; fp=fopen("system","rb"); for(i=0;i<200;i++) { fread(&memory[i],sizeof(struct block),1,fp); }
fread(&super_block,sizeof(struct block_super),1,fp);
for(i=0;i<64;i++) { fread(&i_node[i],sizeof(struct node),1,fp); }
for(i=0;i<64;i++) { fread(&c[i],sizeof(struct dir),1,fp); } fclose(fp); }
void callback(int length) /* 回收磁盘空间 / { int i,j,k,m,q=0; for(i=length-1;i>=0;i--) { k=physic[i]; / 需要提供要回收的文件的地址 / m=49-super_block.n; / 回收到栈中的哪个位置 / if(super_block.n==50) / 注意 当super_block.n==50时 m=-1;的值 / { / super_block.n==50的时候栈满了,要将这个栈中的所有地址信息写进下一个地址中 / for(j=0;j<50;j++) { memory[k].free[j]=super_block.free[j]; } super_block.n=0; memory[k].n=50; } memory[k].a=0; if(m==-1) { m=49; / 将下一个文件地址中的盘块号回收到栈底中,这个地址中存放着刚才满栈的地址
的信息 / } super_block.free[m]=physic[i]; / 将下一个文件地址中的盘块号回收到栈中 */ super_block.n++; } }
void allot(int length) /* 分配空间 /
{
int i,j,k,m,p;
for(i=0;i<length;i++)
{
k=50-super_block.n; / 超级块中表示空闲块的指针 /
m=super_block.free[k]; / 栈中的相应盘块的地址 /
p=super_block.free[49]; / 栈中的最后一个盘块指向的地址 /
if(m==-1||memory[p].a==1) / 检测是否还有下一组盘块 /
{
printf("内存不足,不能够分配空间\n");
callback(length);
break;
}
if(super_block.n==1)
{
memory[m].a=1; / 将最后一个盘块分配掉 /
physic[i]=m;
super_block.n=0;
for(j=0;j<memory[m].n;j++) / 从最后一个盘块中取出下一组盘块号写入栈中 /
{
super_block.free[j]=memory[m].free[j];
super_block.n++;
}
continue; / 要跳过这次循环,下面的语句在IF中已经执行过 /
}
physic[i]=m; / 栈中的相应盘块的地址写进 文件地址缓冲区 */
memory[m].a=1;
super_block.n--;
}
}
void create_file(char filename[],int length) /* 创建文件 / { int i,j; for(i=0;i<64;i++) { if(strcmp(filename,c[i].file_name)==0) { printf("文件已经存在,不允许建立重名的文件\n"); return; } } for(i=0;i<64;i++) { if(c[i].i_num==-1) { c[i].i_num=i; strcpy(c[i].file_name,filename); strcpy(c[i].dir_name,cur_dir); / 把当前目录名 给新建立的文件 */ i_node[i].file_style=style; i_node[i].file_length=length; allot(length); for(j=0;j<length;j++) { i_node[i].file_address[j]=physic[j]; } break; } } }
void create_dir(char filename[]) /* 创建目录 / { style=0; / 0代表文件类型是目录文件 / create_file(filename,4); style=1; / 用完恢复初值,因为全局变量,否则 */
原文地址: https://www.cveoy.top/t/topic/f1Dx 著作权归作者所有。请勿转载和采集!