📄 staticpart.cpp
字号:
// FIXED PARTITIONING DEMONSTRATION
//
#include<stdio.h>
#include<conio.h>
struct PCB{
int ID;
int SIZE;
int PNO;// partition number
int FRAG;// fragmentation
}processes[1024];
void main(void)
{
clrscr();
int op,flag=0,pno=0,partno,DEL_ID,p,j,l;
long partsize;
int RAM[1024]={0};
printf("\nEnter number of Partitions");
scanf("%d",&partno);
///////////////////////// allocate partitions
partsize=(1024/partno);
int allocate,id_dup,ID;
printf("\nSize of Each Fixed partition is :%d kb",partsize);
while(1)
{
printf("\n1.Create a process\n2.Delete a Process\n3.Display Status\n4.Exit\n");
scanf("%d",&op);
switch(op)
{
case 1:
{
//////////////////// check if any partition is available
allocate=0;
for(int i=0;i<1024;i++)
{
if(RAM[i]==0)// if available
{ allocate=1;
break;
}
}
///////////////// RAM is FULL
if(allocate==0)
{
printf("\nSorry no more processes can be accomodated");
break;
}
else
printf("\nEnter Process ID");
scanf("%d",&ID);
/////////////// CHECK IF ID ALREADY EXISTS /////////
id_dup=0;// id duplicate
for(i=0;i<=pno;i++)
{ if(processes[i].ID==ID)
{printf("\nSorry this ID already exists");
id_dup=1;
break;}
}
if(id_dup==1)// duplicate id
break;
else
processes[pno].ID=ID;// assign ID to process
printf("\nEnter Process Size in kb");
scanf("%d",&processes[pno].SIZE);
if(processes[pno].SIZE<=partsize)
{
processes[pno].FRAG=partsize-processes[pno].SIZE;
//////////////////// ALLOCATE RAM
/////////////////// search free partition
partno=0;
for(int j=0;j<1024;j+=partsize)
{
if(RAM[j]==0)
break;
partno++;//go to next partition
}
//////////////// allocate partition
processes[pno].PNO=partno+1;
//partitions[partno]=1;// update partitions table
printf("\npartition no for process is %d",processes[pno].PNO);
// allocate RAM
int r=((processes[pno].PNO*partsize)-partsize);
printf("\n Partion will Start at %d Kb",r);
printf("\n Partition will end at %d Kb",(processes[pno].PNO*partsize));
int frag=r+processes[pno].SIZE;
for(j=r;j<processes[pno].PNO*partsize;j++)
{
if(j>frag)
RAM[j]=9;
else
RAM[j]=1;
}
pno++;
}
else
printf("\nSorry the size of the process is greater");
break;
}
case 2://///////////////////////////// del a process
{
printf("\nEnter Process ID to delete: ");
scanf("%d",&DEL_ID);
for(int i=0;i<=pno;i++)
{
if(processes[i].ID==DEL_ID)// id found
{//update RAM
p=processes[i].PNO;
printf("%d",p);
printf("\nID found%d,partsize=%d,partno=%d",processes[i].ID,partsize,processes[i].PNO);
j=((p*partsize)-partsize);
printf("start at %d",j);
l=j+partsize;
printf("end at %\d",l);
for(int k=j;k<l;k++)
{
RAM[k]=0;
}
processes[i].ID=0;
processes[i].SIZE=0;
processes[i].FRAG=0;
// partitions[i]=0;
for(i=0;i<1024;i++)
{
printf("%d",RAM[i]);
}
getch();
break;
}//end if
}// end for
break;
}
case 3:
{
printf("\nPartition#\tID\tSIZE\tFRAG\n");
for(int i=0;i<=pno;i++)
{
printf("%d\t\t",processes[i].PNO);
printf("%d\t",processes[i].ID);
printf("%d\t",processes[i].SIZE);
printf("%d\t\n",processes[i].FRAG);
}//end for
printf("\nRAM\n1's show allocated area\n'9'shows fragments\n0 shows free RAM\n");
for(i=0;i<1024;i++)
{
printf("%d",RAM[i]);
}
getch();
break;
}
case 4:
{
flag=1;
break;
}
default:
{
printf("\nEnter from 1-4");
break;
}
}// end switch
if(flag==1)
break;
}
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -