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

📄 computerosexp_02_02.cpp

📁 进程管理 用链表实现的 有三种算法实现的
💻 CPP
字号:
// ComputerOSExp_02_02.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "PCB.h"
#include <iostream.h>
#include <iomanip.h>
#include "PCBList.h"
//planPCBList  等待处理的进程
//dealPCBList	已经处理的进程
void DealPCB(PCBList *planPCBList, PCBList *dealPCBList)
{
	int systemAssignTime = 0;
	int len = planPCBList->len;
	if(len == 0 || dealPCBList->len != 0)
		return;
	cout << "请输入时间片的大小(整数)" << endl;
	cin >> systemAssignTime;
	int curTime = planPCBList->GetShortSubmitTime();
	PCBList *readyPCBList = new PCBList();
	planPCBList->PrintTitle();
	PCB *dealPCB = 0;
	int i = 0;
	int sumTurnOverTime = 0;
	int sumPowerTurnOverTime = 0;	
	while(dealPCBList->len != len)
	{
		while(true)
		{
			PCB *readyPCB = planPCBList->GetReadyPCB(curTime, readyPCBList);
			if(readyPCB != 0)
			{
				planPCBList->DeletePCB(readyPCB);//准备进程删除第一个进程
				readyPCBList->InsertPCB(readyPCB);//就绪进程增加一个进程
			}
			else
				break;
		}
		dealPCB = readyPCBList->GetMostPriorityPCB();
		if(dealPCB != 0)
		{
			i++;
			dealPCB->DealTime(curTime, systemAssignTime);
			dealPCB->PrintDetail(i);
			//处理完的进程添加到已处理进程链表中
			if(dealPCB->state == 1)
				dealPCBList->InsertPCB(dealPCB);
			else
				readyPCBList->InsertPCB(dealPCB);
			sumTurnOverTime += dealPCB->turnOverTime;
			sumPowerTurnOverTime += dealPCB->powerTurnOverTime;
			
		}
	}
	cout << "平均周转时间 " << sumTurnOverTime / len << endl;
	cout << "带权平均周转时间 " << sumPowerTurnOverTime / len << endl;
	cout << endl;
	delete readyPCBList;
	cout << endl;
}

int main(int argc, char* argv[])
{
	/*测试数据
	char *sName[4] = {"AA", "BB", "CC", "DD"};
	int sST[4] = {80, 85, 70, 91};
	//int sST[4] = {91, 90, 85, 80};
	//int sRT[4] = {10, 5, 2, 1};
	int sRT[4] = {10, 5, 2, 1};
	int sPT[4] = {2, 1, 1, 3};*/

	/*测试数据*/
	char *sName[12] = {"AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "JJ", "KK", "LL", "MM"};
	int sST[12] = {0, 1, 2, 3, 6, 8, 12, 12, 12, 18, 25, 25};
	int sRT[12] = {6, 4, 10, 5, 1, 2, 5, 10, 4, 3, 15, 8};
	int sPT[12] = {12, 8, 14, 4, 8, 7, 23, 4, 17, 11, 14, 8};

	PCBList *planPCBList = new PCBList();//进程链表的准备的进程
	PCBList *dealPCBList = new PCBList();//进程链表的已处理的进程
	planPCBList->PrintTitle();
	cout << "请输入进程的个数" << endl;
	int len = 0;
	cin >> len;
	cin.get();
	cout << "依次打印输入的进程" << endl;
	for(int i = 0; i < len; i++)
	{
		PCB *pcb = new PCB();
		/*cout << "请输入第" << i + 1 << "个进程的名称" << endl;
		cin.getline(pcb->name, 80, '\n');
		cout << "请输入第" << i + 1 << "个进程提交时间(整数)" << endl;
		cin >> pcb->submitTime;
		cin.get();
		pcb->remainTime = pcb->runTime;
		cout << "请输入第" << i + 1 << "个进程运行时间(整数)" << endl;
		cin >> pcb->runTime;
		cin.get();
		cout << "请输入第" << i + 1 << "个进程优先级(整数)" << endl;
		cin >> pcb->priority;
		cin.get();*/
		/*测试数据*/
		pcb->name = sName[i];
		pcb->submitTime = sST[i];		
		pcb->runTime = sRT[i];
		pcb->remainTime = pcb->runTime;
		pcb->priority = sPT[i];
		planPCBList->InsertPCB(pcb);
		pcb->PrintDetail(i + 1);
	}

	cout << "原始数据" << endl;
	cout << "进程链表按照优先级,提交时间排序" << endl;
	planPCBList->Print();
	//FCFS(planPCBList, dealPCBList);
	//dealPCBList->BackPCBList();
	DealPCB(planPCBList, dealPCBList);
	//RoundRobin(dealPCBList, planPCBList);
	//planPCBList->BackPCBList();
	//MostPriority(planPCBList, dealPCBList);
	delete planPCBList;
	delete dealPCBList;
	return 0;
}

⌨️ 快捷键说明

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