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

📄 fpgl.cpp

📁 模拟操作系统管理内存方法
💻 CPP
字号:
// FPGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#include <iostream>
#include <string>
using namespace std;
struct CHCT;
struct COCT;
struct DCT;
struct SDT;
struct PCB;
void show();

//----------------------------------------------------------------------------
/*             数据结构                 */
 struct PCB
{
	DCT *index_dct;
	PCB *next;
};
 struct SDT
{
	string name;
	DCT    *dct;
	SDT    *prior,*next;
};
 struct CHCT
{
	string name;
	bool attitude;
	COCT *back;
	PCB *prior;
	PCB *next;
};
struct COCT
{
	string name;
	bool attitude;
	CHCT *chct;
	PCB *prior;
	PCB *next;
};
struct DCT
{
    string name;
	int  type;
	bool attitude;
	COCT *coct;
	int  number;
	PCB  *next;
};
SDT *head_sdt;
//--------------------------------------------------------------------------------------------


//-----------------------------------
CHCT *init_chct(string name1)
{
	CHCT *B=new CHCT;
	//B=(CHCT *)malloc(sizeof(CHCT));
	B->name=name1;                   //??????????????????????????
	B->attitude=false;
	B->back=NULL;
	B->prior=NULL;
	B->next=NULL;
	return B;
}
//----------------------------------
COCT *init_coct(string name,CHCT *chct)
{
	COCT *N=new COCT;
	//N=(COCT *)malloc(sizeof(COCT));
	N->name=name;
	N->attitude=false;
	N->chct=chct;
	N->prior=NULL;
	N->next=NULL;
	return N;
}
//------------------------------------------------------------------------------
DCT *init_dct(string name,COCT *coct)
{
	DCT *M=new DCT;
	//M=(DCT *)malloc(sizeof(DCT));
	M->name=name;
	M->attitude=false;
	M->coct=coct;
	M->number=0;
	M->type=-1;                                             //  类型为输入( 0 ),输出( 1 )
	M->next=NULL;
	SDT *r;
	SDT *m=new SDT;
	//m=(SDT *)malloc(sizeof(SDT));
	m->name=M->name;
	m->dct=M;
	r=head_sdt;
	while(r->next!=NULL)
		r=r->next;
	r->next=m;
	m->prior=r;
	m->next=NULL;
	return M;
}
//-------------------------------------------------------------------------------
bool chack_dct(string name)
{
	SDT *rear;
	rear=head_sdt;
	while(rear->next!=NULL)
	{
		rear=rear->next;
		if(rear->name==name)
			return false;
	}
	return true;
}

