q->size = q->size + s->size + p->size; }else{
if(s->sPosition + s->size == p->sPosition){ //only bottom is free space; s->size = s->size + p->size;
if((tmp = (free_block * )malloc(sizeof(free_block))) == NULL){ printf(\ }
tmp->size = s->size;
tmp->sPosition = s->sPosition; tmp->next = p->next; q->next = tmp; free(p); }else{
if(q->sPosition + q->size == s->sPosition){ //only top is free space q->size = q->size + s->size;
}else{ //neither bottom or top is free space
if((tmp = (free_block * )malloc(sizeof(free_block))) == NULL){ printf(\ }
tmp->size = s->size;
tmp->sPosition = s->sPosition; tmp->next = p; q->next = tmp; } } }
}else{ //no bottom free space;
if(q->sPosition + q->size == s->sPosition){ //only top is free space q->size = q->size + s->size;
}else{ //neither bottom or top is free space
21
if((tmp = (free_block * )malloc(sizeof(free_block))) == NULL){ printf(\ }
tmp->size = s->size;
tmp->sPosition = s->sPosition; tmp->next = NULL; q->next = tmp; } } }
//释放空间函数 void cfree() {
int pid;
process * p_p,*p_q;
p_p = p_head->next; printf(\
printf(\ scanf(\
//find the process of the specify pid while(p_p->pid != pid && p_p != NULL){
p_q = p_p; //p_p stand for the process ;p_q stand for pre process p_p = p_p->next; }
if(p_p == NULL){
printf(\ }else{
cfree2(p_p); if(p_p == NULL){
22
p_q->next = NULL; }else{
p_q->next = p_p->next; } }
//free space showFreeChain(); showProcessChain(); } //主页面 void prtUsage() {
printf(\\
printf(\ printf(\ printf(\ printf(\
printf(\} int main() { init(); int a; while(1){ prtUsage();
printf(\/2/0 to select\\n\ scanf(\ if(a > 2 || a < 0){
23
printf(\ continue; } switch(a){
case 1:cmalloc();break; case 2:cfree();break; case 0:goto end; default:
printf(\ break; } } end: getchar();
printf(\}
主要功能模块:
void init() 初始化
void showFreeChain() 显示空闲内存信息 void showProcessChain()显示进程信息 void cmalloc()申请空间函数 void cfree()释放空间函数 void prtUsage()主页面
24
运行结果:
主界面
申请内存
释放内存
25