操作系统-c语言实现空闲块表的存储空间的分配和回收

    xiaoxiao2021-12-14  20

    #include<iostream.h> #include<stdlib.h> #include<time.h> #include<stdio.h> void init(int map[20][20],int row,int col)//初始化函数 { int i,j,temp; temp = time(NULL); srand(temp); for(i = 0; i<row; i++) { for(j=0; j<col; j++) { map[i][j] = rand()%2; cout<<map[i][j]<<" "; } cout<<endl; } } void fenpei(int map[20][20],int row,int col)//分配函数 { int n,count=0,i,j,b; cout<<"请输入需要的盘块数:"<<endl; cin>>n; for( i =0; i<row; i++) for( j=0; j<col; j++) { if(count<n) { if ( map[i][j] == 0 ) { map[i][j]=1; b = (i)* col +j; cout<<"第"<<++b<<"盘块被分配!"<<endl; count++; } } else break; } cout<<"分配后的信息:"<<endl; for(i=0;i<row;i++) { for(j=0;j<col;j++) cout<<map[i][j]<<" "; cout<<endl; } } void huishou(int map[20][20],int row,int col) { int n,count=0,i,j; cout<<"请输入需要回收的盘块号:"<<endl; cin>>n; if( n>=row*col ) cout<<"你输入的盘块号不存在!"<<endl; else { i = n / col; j = n % col; if(map[i][j] ==0) cout<<"你输入的盘块号已空闲!"<<endl; else map[i][j]=0; } cout<<"分配后的信息:"<<endl; for(i=0;i<row;i++) { for(j=0;j<col;j++) cout<<map[i][j]<<" "; cout<<endl; } } void main() { int row,col,type; int map[20][20]; cout<<"请输入位示图的行和列:不能超过20!"<<endl; cin>>row>>col; init(map,row,col); while(1) { cout<<"请输入操作类型,1:分配,2:回收,3:退出!"<<endl; cin>>type; if(type==1) fenpei(map,row,col); else if(type == 2) huishou(map,row,col); else return; } }

    转载请注明原文地址: https://ju.6miu.com/read-970970.html

    最新回复(0)