동적 분할 코드

23150 단어
파이프 프로그램
#include 
#include 
#include 
#include 
int main(void)
{
int fds[2];
pid_t pid;
if(pipe(fds) == -1)
{
perror("pipe error");
exit(1);
}
pid=fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}
if(pid == 0)//parent
{
char buf[1024];
int n;
close(fds[1]);
n = read(fds[0], buf, 1024);//          buf
write(STDOUT_FILENO, "child:",6);
write(STDOUT_FILENO, buf, n);//           
}
else//child
{
close(fds[0]);
write(fds[1], "hello world
", 12);// “hello world
” wait(NULL); } return 0; }

동적 분할 프로그램 C
#define _CRT_SECURE_NO_WARNINGS 1 
#include
#include
#include
#include
#define N 10000
int n1;//       
int n2;//      
struct kongxian
{
    int start;  //  
    int end;    //  
    int length;  //  
}kongxian[N];
struct zuoye
{
    int start;  //  
    int end;   //  
    int length;  //  
}zuoye[N];
int cmp1(const void *a, const void *b)
{
    return (*(struct kongxian *)a).start - (*(struct kongxian *)b).start;
}
int cmp2(const void *a, const void *b)
{
    return (*(struct zuoye *)a).start - (*(struct zuoye *)b).start;
}
void init()
{
    n1 = 1;  //          
    n2 = 0;  //      
    kongxian[0].start = 0;
    kongxian[0].end = 511;
    kongxian[0].length = 512;
}
void print1() //      
{
    int i;
    for (i = 0; i= len)  //      
                {
                    flag = 1;
                    break;
                }
            }
            if (!flag)
            {
                printf("      
"); } else { // zuoye[n2].start = kongxian[i].start; zuoye[n2].end = zuoye[n2].start + len; zuoye[n2].length = len; n2++; // 1 if (kongxian[i].length == len) // , { for (j = i; jzuoye[id].end) break; if (kongxian[i].end == zuoye[id].start) // { front = 1; t1 = i; } if (kongxian[i].start == zuoye[id].end) // { behind = 1; t2 = i; } } if (!front&&!behind) // { kongxian[n1].start = zuoye[id].start; kongxian[n1].end = zuoye[id].end; kongxian[n1].length = zuoye[id].length; n1++; // qsort(kongxian, n1, sizeof(struct kongxian), cmp1); // for (j = id; j

동적 구역 프로그램 c++
#include
#include
#include
#include 
using namespace std;
typedef struct freetable    
{
    long start; //       
    long length;    //    
    long state; //     
    struct freetable *next; //         
}freetable;

freetable *flist=NULL,*blist=NULL;  //    ,      ,     
long nfree=0;               //               

int initializtion(int i);    //     ,i=1     ,i=2    
int showlist(int i);         //   ,i=1     ,i=2    
int sortlist(int i);         //      ,1              ,2             ,3             
int firstbest(long j,long cnum,long csize); //j:1      ,2      
int worstfit(long cnum,long csize);     //      
int recover(long cnum);             //    
int menu();                 //  
int check(int i,freetable *cc);//         

int check(int i,freetable *cc)//         
{
    freetable *nn,*p;   //           。
    long caddress=cc->start,ccsize=cc->length,cstate=cc->state;// c         
    long plast,clast=caddress+ccsize;   //clast        
    nn=blist;   //nn       
    p=nn->next; //p        
    while(p!=NULL)
    {
        plast=p->start+p->length;  //   
        if ( !( (p->start>caddress&&plast>clast)||(p->startstate==cstate) return 0;//   
        p=p->next; 
    }
    nn=flist;   
    p=nn->next; //     
    while (p!=NULL)
    {
        plast=p->start+p->length;
        if ( !( (p->start>caddress&&plast>clast)||(p->startnext;                  
    }
    return 1;   //    
}

int initializtion(int i)    //   ,1   ,2   
{
    freetable *nn,*p;
    int num,f=0,k;
    do
    {
        if (i==1)
        {
            cout<>num;
        k=0;
        if (num>0) f=num;
    }while (f<=0);
    if (f)  
    {
        if (i==1) cout<start,&nn->length);
        if (i==2) scanf("%ld",&nn->state);else nn->state=++nfree;
        if (!check(i,nn)) 
        {
            cout<next=p->next;p->next=nn;}//          
    }
    nn=NULL;
    cout<next;
        cout<next;
        cout<state,nn->start,nn->length);
        nn=nn->next;
    }
    nn=NULL;
    cout<next==NULL) return 0;//     
    freetable *change,*last;
    freetable *newhead,*newlast=NULL;
    newhead=(freetable  *)malloc(sizeof(freetable ));newhead->next=NULL;
    while(flist->next!=NULL)
    {
        change=flist;
        last=flist->next;
        if (last!=NULL)//       ,last->next    
        {
            while(last->next!=NULL)
            {
                if (i==1) 
                {
                    if (change->next->start < last->next->start) change=last;
                }else if (i==2) 
                {
                    if (change->next->length < last->next->length) change=last;
                }else if (i==3) 
                {
                    if (change->next->length > last->next->length) change=last;
                }
                last=last->next;
            }
        }
        last=change->next;
        change->next=last->next;
        last->next=newhead->next;
        newhead->next=last;
    }
    free(flist);
    flist=newhead;
    newhead=newlast=last=change=NULL;
    return 0;
}

int menu()//    
{
    flist=(freetable *)malloc(sizeof(freetable)); flist->next=NULL;//    
    blist=(freetable *)malloc(sizeof(freetable)); blist->next=NULL;
    int i=1,j=1;
    while(i)
    {
        system("reset");
        cout<>i;j=1;
        system("reset");
        if (i==0) 
        {
            cout<>j;
                system("reset");
                if (j==0) break;
                else if (j==1||j==2) {initializtion(j);showlist(j);}
                else cout<>j;
                system("reset");
                if (j==0) break;
                else if (j==1||j==2) showlist(j);
                else cout<>j;
                system("reset");
                if (j==0) break;
                if (j!=1&&j!=2&&j!=3) 
                {
                    cout<>cnum>>csize;
                if (j==3) worstfit(cnum,csize);//      
                else firstbest(j,cnum,csize);//      ,      
            }
        }else if (i==4)
        {
            long cnum;
            while(j)
            {
                system("reset");
                cout<>cnum;
                recover(cnum);
                cout<>j;
            }
        }else cout<next!=NULL)
    {
        if (head->next->length >= csize) 
        {
            if (head->next->length==csize)
            {
                nn=head->next;
                nn->state=cnum;
                head->next=nn->next;
                nn->next=blist->next;
                blist->next=nn;
            }else
            {
                nn=(freetable  *)malloc(sizeof(freetable ));
                nn->start=head->next->start;
                nn->state=cnum;
                nn->length=csize;
                head->next->length-=csize;
                head->next->start+=csize;
                nn->next=blist->next;
                blist->next=nn;
            }
            csize=-1;
            break;
        }
        head=head->next;
    }
    if (csize==-1) cout<next!=NULL && flist->next->length >=csize)
    {
        if (flist->next->length==csize)
        {
            nn=flist->next;
            nn->state=cnum;
            flist->next=nn->next;
            nn->next=blist->next;
            blist->next=nn;
        }else
        {
            nn=(freetable  *)malloc(sizeof(freetable ));
            nn->start=flist->next->start;
            nn->state=cnum;
            nn->length=csize;
            flist->next->length-=csize;
            flist->next->start+=csize;
            nn->next=blist->next;
            blist->next=nn;
        }
        cout<next==NULL)
    {
        cout<next!=NULL)
    {
        if (busyhead->next->state==cnum)//    freelist
        {
            nn=busyhead->next;
            busyhead->next=nn->next;
            nn->next=freehead->next;
            freehead->next=nn;
        }else busyhead=busyhead->next;
    }
    sortlist(1);//1           
    freehead=flist;
    freehead=freehead->next;
    if (freehead==NULL)
    {
        cout<next!=NULL)//      
    {
        if ((freehead->length+freehead->start) == freehead->next->start)//  
        {
            nn=freehead->next;
            freehead->length=freehead->length+nn->length;
            freehead->next=nn->next;
            nn->next=NULL;
        }else freehead=freehead->next;
    }
    showlist(1);showlist(2);
    return 0;
}

int main()
{
    menu();
    return 0;
} 

동적 분할 알고리즘 C 강화
#include
#include
struct nodespace{
    int teskid;   //     
    int begin;    //      
    int size;     //    
    int status;   //    0    ,1     
    struct nodespace *next;  //     
};
 
void initNode(struct nodespace *p){
    if(p == NULL){  //           
        p = (struct nodespace*)malloc(sizeof(struct nodespace));
    }
    p->teskid = -1;
    p->begin = 0;
    p->size = 640;
    p->status = 1;
    p->next =NULL; 
}
 
/*
*         
*/ 
void myMalloc1(int teskid,int size,struct nodespace *node){
    while(node != NULL){
        if(node->status == 1){  //      
            if(node->size > size){  //               
                //         
                struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));
                p->begin = node->begin + size;
                p->size = node->size - size;
                p->status = 1;
                p->teskid = -1;
                //      
                node->teskid = teskid; 
                node->size = size;
                node->status = 0;
                //        
                p->next = node->next; 
                node->next = p;
                break; 
            }else if(node->size == size){ //               
                node->teskid = teskid; 
                node->size = size;
                node->status = 0;
                break;
            }   
        }
        if(node->next == NULL){
            printf("    ,       !
"); break; } node = node->next; } } /* * */ void myMalloc2(int teskid,int size,struct nodespace *node){ // struct nodespace *q = NULL; // while(node != NULL){ if(node->status == 1 && node->size >= size){ q = node; break; } // if(node->next == NULL){ printf(" , !
"); break; } else{ node = node->next; } } // while(node != NULL){ if(node->status == 1 && node->size >= size && node->size < q->size){ // q = node; } node = node->next; } if(q->size > size){ // // struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace)); p->begin = q->begin + size; p->size = q->size - size; p->status = 1; p->teskid = -1; // q->teskid = teskid; q->size = size; q->status = 0; // p->next = q->next; q->next = p; }else if(q->size == size){ // q->teskid = teskid; q->size = size; q->status = 0; } } void myFree(int teskid,struct nodespace *node){ if(node->next == NULL && node->teskid == -1){ printf(" !
"); } while(node != NULL){ if(node->status == 1 && node->next->status ==0 && node->next->teskid == teskid){ // node->size = node->size + node->next->size; struct nodespace *q = node->next; node->next = node->next->next; free(q); if(node->next->status == 1){ // node->size = node->size + node->next->size; struct nodespace *q = node->next; node->next = node->next->next; free(q); } break; }else if(node->status == 0 && node->teskid == teskid){ // node->status = 1; node->teskid = -1; if(node->next != NULL && node->next->status == 1){ // node->size = node->size + node->next->size; struct nodespace *q = node->next; node->next = node->next->next; free(q); } break; }else if(node->next == NULL){ // id printf(" !
"); break; } node = node->next; } } void printNode(struct nodespace *node){ printf("
"); printf(" -------------------------------------------------------
"); printf("| \t \t \t \t id\t|
"); while(node != NULL){ if(node->status==1){ printf("| %d\t\t%d\t\t%dKB\tfree\t \t|
", node->begin + 1, node->begin+node->size, node->size); }else{ printf("| %d\t\t%d\t\t%dKB\tbusy\t %d\t|
", node->begin + 1, node->begin+node->size, node->size, node->teskid); } node = node->next; } printf(" -------------------------------------------------------
"); } void destory(struct nodespace *node){ struct nodespace *q = node; while(node != NULL){ node = node->next; free(q); q = node; } } void menu(){ printf("1.
"); printf("2.
"); printf("3.
"); printf("4.
"); printf(" :"); } int main(){ // node struct nodespace *init = (struct nodespace*)malloc(sizeof(struct nodespace)); struct nodespace *node = NULL; initNode(init); // node = init; // int option; int teskid; int size; while(1){ printf(" :
1.
2.
3.
"); scanf("%d",&option); if(option == 1){ // while(1){ // printf(" :
1.
2.
3.
"); scanf("%d",&option); if(option == 1){ // printf(" 1 130 KB
"); myMalloc1(1,130,node); // 1 130 KB printNode(node); printf(" 2 60 KB
"); myMalloc1(2,60,node); // 2 60 KB printNode(node); printf(" 3 100 KB
"); myMalloc1(3,100,node); // 3 100 KB printNode(node); printf(" 2 60 KB
"); myFree(2,node); // 2 60 KB printNode(node); printf(" 4 200 KB
"); myMalloc1(4,200,node); // 4 200 KB printNode(node); printf(" 3 100 KB
"); myFree(3,node); // 3 100 KB printNode(node); printf(" 1 130 KB
"); myFree(1,node); // 1 130 KB printNode(node); printf(" 5 140 KB
"); myMalloc1(5,140,node); // 5 140 KB printNode(node); printf(" 6 60 KB
"); myMalloc1(6,60,node); // 6 60 KB printNode(node); printf(" 7 50 KB
"); myMalloc1(7,50,node); // 7 50 KB printNode(node); printf(" 6 60 KB
"); myFree(6,node); // 6 60 KB printNode(node); destory(node); // initNode(init); // node = init; // }else if(option == 2){ // printf(" 1 130 KB
"); myMalloc2(1,130,node); // 1 130 KB printNode(node); printf(" 2 60 KB
"); myMalloc2(2,60,node); // 2 60 KB printNode(node); printf(" 3 100 KB
"); myMalloc2(3,100,node); // 3 100 KB printNode(node); printf(" 2 60 KB
"); myFree(2,node); // 2 60 KB printNode(node); printf(" 4 200 KB
"); myMalloc2(4,200,node); // 4 200 KB printNode(node); printf(" 3 100 KB
"); myFree(3,node); // 3 100 KB printNode(node); printf(" 1 130 KB
"); myFree(1,node); // 1 130 KB printNode(node); printf(" 5 140 KB
"); myMalloc2(5,140,node); // 5 140 KB printNode(node); printf(" 6 60 KB
"); myMalloc2(6,60,node); // 6 60 KB printNode(node); printf(" 7 50 KB
"); myMalloc2(7,50,node); // 7 50 KB printNode(node); printf(" 6 60 KB
"); myFree(6,node); // 6 60 KB printNode(node); destory(node); // initNode(init); // node = init; // }else if(option == 3){ // break; }else{ printf(" , !
"); } } }else if(option == 2){ // while(1){ // printf(" :
1.
2.
3.
"); scanf("%d",&option); int n = option; // ,n == 1 , n == 2 if(option != 3){ while(1){ menu(); // scanf("%d",&option); if(option == 1 && n == 1){ // printf(" id :
"); scanf("%d%d",&teskid,&size); myMalloc1(teskid,size,node); printNode(node); }else if(option == 1 && n == 2){ // printf(" id :
"); scanf("%d%d",&teskid,&size); myMalloc2(teskid,size,node); printNode(node); }else if(option == 2){ printf(" id:
"); scanf("%d",&teskid); myFree(teskid,node); printNode(node); }else if(option == 3){ printNode(node); }else if(option == 4){ destory(node); // initNode(init); // node = init; // break; }else{ printf(" , !
"); continue; } } }else if(option == 3){ destory(node); // initNode(init); // node = init; // break; } else{ printf(" , !
"); } } }else if(option == 3){ // destory(node); return 0; }else { printf(" , !
"); continue; } } return 0; }

좋은 웹페이지 즐겨찾기