c_cpp 用于简单内存分配算法的模拟器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 用于简单内存分配算法的模拟器相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TotalMemoryAmount      5000					  ##UL
#define MemRequestMin          40					  ##UL
#define MemRequestMax          1000					  ##UL
#define MemRequestSlice        20					  ##UL
#define MemThreshold           1					  ##UL
#define TotalTime              1000000					  ##UL
#define TAllocMin              5					  ##UL
#define TAllocMax              30					  ##UL
#define TRequestMin            1					  ##UL
#define TRequestMax            5					  ##UL
    
#define COUNTER
#define rand_time(t_min,t_max) (unsigned long) ((rand() * 1.0 / RAND_MAX * (t_max - t_min) + t_min))
#define rand_size (unsigned long) ((rand() * 1.0 / RAND_MAX) * (MemRequestMax - MemRequestMin) + MemRequestMin)/MemRequestSlice * MemRequestSlice
#ifdef COUNTER
#define FORMAT "\rTime = %08lu, Reqs = %08lu, Fails = %08lu, Ratio = %05.2f%%"
#endif /* COUNTER */
    typedef struct tag_tblock {
    unsigned char free;
     unsigned long size, time;
     struct tag_tblock *next;
} tblock;

tblock *blocks, *head, *ptr;
unsigned char method;

const char *descs[6] =  { "FIRST FIT", "BEST  FIT", "WORST FIT", "LAST  FIT", "BEST  FIT REVERSED", "WORST FIT REVERSED" };

static int mem_alloc(const unsigned long size, const unsigned long time) 
{
    unsigned long dif = (method == 1 || method == 4) ? 0xFFFFFFFFUL : 0;

    ptr = NULL;
    blocks = head;
    
    do
	if ((blocks->free && blocks->size >= size) 
	     &&(!method && !ptr || method == 1 
		&&blocks->size - size < dif 
		||(method == 2 && blocks->size - size > dif) 
		||method == 3 || (method == 4 
				  &&blocks->size - size <= dif) 
		||method == 5 && blocks->size - size >= dif)) {
	    dif = blocks->size - size;
	    ptr = blocks;
	}
    while ((blocks = blocks->next) != NULL);
    blocks = ptr;
    if (!blocks)
	return 1;
    if (dif < MemThreshold) {
	blocks->time = time;
	return blocks->free = 0;
    } else {
	ptr = (tblock *) malloc(sizeof(tblock));
	ptr->size = dif;
	ptr->next = blocks->next;
	ptr->free = 1;
	blocks->size = size;
	blocks->next = ptr;
	blocks->time = time;
	return blocks->free = 0;
    }
}

int main() 
{
    unsigned int randseed = (unsigned int) time(NULL);
    unsigned long numfails, numreqs, time_sim, time_req;
    float ratio[6];

    for (method = 0; method < 6; method++) {
	srand(randseed);
	printf("Using the following parameters:\n");
	printf("TotalMemoryAmount = %lu\n", TotalMemoryAmount);
	printf("MemRequestMin     = %lu\n", MemRequestMin);
	printf("MemRequestMax     = %lu\n", MemRequestMax);
	printf("MemRequestSlice   = %lu\n", MemRequestSlice);
	printf("MemThreshold      = %lu\n", MemThreshold);
	printf("TotalTime         = %lu\n", TotalTime);
	printf("TAllocMin         = %lu\n", TAllocMin);
	printf("TAllocMax         = %lu\n", TAllocMax);
	printf("TRequestMin       = %lu\n", TRequestMin);
	printf("TRequestMax       = %lu\n", TRequestMax);
	printf("Method            = %s\n\n", descs[method]);
	head = (tblock *) calloc(1, sizeof(tblock));
	if (!head) {
	    printf("Error: not enough memory");
	    exit(1);
	}
	head->size = TotalMemoryAmount;
	head->free = 1;
	time_sim = numfails = numreqs = 0;
	while (time_sim < TotalTime) {
	    time_req = rand_time(TRequestMin, TRequestMax);
	    while (time_req--) {
		blocks = head;
		while (blocks->next)
		    if (blocks->free && blocks->next->free) {
			blocks->size += blocks->next->size;
			ptr = blocks->next;
			blocks->next = blocks->next->next;
			free(ptr);
		    } else
			blocks = blocks->next;
		blocks = head;
		
		do
		    if (!blocks->free)
			if (!blocks->time--)
			    blocks->free = 1;
		while ((blocks = blocks->next) != NULL);
		time_sim++;
		
#ifdef COUNTER
		if (!(time_sim % 100))
			printf(FORMAT, time_sim, numreqs, numfails, numfails * 100. / numreqs);
		
#endif /* COUNTER */
		if (time_sim > TotalTime)
			break;
	    }
	    numreqs++;
	    numfails += mem_alloc(rand_size, rand_time(TAllocMin, TAllocMax));
	}
	blocks = head;
	while (blocks) {
	    ptr = blocks;
	    blocks = blocks->next;
	    free(ptr);
	}
	printf("\r%79s\rTotal number of requests  = %lu", " ", numreqs);
	printf("\nTotal number of failures  = %lu", numfails);
	printf("\nPercentage of failures    = %05.2f%%\n", ratio[method] = numfails * 100.f / numreqs);
    }
    printf("Comparison between failure rates of the six methods:\n\n");
    for (method = 0; method < 6; method++)
	printf("%-30s%05.2f%%\n", descs[method], ratio[method]);
    return 0;
}

以上是关于c_cpp 用于简单内存分配算法的模拟器的主要内容,如果未能解决你的问题,请参考以下文章

模拟操作系统中进程的三种基础状态与内存的分配和回收(最佳适配算法)

jvm内存模型-回收算法-和内存分配以及jdkjrejvm是什么关系(阿里,美团,京东面试题)

c_cpp 注意需要进行内存分配

c_cpp 带有内存分配的C中的3D矩阵

操作系统中常见的调度算法

操作系统常用调度算法