OS-内存分配回收(最佳适应,最差适应,首次适应)

#include 

using namespace std;
class freeMemoryTable
{
    public:
    int id;
    int begin;
    int size;
    int status;
};
class Node
{
    public:
    freeMemoryTable data;
    Node *pre;
    Node * next;

};
Node* first;
Node* endm;
int flag;
int InitBlock()
{

    first->pre=NULL;
    first->next=endm;
    first->data.status=1;
    endm->pre=first;
    endm->next=NULL;
    endm->data.id=1;
    endm->data.begin=40;
    endm->data.size=1000;
    endm->data.status=0;
    cout<<"Init success"<next;
    Node *q;
    int temp1=999999;
    int temp2=-99999;
    int index=0;
    int index2=0;
    if(type==1)//Sort by adress
    {
         for(;p!=NULL;p=p->next)
         {

                if((p->data.size-request)>=0&&(p->data.status!=1))
                {
                    return index;
                }
                index++;
         }
    }
    else if(type==2)//Sort by memory <
    {
         for(;p!=NULL;p=p->next)
         {

                if((p->data.size-request)>=0&&(temp1>p->data.size)&&(p->data.status!=1))
                {
                             temp1=p->data.size;
                             index2=index;
                }
                index++;
         }
         cout<
    {
            for(;p!=NULL;p=p->next)
                {

                if((p->data.size-request)>=0&&(temp2data.size)&&(p->data.status!=1))
                {
                             temp1=p->data.size;
                             index2=index;
                }
                index++;
                }
                cout<next;
             int j=0;
         for(;p!=NULL;p=p->next)
         {

                if(index==j)
                {
                    return p;
                }
                j++;
         }
}
void add(Node *q)
{
    endm->next=q;
    q->pre=endm;
    q->next=NULL;
    endm=q;
}
void operation(int request,Node*p)
{
       if(p->data.size==request)
       {
           p->data.status=1;
       }
       else
       {
          p->data.status=1;

          Node *q =new Node();
          q->data.id=p->data.id+1;
          q->data.status=0;

          q->data.size=p->data.size-request;
           p->data.size=request;
          add(q);
       }
}
void printM()
{
    Node *p=first->next;
    cout<<"------------------------"<next)
         {
           cout<<"id "<data.id<<" Size "<data.size
           <<" Status "<data.status<next;
    Node *qq=p->pre;
    bool flag=true;
     for(;p!=NULL&&flag;p=p->next)
         {

           if(p->data.id==id)
           {
               flag=false;
               break;
           }
           if(flag)
            qq=p;
         }
    if(p->next!=NULL)
    {
        if(!qq->data.status&&!p->next->data.status)
        {
                cout<<"Enter1"<data.size+=p->data.size;
                  q->data.size+=p->next->data.size;
                  q->next=p->next->next;

        }
        else if(!qq->data.status&&p->next->data.status)
        {
            cout<<"Enter2"<data.size+=p->data.size;
                  q->next=p->next;
                  p->next->pre=q;
        }
        else if(qq->data.status&&!p->next->data.status)
        {
                cout<<"Enter3"<data.status=0;
                  p->data.size+=p->next->data.size;
                  p->next=p->next->next;

        }
        else
        {
              cout<<"Enter4"<data.status=0;
        }
    }
    else
    {
           if(!qq->data.status)
            {
                cout<<"Enter5"<data.size+=p->data.size;

                  q->next=p->next->next;
            }
             else
             {  cout<<"Enter6"<data.status=0;
             }
    }
}
void menu()
{
    cout<<"1.first fit"<>type;
         int req;
         if(type!=4)
         {
            cout<<"Request size"<>req;
            operation(req,searchM(sortf(type,req)));
         }
         else
         {
             cout<<"Memory id"<>id;
             MemoryRe(id);
         }

         printM();
     }

    return 0;
}

你可能感兴趣的:(操作系统OS)