⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 索引文件.c

📁 包含操作系统原理书籍中所提到的很多方法的实现函数
💻 C
字号:
/*磁盘索引文件的源程序**/
#include <stdio.h>
#include <stdlib.h>

/*下面是文件系统定义的数据结构*/

struct index
{       /*文件索引表的定义*/
	int lr[32];  /*逻辑块号数组*/
	int pr[32]; /*物理块号数组*/
	char st[32]; /*状态数组*/
}*wq;
 
struct list
{
	char names[32]; /*文件名*/
	int  size[32]; /*文件大小*/
	struct index *p[32]; /*文件的索引表地址*/
}*HEAD;

struct que
{
	char name;
	int size;
	struct que *next;
}*job,*top,*head; /* the job list*/

int i,j, ly, li;
char bb;
int N=0;      /*N代表系统的总文件数*/
int M=32;     /*M代表系统中空闲磁盘块数*/
int J[4][8];  /*位示图数组*/
FILE *e;
int jobs=0;   /**/
 
void run()    /**/
{  
	int x,y,z;
	int FIND=0;
	for(x=0; x<N; x++)
		if(HEAD->names[x] == top->next->name)
			FIND = 1;
	if(FIND == 0)
    {
		if(top->next->size<=M)
		{ 
			ly=0;
			M-=top->next->size;
			HEAD->names[N]=top->next->name;
			HEAD->size[N]=top->next->size;
			wq=(struct index *)malloc(sizeof(struct index));
			HEAD->p[N]=wq;
			N++;
			for(z=0;z<32;z++)
			{ 
				wq->lr[z]=z;
                wq->pr[z]=0;
                wq->st[z]='N' ;
			}
			for( j = 0 ; (j < 4)&&(ly < top->next->size); j++ )
				for( i = 0; (i < 8)&&( ly < top->next->size); i++)
				{
					if(J[j][i]==0)
					{ 
						li=j*8+i;
						wq->pr[ly]=li;
						wq->st[ly]='Y';
						ly++;
						J[j][i]=1; 
					}
				}
		}
		else
		{ 
			fprintf(e,"\nThere are no free blocks in the memory now!\n");
			fprintf(e,"File %c must wait!\n",top->next->name);
		}
	}
    else
    { 
        fprintf(e,"\n  .......  File %c  has been  loaded  in  the  system!  ....\n",top->next->name);
	}
	top->next=top->next->next;
	fprintf(e," ....... This time, the file directory:    -- \n");
	fprintf(e,"     NAME     INDEX_ADDRESS\n");
	for(x = 0; x < N; x++)
		fprintf(e,"      %c            %x\n",HEAD->names[x],HEAD->p[x]);
	for(x = 0; x < N; x++)
	{ 
		fprintf(e,"///////The index of FILE %c:///////\n",HEAD->names[x]);
		fprintf(e, "LOGIC_NUMBER   PHYSICS_NUMBER   FLAG\n");
		for(y = 0 ; y < HEAD->size[x]; y++)
			fprintf(e,"    %d              %d              %c\n",
		HEAD->p[x]->lr[y],HEAD->p[x]->pr[y],HEAD->p[x]->st[y]);
	}
	fprintf(e,"         This time, the bit mapping graph:        \n");
	for (j= 0; j < 4; j++)
	{  
		fprintf(e,"        ");
		for(i = 0; i < 8; i++)
			fprintf(e, " %d ",J[j][i]);
		fprintf(e, "\n") ;
	}
}

void main()
{
	int k;
	e=fopen("results.txt","w");
	for(j = 0; j < 4; j++)
		for(i = 0; i < 8; i++)
			J[j][i]= 0;
	HEAD=(struct list *)malloc(sizeof(struct list));
	for(i = 0; i < 32; i++)
	{  
		HEAD->names[i] = ' ';
		HEAD->size[i] = 0;
		HEAD->p[i]=NULL;
	}
	printf("please input number of jobs:");
	scanf("%d",&jobs);
	scanf("%c",&bb);
	job=(struct que *)malloc(sizeof(struct que));
	job->name=' ';
	job->size= 0;
	job->next=NULL;
	top=job;
	head=top;
	for(k=1;k<=jobs;k++)
	{ 
		job=(struct que *)malloc(sizeof(struct que));
		job->next=NULL;
		printf("FILE %d:\n",k);
		fprintf(e,"FILE %d:\n",k);
		printf("name and size:");
		scanf("%c,%d",&(job->name),&(job->size));
		scanf("%c",&bb);
		fprintf(e,"%c,%d\n",job->name, job->size);
		if(top->next == NULL)
		{ 
			top->next = job;
			head = job;
		}
		else
		{  
			head->next = job;
			head = job;
		}
	}
    for(k=1;k<=jobs;k++)
		run();
	fclose(e);
	for(i = 0; i < 32; i++)
	{  
		free(HEAD->p[i]);
		HEAD->p[i] = NULL;
	}
	free(HEAD);
	HEAD = NULL;
	while(top->next != NULL)
	{
		struct que *tmp = top->next;
		top = top->next;
		free(tmp);
		tmp = NULL;
	}
	free(top);
	top = NULL;
}

/*  程序运行过程如下:
	please input number of jobs:3
	FILE1:
	name and size:a,4
	FILE2:
	name and size:b,8
	FILE3:
	name and size:c,2 

	之后在程序运行的当前目录下生成一个result.txt文件
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -