Fixed partition storage management (C language)

Fixed partition storage management (C language) (including code)

course

Operating system
code download:
https://download.csdn.net/download/qq_48499842/87579330

experiment name

Fixed partition storage management

Experimental requirements

1. Realize the allocation and recovery of storage space under the fixed partition storage management mode.
2. The known current memory allocation table is as follows

partition number initial address length state
1 10KB 30KB Job5
2 40KB 7KB 0
3 47KB 50KB Job2

3. Several jobs apply for or release memory space, and the requests are as follows:
(1) Job6 requests resources, and applies for 20KB of memory space; (
2) Job Job7 requests resources, and applies for 5KB of memory space;
(3) Job Job2 After execution, free up space.
4. Write a program to realize the allocation and recovery of the corresponding storage space. If the request is successful, modify the main allocation table and output the table. If the request cannot be satisfied, output "allocation failure".

Purpose

By writing a simulation program for fixed partition storage management, deepen the understanding of the corresponding knowledge of fixed partition management methods and main memory allocation tables in the storage management function of the operating system.

source code

#include "stdio.h"
int begin[]={
    
    10,40,47};	//起始地址 
int len[]={
    
    30,7,50};	//长度 
int t[]={
    
    5,0,2};	//状态 
int n,m,k; // 进程号、完成进程号、申请的资源空间大小 
char s,c; //用于接受判断是否继续时输入的字符变量 

//申请资源 
void p()
{
    
    
	int flag=0;		//标记1 
	int flag2=0;	//标记2 
	while(flag==0) 
	{
    
     
		int sum=0;	//累加器 
		for(int i=0;i<3;i++)
		{
    
    
			if(t[i]!=0)		
				sum=sum+1;
		} 
		if(sum==3)	//空间满了,跳出循环,进入释放 
		{
    
    
			printf("已经满了!不能再添加进程了!\n");
			break;
		} 
		printf("请输入需要申请资源的进程号:"); 
		scanf("%d",&n);
		while(flag2==0) 
		{
    
    
			int sum=0;	//累加器 
			for(int i=0;i<3;i++)
			{
    
    
				sum=sum+1;
				if(n==t[i])		//判断进程是否存在 
				{
    
    
					printf("该进程%d已存在!\n",n);
					printf("请重新输入需要申请资源的进程号:"); 
					scanf("%d",&n);
					sum=0; //归零,重新计算 
				}
			}
			if(sum==3)	break;//没有重复的进程存在 
		} 
		printf("请输入需要申请资源的内存空间:"); 
		scanf("%d",&k);
		for(int i=0;i<3;i++)
		{
    
    
			if(t[i]==0)	//状态为0 
			{
    
    
				if(k<=len[i])	//申请的资源小于等于分配的空间大小 
		 		{
    
    
				 	t[i]=n; 
		 			printf("申请成功!\n");
   					//申请成功打印新的分配表 
   					printf("分区号  起始地址   	长度  	状态\n");
   					for(int i=0;i<3;i++)
   					{
    
    
       					if(t[i]==0) //输出格式 
				   		{
    
    
					       	printf("%d 	   %d KB	%d KB	 %d \n",i+1,begin[i],len[i],t[i]);
		       			}else
					   	{
    
    
			       			printf("%d 	   %d KB	%d KB	job%d \n",i+1,begin[i],len[i],t[i]);
			       		}
   					}
		 		} 
		 		else	//申请的资源大于分配的空间大小
				{
    
    
					printf("分配失败!\n");  
				}
			} 
			 
		}
		printf("是否继续申请资源(y/n):"); 
		scanf("%s",&s);
		if(s=='n')
		{
    
    
			flag=1;
		}
	} 	 
}
 
//释放 
void v()
{
    
    
	int flag=0;		
	while(flag==0)
	{
    
    	
		printf("请输入需要执行完成的进程号:");
		scanf("%d",&m);		
		int sum=0;
		int flag2=0;
		while(flag2==0) 
		{
    
    
			for(int i=0;i<3;i++)
			{
    
    
				if(t[i]!=m)	
				{
    
    
					sum=sum+1;
				}							
			}
			if(sum==3)
			{
    
    
				sum=0;
				printf("进程不存在!\n"); 
				printf("请重新输入需要执行完成的进程号:");
				scanf("%d",&m);				
			}else
			{
    
    
				flag2=1;				
			}
		}
		for(int i=0;i<3;i++)
		{
    
    
			if(m==t[i])  //进程存在则释放 
			{
    
    
				t[i]=0;
				//释放成功打印新的分配表 
				printf("释放成功!\n");
				printf("分区号  起始地址   	长度  	状态\n");
				for(int i=0;i<3;i++)
				{
    
    
					if(t[i]==0) //输出格式 
			   		{
    
    
				       	printf("%d 	   %d KB	%d KB	 %d \n",i+1,begin[i],len[i],t[i]);
	       			}else
				   	{
    
    
		       			printf("%d 	   %d KB	%d KB	job%d \n",i+1,begin[i],len[i],t[i]);
		       		}
		   		}
	   			printf("是否继续需要执行完成的进程号(y/n):"); 
				scanf("%s",&s);
				if(s=='n')
				{
    
    
					flag=1;
				} 
			}
		}
	
	}
} 

Main program (test01):

int main()
{
    
    
	//分配表 
	printf("已知当前内存分配表如下\n");
	printf("分区号  起始地址   	长度  	状态\n");
	for(int i=0;i<3;i++)
   	{
    
    
       	if(t[i]==0) //输出格式 
   		{
    
    
	       	printf("%d 	   %d KB	%d KB	 %d \n",i+1,begin[i],len[i],t[i]);
		}else
	   	{
    
    
   			printf("%d 	   %d KB	%d KB	job%d \n",i+1,begin[i],len[i],t[i]);
   		}
   	}
	p();
	v();	 	
}

flow chart

Overall model diagram
insert image description here

P() module flow chart:
insert image description here

V() module flow chart :
insert image description here

Experimental results

insert image description here

Experiment summary

Through this experiment, I have deepened my understanding of the corresponding knowledge such as the fixed partition management method and the main memory allocation table in the storage management function of the operating system, and have a preliminary understanding of how the operating system implements storage management. The advantage of the fixed partition is that it is easy to implement. , requires very little operating system overhead, and it also has many disadvantages, such as partial fragmentation and insufficient use of memory. The maximum number of active processes is also fixed.

teacher comments

Guess you like

Origin blog.csdn.net/qq_48499842/article/details/124449198