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

📄 memory.c

📁 操作系统可变分区算法 用首次适应算法实现 基于C
💻 C
字号:
#include <stdio.h>

#define WAITING 0
#define RUNNING 1
typedef int STATE;
typedef struct FreeBlock
{
	int begin;
	int size;
    struct FreeBlock* next;
} FreeBlock;
typedef struct RunBlock
{
	int begin;
	int size;
	STATE state;
	struct RunBlock* next;
}RunBlock;
/*
  Param: mneedAllocBlock is needed to Allocated;
  Param: freeHead is the head of the linked list;
  Return:RunBlock is the Allocated block,wheater allocated successfully or not
   is predicted by Param state,WAITING is not successful;
*/
RunBlock Allocate(int needAllocBlock,FreeBlock* freeHead)
{
	RunBlock run={0,0,WAITING,NULL};
	FreeBlock* tempHead=NULL;
	tempHead=freeHead;
	while(tempHead!=NULL)
	{
		if(needAllocBlock<=tempHead->size)
		{
			run.begin=tempHead->begin;
			run.size=needAllocBlock;
            run.state=RUNNING;

			tempHead->begin+=needAllocBlock;
			tempHead->size-=needAllocBlock;
		}
		tempHead=tempHead->next;
	}
	return run;
}

int main()
{
	FreeBlock* freeHead=NULL;
	RunBlock* freeRun=NULL;
    int needAllocBlockArr[]={10,20,30,15,13,18,31};
	FreeBlock freeBlock;

	freeBlock.begin=1;
	freeBlock.size=100;
	freeBlock.next=NULL;
    freeHead=&freeBlock;

	Allocate(needAllocBlockArr[0],freeHead);
	printf("HelloWorld!");
	getchar(); 
	return 0;
}

⌨️ 快捷键说明

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