基本分页管理的实现
#include
#include
#include
#include
#include
#define N 100
int process[N][N+1];
int block[N];
int blockCount=0;
int processCount=0;
bool flag =true;
void init();
void output();
bool createProcess();
bool endProcess();
void init(){
int i;
int j;
// 初始化内存状态标志数组
for(i=0;i<N;i++)
block[i]=0;
for(i=0;i<20;i++)
block[rand()%(N-1)]=1;
blockCount=0;
for(i=0;i<N;i++)
if(block[i]==0)
blockCount++;
// 初始化存放进程的数组
for(i=0;i<N;i++){
process[i][0]=0;
for(j=1;j<N;j++)
process[i][j]=-1;
}
processCount=0;
printf("Initialization result are as follows:\n");//初始化结果如下
output();
flag=false;
}
void output(){
printf("\nTotal memory:%d , Used space:%d , Residual space:%d , Total process:%d \n",
N, N-blockCount, blockCount, processCount); //内存总量 已用空间 剩余空间 进程总数
if (flag && blockCount < N) {
printf("Memory block already used(%d):\n", N-blockCount);//已使用的内存块
for (int k=0,count=0; k
if (block[k] == 1)
printf("%2d ", k, ++count);
if (count == 15){
putchar('\n');
count=0;
}
}
putchar('\n');
}
// 输出各进程占用内存详细情况
if (processCount > 0){
printf("Memory details are as follows:\n"); //内存详细使用情况如下
for (int i=0; i
if (process[i][0] > 0)
{
printf("Process ID %d \nMemory block (%2d):", i, process[i][0]); //进程号 占用内存块
for (int j=1,count=0; j<=process[i][0]; j++) {
printf("%2d ", process[i][j], count++);
if (count == 15){
putchar('\n');
printf(" ");
count = 0;
}
}
putchar('\n');
}
}
}
else
printf("There is no process in current memory!\n"); //当前内存无进程!
/*// 输出空闲内存块
if (blockCount > 0) {
printf("空闲内存块(%d):\n", blockCount);
for (int k=0,count=0; k<N; k++)
{ if (block[k] == 0)
printf("%2d ", k, ++count);
if (count == 15)
{ putchar('\n');
count = 0;
}
}
putchar('\n');
}*/
putchar('\n');
}
bool createProcess()
{ int pid, pages, k = 0;
loop:printf("Enter the process ID(<%d):and number of pages required", N); //请输入进程号和所需页面数
scanf("%d%d", &pid, &pages);
if (pid > 99) {
printf("Error! Process ID exceeded!\n"); //错误!进程号过大!
goto loop; }
if (pages > blockCount)
return false;
blockCount -= pages;
process[pid][0] = pages;
for (int i=1; i<=pages; i++)
{ while (block[k]==1 && k<100)
k++;
process[pid][i] = k;
block[k] = 1; k++; }
processCount++;
return true;
}
bool endProcess()
{ int pid, pages;
if (processCount < 1)
{
printf("\nThere is no process in current memory\n");//当前内存没有进程!
return false;
}
printf("There are %d processes in current memory,Process ID are: ", processCount); //当前内存中的进程有 %d 个,进程号为:
for (int i=0; i<N; i++)
if (process[i][0] > 0)
printf("%2d ", i);
putchar('\n');
printf("Enter the process you will end (<%d):", N);//请输入您要结束的进程号:
scanf("%d", &pid); pages = process[pid][0];
if (pages == 0)
{ printf("Sorry! The process does not exist!\n"); //对不起!该进程不存在!
return false;
}
for (int j=1; j<pages; j++)
{ block[process[pid][j]] = 0;
process[pid][j] = -1;
}
process[pid][0] = 0;
processCount--;
blockCount += pages;
return true;}
void menu()
{
int choice;
while (true) {
printf("Action menu :\n"); //操作菜单
printf(" 1 -->Create process\n 2 --> End process\n 3 -->See memory\n 0 -->Exit program\n");
printf("Enter the operation you want to do:");
scanf("%d", &choice);
switch (choice) {
case 1:
if (createProcess())
printf("Create new process successfully!\n\n"); //创建新进程成功
else
printf("sORRY!Insufficient memory space to create a failure!\n\n"); //抱歉!内存空间不足,创建新进程失败
break;
case 2:
if (endProcess())
printf("Process is end!\n\n"); //进程已结束
else
printf("Ending process fail!\n\n");//进程结束失败
break;
case 3:
output();
break;
case 0:
return;
printf("Sorry ,your choice is incorrect!Please reselect\n\n");
}
}
}
int main(){
init();
menu();
return 0;
}