⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynmpart.cpp

📁 Memory Management Simulator...........
💻 CPP
字号:

//	DYNAMIC PARTITIONING DEMONSTRATION

#include<stdio.h>
#include<conio.h>

struct PCB{
		int ID;
		int SIZE;
		int PNO;// partition number
		int PSTART;// partition start
		int PEND;// partition end address
	  }processes[1024];


void main(void)
{

clrscr();
int op,flag=0,pno=0,partno=1,DEL_ID,p,j,l;
int part_av[24][3];// partitions available

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(int 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");
		 int SIZE;
		 scanf("%d",&SIZE);
		 /////////////////// check if this size of Partition exists
		 /*int countsize=0;
		 for(i=0;i<1024;i++)
		 {
		  if(RAM[i]==0)// free
		   {
		   countsize++;
		   }
		 }
		 if(countsize<SIZE)//if not available
		 {printf("\nSorry this much size of partition is not available");
		  break;
		 }*/


		 //////////////////// ALLOCATE RAM
		 /////////////////// search free partition

		/* for(int j=0;j<1024;j++)
		  {
		  if(RAM[j]==0)
		  break;
		  //partno++;//go to next partition
		  }*/
		  //////////////// allocate partition
		  //processes[pno].PNO=partno+1;
		  //partitions[partno]=1;// update partitions table
		 ////////////////// allocate RAM



		   int pstart;
		   int pend;
		   int m=0;
		 int counter=0;
		 allocate=0;

		for(i=0;i<1024;i++)
		{
			if(RAM[i]==0)
				{ if(part_av[m][2]==0)
					{part_av[m][0]=i;
					 part_av[m][2]=1;//set start
					 }
				   if(RAM[i+1]>0)//part starts
				   { part_av[m][1]=i+1;
				     m++;}
				 }
		 }
		 if(i==1024)
		 part_av[m][1]=1023;
		 for(int j=0;j<m;j++)
		 {
		 printf("%d\t%d",part_av[j][0],part_av[j][1]);
		 }
		 /////////////////expriment

		 for(int k=0;k<1024;k++)
		 {
		    if(pno>0)
		    {if(k==processes[m].PSTART)//process starts here
			{
			k=processes[m].PEND;// skip process
			m++;//move to next process
			counter=0;//start counting again
			}// end if
		     }
		    counter++;
		    if(counter==SIZE)// if enough space is available
			{
			 allocate=1;
			 break;
			}
		  }// end for
		  ///////////// allcate partition
		  if(allocate==0)
		    {printf("\nPartition available is of %dkb size only",counter);
		    }
		  if(allocate==1)
		  {
		  if(m>0)//not the first process to insert
		  {processes[pno].PNO=(processes[m-1].PNO+1);
		    pstart=processes[m-1].PEND+1;
		    pend=pstart+SIZE;
 //		    if(pend>1024)
 //		    printf("\nSorry the partition available is less than process size");
		   }
		  if(m==0)// first process to insert
		  { processes[pno].PNO=1;
		    pstart=0;
		    pend=(SIZE-1);
		  }

		 if(allocate==1)//allocate RAM
		 {

		   for(int i=pstart;i<=pend;i++)
		   {
		    RAM[i]=ID;
		    }


		 }
		   printf("\n Partion will Start at %d Kb",pstart);
		   processes[pno].PSTART=pstart;
		   printf("\n Partition will end at %d Kb",pend);
		   processes[pno].PEND=pend;
		   //processes[pno].PEND=processes[pno].PSTART+processes[pno].SIZE;
		   /*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;
		   }
		   //processes[pno].PNO=partno;
		   //partno++;
		   */

		  printf("\npartition no for process is %d",processes[pno].PNO);

		 pno++;// increment process number
		 }//end if
		 break;
		 }
		//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("process was in partition number %d",p);
				 printf("\nID found%d,partsize=%d,partno=%d",processes[i].ID,partsize,processes[i].PNO);

				 j=processes[i].PSTART;
				 printf("start at %d",j);
				 l=processes[i].PEND;
				 printf("end at %d\n",l);
				 for(int k=j;k<=l;k++)
				 {
				  RAM[k]=0;
				 }
				 //////////////// shift processes up
				 for(int j=i;j<=pno;j++)
				 {
				 processes[i].ID=processes[i+1].ID;
				 processes[i].SIZE=processes[i+1].SIZE;
				 processes[i].PSTART=processes[i+1].PSTART;
				 processes[i].PEND=processes[i+1].PEND;
				 processes[i].PNO=processes[i+1].PNO;
				 }
				 pno--;// decrease process size

//				 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:
		{
		//////////// count no of partitions

		printf("\nPartition#\tID\tSIZE\tPSTART\tPEND\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",processes[i].PSTART);
			printf("%d\t\n",processes[i].PEND);
//			printf("%d\t\n",processes[i].FRAG);
			}//end for
		printf("\nRAM\nnumbers show allocated area\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 + -