以下代码如有疑问,请联系博主,最佳适应算法与最差适应算法只需添加简单的排序即可完成(本代码中未添加)
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#define MAX_THREAD 3
typedef struct freearea {
struct freearea *next;
int start_address;
int size;
} FREEAREA;
typedef struct require_memory {
struct require_memory *next;
char thread_name[10];
int size;
int duration;
} REQUIRE_MEMORY;
typedef struct thread_residence_memory {
struct thread_residence_memory *next;
char thread_name[10];
int start_address;
int size;
} THREAD_RESIDENCE_MEMORY;
FREEAREA init_free_area_table[5]= {
{NULL,10,10},
{NULL,40,30},
{NULL,80,5},
{NULL,145,15},
{NULL,180,20}
};
REQUIRE_MEMORY init_thread_require_memory_table[3]= {
{NULL,"thread_1",20,0},
{NULL,"thread_2",10,0},
{NULL,"thread_3",5,0}
};
THREAD_RESIDENCE_MEMORY init_thread_residence_memory_table[5]= {
{NULL,"a",0,10},
{NULL,"b",20,20},
{NULL,"c",70,10},
{NULL,"d",85,60},
{NULL,"e",160,20}
};
FREEAREA *p_free_area_list=NULL;
REQUIRE_MEMORY *p_thread_require_memory_queue=NULL;
THREAD_RESIDENCE_MEMORY *p_thread_residence_memory_list=NULL;
THREAD_RESIDENCE_MEMORY *tail_thread_residence_memory_list=NULL;
CRITICAL_SECTION CS_THREAD_MEMORY_LIST;
CRITICAL_SECTION CS_SCREEN;
CRITICAL_SECTION CS_FREEAREA_LIST;
HANDLE h_thread[MAX_THREAD];
void print_space(int num);
void display_thread_residence_memory_list();
FREEAREA *FF_initialize_freearea_list(FREEAREA *init_table,int num);
void FF_delete_freearea_list();
REQUIRE_MEMORY *FF_initialize_require_memory_list(REQUIRE_MEMORY *init_table,int num);
void FF_delete_require_memory_list();
THREAD_RESIDENCE_MEMORY *FF_initialize_thread_residence_memory_list
(THREAD_RESIDENCE_MEMORY *init_table,int num);
void FF_delete_thread_residence_memory_list();
void FF_thread(void *data);
int FF_require_memory(int size);
void FF_release_memory(int start_address,int size);
void FF();
void print_space(int num) {
int i;
for(i=0; i<num; i++) {
printf(" ");
}
}
void display_thread_residence_memory_list() {
THREAD_RESIDENCE_MEMORY *p;
char buffer[20];
p=p_thread_residence_memory_list;
printf("|-------------------|--------------------|------------------|\n");
printf("| thread_name | start_address(kB) | size(KB) |\n");
printf("|-------------------|--------------------|------------------|\n");
while(p!=NULL) {
printf("| %s",p->thread_name);
print_space(18-strlen(p->thread_name));
printf("| %d",p->start_address);
itoa( p->start_address, buffer, 10 );
print_space(19-strlen(buffer));
printf("| %d",p->size);
itoa(p->size, buffer, 10 );
print_space(17-strlen(buffer));
printf("|\n");
p=p->next;
};
printf("|-------------------|--------------------|------------------|\n\n");
}
FREEAREA *FF_initialize_freearea_list(FREEAREA *init_table,int num) {
FREEAREA *temp;
FREEAREA *head=NULL;
FREEAREA *tail=NULL;
int i;
for(i=0; i<num; i++) {
temp=(FREEAREA *)malloc(sizeof(FREEAREA));
temp->start_address=init_table[i].start_address;
temp->size=init_table[i].size;
temp->next=NULL;
if(head==NULL)
head=tail=temp;
else {
tail->next=temp;
tail=tail->next;
}
};
return head;
}
void FF_delete_freearea_list() {
FREEAREA *temp;
temp=p_free_area_list;
while(temp!=NULL) {
temp=p_free_area_list->next;
free(p_free_area_list);
p_free_area_list=temp;
}
p_free_area_list=NULL;
}
REQUIRE_MEMORY *FF_initialize_require_memory_list(REQUIRE_MEMORY *init_table,int num) {
REQUIRE_MEMORY *temp;
REQUIRE_MEMORY *head=NULL;
REQUIRE_MEMORY *tail=NULL;
int i;
for(i=0; i<num; i++) {
temp=(REQUIRE_MEMORY *)malloc(sizeof(REQUIRE_MEMORY));
strcpy(temp->thread_name,init_table[i].thread_name);
temp->size=init_table[i].size;
temp->duration=init_table[i].duration;
temp->next=NULL;
if(head==NULL)
head=tail=temp;
else {
tail->next=temp;
tail=tail->next;
}
};
return head;
}
void FF_delete_require_memory_list() {
REQUIRE_MEMORY *temp;
temp=p_thread_require_memory_queue;
while(temp!=NULL) {
temp=p_thread_require_memory_queue->next;
free(p_thread_require_memory_queue);
p_thread_require_memory_queue=temp;
}
p_thread_require_memory_queue=NULL;
}
THREAD_RESIDENCE_MEMORY *FF_initialize_thread_residence_memory_list(THREAD_RESIDENCE_MEMORY *init_table,int num) {
THREAD_RESIDENCE_MEMORY *temp;
THREAD_RESIDENCE_MEMORY *head=NULL;
THREAD_RESIDENCE_MEMORY *tail=NULL;
int i;
for(i=0; i<num; i++) {
temp=(THREAD_RESIDENCE_MEMORY *)malloc(sizeof(THREAD_RESIDENCE_MEMORY));
strcpy(temp->thread_name,init_table[i].thread_name);
temp->start_address=init_table[i].start_address;
temp->size=init_table[i].size;
temp->next=NULL;
if(head==NULL)
head=tail=temp;
else {
tail->next=temp;
tail=tail->next;
}
};
tail_thread_residence_memory_list=tail;
return head;
}
void FF_delete_thread_residence_memory_list() {
THREAD_RESIDENCE_MEMORY *temp=p_thread_residence_memory_list;
temp=p_thread_residence_memory_list;
while(temp!=NULL) {
temp=p_thread_residence_memory_list->next;
free(p_thread_residence_memory_list);
p_thread_residence_memory_list=temp;
}
p_thread_residence_memory_list=NULL;
}
void FF_thread(void *data) {
int start_address=-1;
THREAD_RESIDENCE_MEMORY *temp;
EnterCriticalSection(&CS_SCREEN);
printf("create thread:%s\n",((REQUIRE_MEMORY *)(data))->thread_name);
LeaveCriticalSection(&CS_SCREEN);
while(1) {
start_address=FF_require_memory(((REQUIRE_MEMORY *)(data))->size);
if(start_address>=0)
break;
else
Sleep(1000);
}
temp=(THREAD_RESIDENCE_MEMORY *)malloc(sizeof(THREAD_RESIDENCE_MEMORY));
strcpy(temp->thread_name,((REQUIRE_MEMORY *)(data))->thread_name);
temp->start_address=start_address;
temp->size=((REQUIRE_MEMORY *)(data))->size;
temp->next=NULL;
EnterCriticalSection(&CS_THREAD_MEMORY_LIST);
tail_thread_residence_memory_list->next=temp;
tail_thread_residence_memory_list=tail_thread_residence_memory_list->next;
LeaveCriticalSection(&CS_THREAD_MEMORY_LIST);
EnterCriticalSection(&CS_SCREEN);
printf("after %s %s\n",((REQUIRE_MEMORY *)(data))->thread_name,"get memory:");
display_thread_residence_memory_list();
LeaveCriticalSection(&CS_SCREEN);
Sleep(((REQUIRE_MEMORY *)(data))->duration);
FF_release_memory(start_address,((REQUIRE_MEMORY *)(data))->size);
}
int FF_require_memory(int size) {
int start_address = -1;
FREEAREA* p;
FREEAREA* p_next;
EnterCriticalSection(&CS_FREEAREA_LIST);
p = p_next = p_free_area_list;
while (p_next != NULL) {
if (size == p_next->size) {
start_address = p_next->start_address;
if (p_next == p_free_area_list)
p_free_area_list = p_next->next;
else
p->next = p_next->next;
free(p_next);
break;
} else if (size < p_next->size) {
start_address = p_next->start_address;
p_next->start_address += size;
p_next->size -= size;
break;
} else {
p = p_next;
p_next = p_next->next;
}
}
LeaveCriticalSection(&CS_FREEAREA_LIST);
return start_address;
}
void FF_release_memory(int start_address,int size) {
EnterCriticalSection(&CS_FREEAREA_LIST);
FREEAREA *temp, *p, *pp;
p=p_free_area_list;
temp = new FREEAREA;
temp->start_address=start_address;
temp->size=size;
temp->next=NULL;
int flag=0;
while(1) {
if(p->next!=NULL&&start_address+size<=p->next->start_address&&start_address>=p->start_address+p->size) {
temp->next=p->next;
p->next=temp;
flag=1;
}
if(flag==1||p->next==NULL) break;
p=p->next;
}
if(start_address>=p->start_address+p->size) {
if(flag==0)
p->next=temp;
} else {
if(flag==0) {
pp=p_free_area_list;
temp->next=pp;
pp->next=p_free_area_list->next;
p_free_area_list=temp;
}
}
FREEAREA *ppp;
ppp=p_free_area_list;
while(1) {
if(ppp->next!=NULL) {
if(ppp->start_address+ppp->size==ppp->next->start_address) {
ppp->size+= ppp->next->size;
if(ppp->next->next!=NULL)
ppp->next=ppp->next->next;
else {
ppp->next=NULL;
}
continue;
}
}
if(ppp->next!=NULL) ppp=ppp->next;
else break;
}
EnterCriticalSection(&CS_SCREEN);
THREAD_RESIDENCE_MEMORY *q;
q = p_thread_residence_memory_list;
if (q->start_address == start_address) {
p_thread_residence_memory_list = p_thread_residence_memory_list->next;
} else {
while (q->next != NULL) {
if (q->next->start_address == start_address) {
if (q->next == tail_thread_residence_memory_list) {
tail_thread_residence_memory_list = q;
}
q->next = q->next->next;
break;
}
q = q->next;
}
}
LeaveCriticalSection(&CS_SCREEN);
LeaveCriticalSection(&CS_FREEAREA_LIST);
}
void FF( ) {
int i=0;
REQUIRE_MEMORY *p;
HANDLE h_thread[MAX_THREAD];
InitializeCriticalSection(&CS_THREAD_MEMORY_LIST);
InitializeCriticalSection(&CS_FREEAREA_LIST);
InitializeCriticalSection(&CS_SCREEN);
printf("最先适应分配算法\n");
p_free_area_list=FF_initialize_freearea_list(init_free_area_table,5);
p_thread_require_memory_queue=FF_initialize_require_memory_list(init_thread_require_memory_table,3);
p_thread_residence_memory_list=FF_initialize_thread_residence_memory_list(init_thread_residence_memory_table,5);
p=p_thread_require_memory_queue;
while(p!=NULL) {
h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(FF_thread),p,0,NULL);
i++;
p=p->next;
};
WaitForMultipleObjects(MAX_THREAD,h_thread,TRUE,-1);
EnterCriticalSection(&CS_SCREEN);
printf("after all threads have finished:\n");
display_thread_residence_memory_list();
LeaveCriticalSection(&CS_SCREEN);
FF_delete_freearea_list();
FF_delete_require_memory_list();
FF_delete_thread_residence_memory_list();
getch();
printf("\n");
}
int main() {
FF( );
}