我正在C
的操作系统Memory Management Paging Scheme
模拟工作,所以这里是我到目前为止所做的:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int alloc[50], base[50], frame[50], job[50];
int i, n, pa, fs, ps, nf, temp;
clrscr();
printf("ntt PAGINGn");
printf("nt Enter the physical address space:");
scanf("%d",&pa);
printf("nt Enter the page size:");
scanf("%d",&ps);
nf=pa/ps;
printf("nt Number of frames = %d",nf);
for(i=0;i<nf;i++)
{
alloc[i]=0;
printf("Enter job number %d",i+1);
scanf("%d",job[i]);
if ( // If job can fit ) {
// Here job will fit one by one
temp=rand()%nf;
while( alloc[temp] == 1 )
temp=rand()%nf;
alloc[temp]=1;
frame[i]=temp;
// The main algo will come here
base[i]=frame[i]*ps;
printf("n %dtt %dtt %dtt",i,frame[i],base[i]);
} else {
//If the job can not fit in the memory
printf("Job %d Can't fit in the Memory.n",i+1);
break;
}
}
getch();
}
我只是想实现下面的程序,我需要的;
1。首先,当我根据页码输入物理地址和页面大小时,我可以输入我的工作
2。秒每个作业可根据作业大小容纳页数
3。
每次我进入作业时,Memory Block Table (MBT)
应该重新加载并告诉有多少内存可用或占用
4。最后,如果没有足够的空间来放置更大的作业,它会给出错误
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int ps,np,nf,log;
int alloc[50],base[50],frame[50],page[50];
int i,f,n,pa,fs,pno,add,offset;
int temp;
int f1;
clrscr();
printf("ntt PAGINGn");
printf("nt Enter the logical address space:");
scanf("%d",&log);
printf("nt Enter the page size:");
scanf("%d",&ps);
printf("nt Enter the physical address space:");
scanf("%d",&pa);
fs=ps;
np=log/ps;
nf=pa/fs;
printf("nt Number of pages = %d",np);
printf("nt Number of frames = %d",nf);
for(i=0;i<nf;i++)
alloc[i]=0;
for(i=0;i<np;i++)
{
temp=rand()%nf;
while(alloc[temp]==1)
temp=rand()%nf;
alloc[temp]=1;
frame[i]=temp;
}
printf("n Page No t Frame No t Base address ");
for(i=0;i<np;i++)
{
base[i]=frame[i]*ps;
page[i]=i;
printf("n%dtt %dt %dtt",i,frame[i],base[i]);
}
getch();
}
The Solution is
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int mbt[50];
int i, j, k, pa, bs, bfree, bfree_mode, ps, js, var = 0, var2=0, jn, job;
clrscr();
printf("ntt PAGINGn");
printf("nt Enter the physical address space:");
scanf("%d",&pa);
printf("nt Enter the Block size:");
scanf("%d",&bs);
bfree = pa / bs;
printf("nt the number of blocks are = %dn",bfree);
bfree_mode = bfree;
for( i = 0 ; i < bfree ; i++ ) {
mbt[i] = 0;
}
printf("How many jobs do you want to enter to the memory ?n");
scanf("%d",&jn);
for ( j = 0 ; j <= jn+2 ; j++ ) {
printf("Enter %d Job size :n",j+1);
scanf("%d",&job);
k = var;
var = job / bs ;
if (job % bs !=0)
var +=1;
if ( var <= bfree ) {
var2 += var;
for ( k ; k < var2 ; k++ ) {
mbt[k] = j+1;
}
bfree -= var;
printf("mbt[0] = OS Reserved!n");
for ( i = 1 ; i < bfree_mode ; i++)
printf("mbt[%d] = %dn",i,mbt[i]);
}else if ( var > bfree ) {
printf("nThe Memory is not enough for the %d Job",j+1);
break;
}
}
getch();
}