编程语言
首页 > 编程语言> > 操作系统,最先适应分配算法代码

操作系统,最先适应分配算法代码

作者:互联网

以下代码如有疑问,请联系博主,最佳适应算法与最差适应算法只需添加简单的排序即可完成(本代码中未添加)

#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;                             //申请内存大小(以KB为单位)
    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() {
//  freopen("1.txt","w+",stdout);
    FF( );

}

标签:操作系统,thread,最先,list,next,算法,temp,memory,NULL
来源: https://blog.csdn.net/qq_44767279/article/details/112094010