实验四
#include <stdio.h> #include <conio.h> #include <string.h> #define MAX 512 struct partition { char pn[10]; int begin; int size; int end; char status; }; struct partition part[MAX]; int p = 0; /*上次扫描结束处 */ void Init() { int i; for ( i = 0; i < MAX; i++ ) part[i].status = '-'; strcpy( part[0].pn, "SYSTEM" ); part[0].begin = 0; part[0].size = 100; part[0].status = 'u'; strcpy( part[1].pn, "-----" ); part[1].begin = 100; part[1].size = 412; part[1].status = 'f'; for ( i = 0; i < MAX; i++ ) part[i].end = part[i].begin + part[i].size; } void Output( int i ) /* 输出行 */ { printf( "\t%s", part[i].pn ); printf( "\t%d", part[i].begin ); printf( "\t%d", part[i].size ); printf( "\t%d", part[i].end ); printf( "\t%c", part[i].status ); } void ShowData() /* 显示分区 */ { int i; int n; printf( "\n================================================================" ); printf( "\n空闲分区表Free:" ); printf( "\n\tNo.\tproname\tbegin\tsize\tend\tstatus" ); printf( "\n\t------------------------------------------------" ); n = 1; for ( i = 0; i < MAX; i++ ) { if ( part[i].status == '-' ) break; if ( part[i].status == 'f' ) { printf( "\n\tNo.%d", n ); Output( i ); n++; } } printf( "\n" ); printf( "\n================================================================" ); printf( "\n已分配分区表Used:" ); printf( "\n\tNo.\tproname\tbegin\tsize\tend\tstatus" ); printf( "\n\t------------------------------------------------" ); n = 1; for ( i = 0; i < MAX; i++ ) { if ( part[i].status == '-' ) break; if ( part[i].status == 'u' ) { printf( "\n\tNo.%d", n ); Output( i ); n++; } } printf( "\n" ); printf( "\n" ); printf( "\n================================================================" ); printf( "\n内存使用情况,按起始址增长的排:" ); printf( "\nprintf sorted by address:" ); printf( "\n\tNo.\tproname\tbegin\tsize\tend\tstatus" ); printf( "\n\t------------------------------------------------" ); n = 1; for ( i = 0; i < MAX; i++ ) { if ( part[i].status == '-' ) break; printf( "\n\tNo.%d", n ); Output( i ); n++; } getch(); } void Fit( int a, char workName[], int workSize ) /* 分配成'u'+'f' */ { int i; for ( i = MAX; i > a + 1; i-- ) { if ( part[i - 1].status == '-' ) continue; strcpy( part[i].pn, part[i - 1].pn ); part[i].begin = part[i - 1].begin; part[i].size = part[i - 1].size; part[i].end = part[i - 1].end; part[i].status = part[i - 1].status; } strcpy( part[a + 1].pn, "-----" ); part[a + 1].begin = part[a].begin + workSize; part[a + 1].size = part[a].size - workSize; part[a + 1].end = part[a].end; part[a + 1].status = 'f'; strcpy( part[a].pn, workName ); part[a].size = workSize; part[a].end = part[a].begin + part[a].size; part[a].status = 'u'; } void Allocation() /* 分配 */ { int i; int a; int workSize; char workName[10]; int pFree; printf( "\n请输入作业名称:" ); while(1) { scanf( "%s", &workName ); for(i=0;i<MAX;i++) { if(part[i].status=='u' && strcmp(workName,part[i].pn)==0) { printf("该作业已存在,请重新输入作业名:"); break; } } if(i==MAX) break; } printf( "请输入作业大小(k):" ); while(1) { scanf( "%d", &workSize ); for ( i = 0; i < MAX; i++ ) { if ( part[i].status == 'f' && part[i].size >= workSize ) { pFree = i; break; } } if ( i == MAX ) { printf( "\n该作业大小超出最大可分配空间,请重新输入:" ); } else break; } printf( "\n请选择分配算法:" ); printf( "\n1、最先适应(first fit)" ); printf( "\n2、下次适应(next fit)" ); printf( "\n3、最优适应(best fit)" ); printf( "\n4、最坏适应(worst fit)" ); printf( "\n请输入选项:" ); while ( 1 ) { scanf( "%d", &a ); if ( a == 1 || a == 2 || a == 3 || a == 4 ) break; else printf( "输入错误,请重新输入:" ); } switch ( a ) { case 1: for ( i = 0; i < MAX; i++ ) if ( part[i].status == 'f' && part[i].size >= workSize ) break; Fit( i, workName, workSize ); break; case 2: for ( p; p < MAX; p++ ) { if ( p == MAX ) p = 0; if ( part[p].status == 'f' && part[p].size >= workSize ) break; } Fit( p, workName, workSize ); break; case 3: for ( i = 0; i < MAX; i++ ) if ( part[i].status == 'f' && part[i].size >= workSize ) if ( part[pFree].size > part[i].size ) pFree = i; Fit( pFree, workName, workSize ); break; case 4: for ( i = 0; i < MAX; i++ ) if ( part[i].status == 'f' && part[i].size >= workSize ) if ( part[pFree].size < part[i].size ) pFree = i; Fit( pFree, workName, workSize ); break; default: break; } printf( "\n分配成功!" ); getch(); } void Merge() /* 合并连续的'f' */ { int i = 0; while ( i != MAX - 1 ) { for ( i = 0; i < MAX - 1; i++ ) { if ( part[i].status == 'f' ) if ( part[i + 1].status == 'f' ) { part[i].size = part[i].size + part[i + 1].size; part[i].end = part[i].begin + part[i].size; i++; for ( i; i < MAX - 1; i++ ) { if ( part[i + 1].status == '-' ) { part[i].status = '-'; break; } strcpy( part[i].pn, part[i + 1].pn ); part[i].begin = part[i + 1].begin; part[i].size = part[i + 1].size; part[i].end = part[i + 1].end; part[i].status = part[i + 1].status; } part[MAX - 1].status = '-'; break; } } } } void Recovery() /* 回收分区 */ { int i; char workName[10]; printf( "\n请输入回收的分区名称:" ); scanf( "%s", &workName ); if ( strcmp( workName, "SYSTEM" ) == 0 ) { printf( "\n该系统分区无法回收" ); return; } for ( i = 0; i < MAX; i++ ) { if ( strcmp( workName, part[i].pn ) == 0 ) { strcpy( part[i].pn, "-----" ); part[i].status = 'f'; Merge(); printf( "\n回收成功!" ); getch(); return; } } if ( i == MAX ) { printf( "\n找不到该分区" ); return; } } void main() { int a; Init(); printf( "\n初始化,设内存容量%dk", MAX ); printf( "\n系统从低地址部分开始使用,占用%dk", part[0].size ); printf( "\n" ); while ( 1 ) { printf( "\n" ); printf( "\n1、显示分区" ); printf( "\n2、分配作业" ); printf( "\n3、回收分区" ); printf( "\n请输入选项:" ); while ( 1 ) { scanf( "%d", &a ); if ( a == 1 || a == 2 || a == 3 ) break; printf( "输入错误,请重新输入:" ); } switch ( a ) { case 1: ShowData(); break; case 2: Allocation(); break; case 3: Recovery(); break; default: break; } } }