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

📄 schedlemode.cpp

📁 这是实现操作系统交通灯调度问题
💻 CPP
字号:
// SchedleMode.cpp: implementation of the SchedleMode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Scheduling.h"
#include "SchedleMode.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SchedleMode::SchedleMode()
{
    pLinkHead = new Process;
	pLinkTail = new Process;
	pLinkHead->next = pLinkTail;
    pLinkTail->last = pLinkHead;
	pLinkTail->next = pLinkHead;
    pLinkHead->last = pLinkTail;
	pCurrent = pLinkHead;
	iProcessNum = 0;
}

SchedleMode::~SchedleMode()
{
    Process *pTemp;
	while(pLinkHead != pLinkTail){
		pTemp = pLinkHead;
		pTemp = pTemp->next;
		delete pLinkHead;
		pLinkHead = pTemp;
	}
	delete pLinkTail;
		
}
void SchedleMode::AddToLinkProcess(Process *pAddProcess){

   //construct the link of Processes
   Process *pTemp = pCurrent->next;
   pCurrent->next = pAddProcess;
   pAddProcess->last = pCurrent;
   pAddProcess->next = pTemp;
   pTemp->last = pAddProcess;
   pCurrent = pAddProcess;
   iProcessNum++;
}


void SchedleMode::FCFSMode(CString &stShowScreen){
	//sort the Processes by the arrive-time
	Process *pCmp1, *pCmp2;
	Process *pTemp1, *pTemp2;	
    for( int i = 0 ;i<iProcessNum ;i++ ){
		pCmp1 = pLinkHead->next;
		for( int j = 0 ;j<iProcessNum-i ;j++ ){
			 pCmp2  = pCmp1->next;
			if( pCmp2 != pLinkTail && pCmp1->arrive_time >= pCmp2->arrive_time ){
				
                 pTemp1 = pCmp1->last;
				 pTemp2 = pCmp2->next;
				 pTemp1->next = pCmp2;
				 pCmp2->last = pTemp1;
                 pCmp2->next = pCmp1;
				 pCmp1->last = pCmp2;
				 pCmp1->next = pTemp2;
				 pTemp2->last = pCmp1;
			}
			else
				pCmp1 = pCmp2;
		}
	}
	pCurrent = pLinkTail->last;
   //sort ended

	CString stProcessName = "          |Process Name:   ";
	CString stArriveTime  = "  Process |Arrive Time:    ";
	CString stRunTime     = "          |Run Time:       ";
	CString stFlag        = "*******************************************************************";
	CString stEndTime	  = "          |End Time:       ";
	CString stRoundTime	  = "          |Round Time:     ";
	CString stWeightTime  = "   FCFS   |Weight Time:    ";
	CString stAverageRound= "          |Average Round:  ";
	CString stAverageWeight="          |Average Weight: ";
	CString str;

	float		sum_round_time = 0.0;
	float		sum_weight_time = 0.0;

	pTemp1 = pLinkHead->next;
	while(pTemp1 != pLinkTail){
		stProcessName += pTemp1->process_name+"    ";
		str.Format("%g",pTemp1->arrive_time);
		stArriveTime += str+"    ";
		str.Format("%g",pTemp1->run_time);
		stRunTime += str+"    ";

		//Get the end-time
        if(pTemp1->last != pLinkHead){
			if((pTemp1->arrive_time) > (pTemp1->last->end_time)){
				pTemp1->end_time = pTemp1->arrive_time + pTemp1->run_time;
			}
			else 
			pTemp1->end_time = pTemp1->last->end_time + pTemp1->run_time;
		}
		else{
            pTemp1->end_time = pTemp1->run_time + pTemp1->arrive_time;
		}
		

        str.Format("%g",pTemp1->end_time);
		stEndTime += str+"    ";

       //Get the round-time
		pTemp1->around_time = pTemp1->end_time - pTemp1->arrive_time;
        str.Format("%g",pTemp1->around_time);
		stRoundTime += str+"    ";
        
	   //Get the weight-time
		pTemp1->weight_time = pTemp1->around_time / pTemp1->run_time;
        str.Format("%g",pTemp1->weight_time);
		stWeightTime += str+"    ";

		//the sum of round-time
        sum_round_time += pTemp1->around_time;
		//the sum of weight-time
        sum_weight_time += pTemp1->weight_time;
		pTemp1 = pTemp1->next;
	}

    str.Format("%g",sum_weight_time/iProcessNum);
	stAverageWeight += "           " + str +"               ";

	str.Format("%g",sum_round_time/iProcessNum);
	stAverageRound  += "           " + str +"               ";

    stShowScreen += stProcessName + "\r\n";
	stShowScreen += stArriveTime  + "\r\n";
	stShowScreen += stRunTime + "\r\n";
	stShowScreen += stFlag + "\r\n";
	stShowScreen += stEndTime + "\r\n";
	stShowScreen += stRoundTime + "\r\n";
	stShowScreen += stWeightTime + "\r\n";
    stShowScreen += stAverageRound + "\r\n";
	stShowScreen += stAverageWeight + "\r\n";
	stShowScreen += stFlag + "\r\n";
}