//----------------------------
bool chack_coct(string name)
{
	SDT *rear;
	rear=head_sdt;
	while(rear->next!=NULL)
	{
		rear=rear->next;
		if(rear->dct->coct->name==name)
			return false;
	}
	return true;
}
//--------------------------------
bool chack_chct(string name)
{
	SDT *rear;
	rear=head_sdt;
	while(rear->next!=NULL)
	{
		rear=rear->next;
		if(rear->dct->coct->chct->name==name)
			return false;
	}
	return true;
}
//-------------------------------------------------------------------------------
CHCT *find_chct(string name)
{
	SDT *rear;
	rear=head_sdt->next;
	while(rear!=NULL)
	{       
		if(rear->dct->coct->chct->name==name)
			return rear->dct->coct->chct;
		rear=rear->next;
	}
	return NULL;
}
//-------------------------------------------------------------------------------
COCT *find_coct(string name)
{
	SDT *rear;
	rear=head_sdt->next;
	while(rear!=NULL)
	{		
		if(rear->dct->coct->name==name)
			return rear->dct->coct;
	    rear=rear->next;
	}
	return NULL;
}
DCT *find_dct(string name)
{
	SDT *rear;
	rear=head_sdt->next;
	while(rear!=NULL)
	{		
		if(rear->dct->name==name)
			return rear->dct;
		rear=rear->next;
	}
	return NULL;
}
//------------------------------------------------------------------------------
void addD()
{
	string name;
	int flag1=0;
	DCT *M;
	SDT *find_node;
	find_node=head_sdt;
	DCT *add_dct;
	printf("输入设备名:");
	//scanf("%c",&name);
	cin>>name;
	getchar();
	if(chack_dct(name)==false)
		printf("error:  你申请的设备已经存在!\n");                  //错误,设备已经存在
	else                                                             //设备不存在时     
	{ 
		M=(DCT *)malloc(sizeof(DCT));
		printf("是否为该设备添加一个驱动器:(y/n)");
		char flag;
		scanf("%c",&flag);
		getchar();
		COCT *add_coct;
		if(flag=='y'||flag=='Y')                        //          加新的驱动器
		{
			while(1)
			{
				printf("输入驱动器的名称:");
				string name1;
				cin>>name1;                                 //???????   从键盘读入字串
				getchar();
				if(chack_coct(name1)==true)
				{
					while(1)
					{
						printf("输入通道的名称:"); 
						string name3;
						cin>>name3;                                  //?????????????   从键盘读字符串
						getchar();
						CHCT *chack;					
						if(chack_chct(name3)==false)
						{
							chack=find_chct(name3);
							add_coct=init_coct(name1,chack);
							printf("添加成功!\n");
							break;
						}
						else
						{
							printf("你输入的通道名字有错!没有此通道。\n");
						}
					}
					break;
				}
				else
				{
					printf("你输入的控制器名字有错!\n");
				}
			}

		}
		else                                             //           不加新的驱动器
		{			
			string name2;
			int i=0;
			do
			{
				if(i!=0)
				     cout<<"没有此驱动器,";
				printf("输入原有的驱动器的名称:");
				cin>>name2;                                 //????  从键盘读入字串	
				getchar();
				i++;
			}while(chack_coct(name2)==true);
			
			add_coct=find_coct(name2);
		}
		add_dct=init_dct(name,add_coct);
	}

}
//------------------------------------------------------------------------------------------------
void deleteD()
{
	string name;
	cout<<"输入要删除设备的名:";
	cin>>name;
	getchar();
	SDT *rear;
	DCT *delete_dct;
	COCT *delete_coct;
	rear=head_sdt->next;
	if(chack_dct(name)==false)                            //   删除的设备存在
	{
		delete_dct=find_dct(name);
		if(delete_dct->attitude==true)
			printf("设备正在使用!不能删除!\n");
		else
		{
			delete_coct=delete_dct->coct;
			while(rear->name!=name)
				rear=rear->next;
			if(rear->next==NULL)
				rear->prior->next=NULL;
			else
			{
				rear->prior->next=rear->next;
				rear->next->prior=rear->prior;
			}
			delete rear;				
			delete delete_dct;                               //  删除设备
			if(chack_coct(delete_coct->name)==true)          //  没有其他的设备使用控制器
				delete delete_coct;                          //  删除控制器 
			printf("成功删除!\n");
		}
	}
    else                                                 //  删除的设备不存在
		printf("删除的设备不存在\n");
		
            
}
//-----------------------------------------------------------------------------------------------
void set_forD()
{
	DCT *set_for_dct;
	PCB *set_for_pcb;                    //
	PCB *rear_set_for;
	printf("你要申请的设备名:");
	string name;
	cin>>name;
	getchar();
	if(chack_dct(name)==true)                            //  申请的设备不存在 
		printf("error: 申请的设备不存在!");
	else
	{
		set_for_dct=find_dct(name);
		set_for_pcb=new PCB; 
		set_for_pcb->index_dct=set_for_dct;
		set_for_pcb->next=NULL;
		if(set_for_dct->attitude==false)             //  申请的设备空闲
		{				
			if(set_for_dct->coct->attitude==false)   //  该设备控制器空闲
			{		
				if(set_for_dct->coct->chct->attitude==false) //   控制器所属的通道空闲时
				{
					set_for_dct->attitude=true;
					set_for_dct->coct->attitude=true;
					set_for_dct->coct->chct->attitude=true;                      
				}
				else                                         //   控制器所属的通道繁忙时
				{
					if(set_for_dct->coct->chct->next==NULL)
						set_for_dct->coct->chct->next=set_for_pcb;
					else
					{
						rear_set_for=set_for_dct->coct->chct->next;
						while(rear_set_for!=NULL)
							rear_set_for=rear_set_for->next;
						rear_set_for=set_for_pcb;
					}
					cout<<"您申请的设备的 ch 忙!ch 空闲时,自动执行您的请求!"<<endl;
					cout<<"ch挂起的状态:"<<set_for_dct->coct->chct->next->index_dct->name<<endl;
				}
			}
			else                                     // 该设备控制器繁忙时
			{
				if(set_for_dct->coct->next==NULL)
					set_for_dct->coct->next=set_for_pcb;
				else
				{
					rear_set_for=set_for_dct->coct->next;
					while(rear_set_for!=NULL)
						rear_set_for=rear_set_for->next;
					rear_set_for=set_for_pcb;
				}
				cout<<"您申请的设备的 co 忙!co 空闲时,自动执行您的请求!"<<endl;
				cout<<"co挂起的状态:"<<set_for_dct->coct->next->index_dct->name<<endl;
			}
		}
		else                                         //  申请的设备忙时
		{
			if(set_for_dct->next==NULL)
				set_for_dct->next=set_for_pcb;
			else
			{
				rear_set_for=set_for_dct->next;
				while(rear_set_for!=NULL)
					rear_set_for=rear_set_for->next;
				rear_set_for=set_for_pcb;
			}
			cout<<"您申请的设备忙!等设备空闲时,自动执行您的请求!"<<endl;
			cout<<"设备挂起的状态:"<<set_for_dct->next->index_dct->name<<endl;

		}
	}
}
//-----------------------------------------------------------------------------------------------
void relaxD()
{
	DCT *relax_dct;
	printf("输入要释放设备名: ");
	string name_relax;
	cin>>name_relax;
	getchar();
	if(chack_dct(name_relax)==true)
		printf("设备不存在!\n");
	else                                                  //    设备存在时
	{
		relax_dct=find_dct(name_relax);
		if(relax_dct->attitude!=NULL)
		{
			if(relax_dct->coct->chct->next==NULL)         //   通道无挂起状态
			{
				if(relax_dct->coct->next==NULL)           //    控制器无挂起状态
				{
					if(relax_dct->next==NULL)             //    设备无挂起状态
					{
						relax_dct->attitude=false;
						relax_dct->coct->attitude=false;
						relax_dct->coct->chct->attitude=false;
					}
					else                                  //     设备有挂起状态
					{
						relax_dct->next=relax_dct->next->next;
					}
				}
				else                                      //      控制器有挂起的状态
				{
					relax_dct->attitude=false;
					relax_dct->coct->next->index_dct->attitude=true;
					relax_dct->coct->next=relax_dct->coct->next->next;
				}					
			}
			else                                         //       通道有挂起状态
			{
				relax_dct->attitude=false;
				relax_dct->coct->attitude=false;
				relax_dct->coct->chct->next->index_dct->attitude=true;
				relax_dct->coct->chct->next->index_dct->coct->attitude=true;
				relax_dct->coct->chct->next=relax_dct->coct->chct->next->next;
			}
	
			
		}
		else                                         //       设备空闲
		{
			printf("设备已经空闲!\n");
		}
	}
}
//-----------------------------------------------------------------------------------------------