void SchedleMode::SJFMode(CString &stShowScreen){

	CString stEndTime	  = "          |End Time:       ";
	CString stRoundTime	  = "          |Round Time:     ";
	CString stWeightTime  = "    SJF   |Weight Time:    ";
	CString stAverageRound= "          |Average Round:  ";
	CString stAverageWeight="          |Average Weight: ";
	CString stFlag        = "*******************************************************************";
	CString str;

	//the following operations base on the sorting by FCFS
	float       min_run_time = 10000;
	float		sum_round_time = 0.0;
	float		sum_weight_time = 0.0;

	Process *pTemp1 ,*pTemp2 ,*pLastProcess;
	pLastProcess = pLinkHead->next;
    
    str.Format("%g",pLastProcess->end_time);
		stEndTime += str+"    ";
	str.Format("%g",pLastProcess->around_time);
		stRoundTime += str+"    ";
	str.Format("%g",pLastProcess->weight_time);
		stWeightTime += str+"    ";
    pLastProcess->flags = true;

//	pTemp1 = pLastProcess->next;
	for(int i=1 ;i<iProcessNum ;i++){
		pTemp1 = pLinkHead->next->next;
		while( pLastProcess->end_time >= pTemp1->arrive_time && pTemp1 != pLinkTail ){
			if( pTemp1->run_time < min_run_time && pTemp1->flags == false ){
			min_run_time = pTemp1->run_time;
			pTemp2 = pTemp1;
			}
			pTemp1 = pTemp1->next;
		}
		if(pLastProcess->end_time < pTemp1->arrive_time && pTemp2 == NULL ){
			pTemp2 = pTemp1;
            pTemp2->end_time = pTemp2->arrive_time + pTemp2->run_time;
		}
		else
		pTemp2->end_time = pLastProcess->end_time + pTemp2->run_time;
	
		str.Format("%g",pTemp2->end_time);
		stEndTime += str+"    ";

		pTemp2->around_time = pTemp2->end_time - pTemp2->arrive_time;
		str.Format("%g",pTemp2->around_time);
		stRoundTime += str+"    ";

		pTemp2->weight_time = pTemp2->around_time / pTemp2->run_time;
		str.Format("%g",pTemp2->weight_time);
		stWeightTime += str+"    ";

		pTemp2->flags = true;
		min_run_time = 10000;

		sum_round_time += pTemp2->around_time;
		sum_weight_time += pTemp2->weight_time;
   
		pLastProcess = pTemp2;
		pTemp2 = NULL;
	}

    str.Format("%g",sum_weight_time/iProcessNum);
	stAverageWeight += "           " + str +"               ";

	str.Format("%g",sum_round_time/iProcessNum);
	stAverageRound  += "           " + str +"               ";

    stShowScreen += stEndTime + "\r\n";
	stShowScreen += stRoundTime + "\r\n";
	stShowScreen += stWeightTime + "\r\n";
    stShowScreen += stAverageRound + "\r\n";
	stShowScreen += stAverageWeight + "\r\n";
	stShowScreen += stFlag + "\r\n";
}

void SchedleMode::HRNMode(CString &stShowScreen){
	CString stEndTime	  = "          |End Time:       ";
	CString stRoundTime	  = "          |Round Time:     ";
	CString stWeightTime  = "    HRN   |Weight Time:    ";
	CString stAverageRound= "          |Average Round:  ";
	CString stAverageWeight="          |Average Weight: ";
	CString stFlag        = "*******************************************************************";
	CString str;

	//the following operations base on FCFS and SJF
	float       high_priority = 0;
	float		sum_round_time = 0.0;
	float		sum_weight_time = 0.0;
	float		priority = 0;

	Process *pTemp1 ,*pTemp2 ,*pLastProcess;
	pLastProcess = pLinkHead->next;
    
    str.Format("%g",pLastProcess->end_time);
		stEndTime += str+"    ";
	str.Format("%g",pLastProcess->around_time);
		stRoundTime += str+"    ";
	str.Format("%g",pLastProcess->weight_time);
		stWeightTime += str+"    ";
    pLastProcess->flags = false;

//	pTemp1 = pLastProcess->next;
	for(int i=1 ;i<iProcessNum ;i++){
		pTemp1 = pLinkHead->next->next;
		while( pLastProcess->end_time >= pTemp1->arrive_time && pTemp1 != pLinkTail ){
			priority = (pLastProcess->end_time - pTemp1->arrive_time + pTemp1->run_time)
				/ pTemp1->run_time;
			if( priority > high_priority && pTemp1->flags == true ){
			high_priority = priority;
			pTemp2 = pTemp1;
			}
			pTemp1 = pTemp1->next;
		}
		if(pLastProcess->end_time < pTemp1->arrive_time && pTemp2 == NULL ){
			pTemp2 = pTemp1;
            pTemp2->end_time = pTemp2->arrive_time + pTemp2->run_time;
		}
		else
		pTemp2->end_time = pLastProcess->end_time + pTemp2->run_time;
	
		str.Format("%g",pTemp2->end_time);
		stEndTime += str+"    ";

		pTemp2->around_time = pTemp2->end_time - pTemp2->arrive_time;
		str.Format("%g",pTemp2->around_time);
		stRoundTime += str+"    ";

		pTemp2->weight_time = pTemp2->around_time / pTemp2->run_time;
		str.Format("%g",pTemp2->weight_time);
		stWeightTime += str+"    ";

		pTemp2->flags = false;
		 high_priority = 0;

		sum_round_time += pTemp2->around_time;
		sum_weight_time += pTemp2->weight_time;
   
		pLastProcess = pTemp2;
		pTemp2 = NULL;
	}

    str.Format("%g",sum_weight_time/iProcessNum);
	stAverageWeight += "           " + str +"               ";

	str.Format("%g",sum_round_time/iProcessNum);
	stAverageRound  += "           " + str +"               ";

    stShowScreen += stEndTime + "\r\n";
	stShowScreen += stRoundTime + "\r\n";
	stShowScreen += stWeightTime + "\r\n";
    stShowScreen += stAverageRound + "\r\n";
	stShowScreen += stAverageWeight + "\r\n";
	stShowScreen += stFlag + "\r\n";

}

⌨️ 快捷键说明

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