void control()
{
	void help();
	string ch=" ";
	help();
	while(ch[0]!='Q'||ch[0]!='q')
	{
		printf("输入命令:");
	    cin>>ch;
		getchar();
		switch(ch[0])
		{
			case 'a':
			case 'A': addD();break;
			case 'd':
			case 'D': deleteD();break;
			case 's':
			case 'S': set_forD();break;
			case 'r':
			case 'R': relaxD();break;
			case 'h':
			case 'H': help();  break;
			default:
				printf("input the command is error!\n");break;
		}
		show();
	}	  
}
//----------------------------------------------------------------------------------------------
void show()
{
	SDT *rear_show;
	printf("\t设备\t\t\t控制器\t\t\t通道\n");
	printf("\t---------------\t\t-------------\t\t---------------\n");
	printf("\tname\tattitude");
	printf("\tname\tattitude");
	printf("\tname\tattitude\n");
	rear_show=head_sdt;
	while(rear_show->next!=NULL)
	{
		rear_show=rear_show->next;
		cout<<"\t"<<rear_show->dct->name<<"\t"<<rear_show->dct->attitude<<"\t\t";
		cout<<rear_show->dct->coct->name<<"\t"<<rear_show->dct->coct->attitude<<"\t\t";
		cout<<rear_show->dct->coct->chct->name<<"\t"<<rear_show->dct->coct->chct->attitude<<endl;
	}
}
//-----------------------------------------------------------------------------------------------
void help()
{
	cout<<"h show help!"<<endl;
	cout<<"a add an aquipment!"<<endl;
	cout<<"d delete an aquipment!"<<endl;
	cout<<"s set for an aquipment!"<<endl;
	cout<<"r release an aquipement!"<<endl;
	cout<<"\t\tcomputer majer"<<endl;
	cout<<"\t  author : xiaosheng"<<endl;
}
//-----------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	system("color 6a"); 
	system("cls");
	head_sdt=new SDT;
	head_sdt->name="";
	head_sdt->dct=NULL;
	head_sdt->prior=NULL;
	head_sdt->next=NULL;
	//head_sdt=(SDT *)malloc(sizeof(SDT));
	CHCT *chct1=init_chct("chct1");
	CHCT *chct2=init_chct("chct2");
	COCT *coct1=init_coct("co1",chct1);
	COCT *coct2=init_coct("co2",chct2);
	COCT *coct3=init_coct("co3",chct2);
	DCT  *dct1=init_dct("Kboard",coct1);
	DCT  *dct2=init_dct("mouse",coct1);
	DCT  *dct3=init_dct("audio",coct2);
	DCT  *dct4=init_dct("printer",coct3);
    show();
	control();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -