////////////////////////////////////////////////////// // // // Incremental Clock Syncronization. // // 236503(20) Summer 2001 // // Advanced Programming Project. // // Technion. Computer Science Department. // // // // Written by: Reitman Anna // // Fidelman Greg // // Supervisor: Hagit Attya // // Saar Pilosof // // // ////////////////////////////////////////////////////// // Scheduler.cpp #include "stdafx.h" #include #include #include "IncClockSynch.h" #include "Scheduler.h" #include "ProcessHolder.h" #include "SyncAlgorithm.h" Scheduler::Scheduler(Scenario sc,ProcessHolder& ph,long nStopTime): m_Scenario(sc),m_nStopTime(nStopTime),m_CurrTime(0),m_ProcHolder(&ph) { if( m_Scenario != GAP_TIME ){ cout << "SCHEDULER: Scenario must be GAP_TIME\n"; exit(1); } if( m_nStopTime < 1 ){ cout << "SCHEDULER: StopTime must be greater than 1\n"; exit(1); } runScenario_(); } Scheduler::Scheduler(Scenario sc,ProcessHolder& ph,long nStopTime,long nStopGap): m_Scenario(sc),m_nStopTime(nStopTime),m_nStopGap(nStopGap),m_CurrTime(0), m_ProcHolder(&ph) { if( (m_Scenario != TIME_PROC) && (m_Scenario != NEW_PROC) ){ cout << "SCHEDULER: Scenario must be TIME_PROC or NEW_PROC\n"; exit(1); } if( ( m_nStopTime < 1 ) || ( m_nStopGap < 0 ) ){ cout << "SCHEDULER: Input is not valid\n"; exit(1); } runScenario_(); } void Scheduler::runScenario_() { long HagitTime = -1; long STTime = -1; char* pFileName = "output.dat"; static bool flag =false; char pbuf[100]; // life cycle while( m_CurrTime <= m_nStopTime ){ // pair std::pair gap = m_ProcHolder->getGap(); // DBG << m_CurrTime << " " << gap.first << " " << gap.second << endl; if( m_Scenario == GAP_TIME ){ sprintf(pbuf,"%d \t %d \t %d \r\n",m_CurrTime, gap.first,gap.second); outputFile.Write( pbuf,strlen(pbuf)); // std::cout << m_CurrTime << " " << gap.first << " " << gap.second << endl; } else{ if( HagitTime < 0 && gap.first <= m_nStopGap ) HagitTime = m_CurrTime; if( STTime < 0 && gap.second <= m_nStopGap ) STTime = m_CurrTime; //both algorithms reached StopGap -> stop the testing if( (HagitTime >= 0) && (STTime >= 0) ){ break; } } //iterate over all processes - give time slot to every one of them for( ProcessHolder::Iterator iterProc(m_ProcHolder); iterProc; ++iterProc ){ if( HagitTime < 0 ) SyncAlgorithm::ExecuteHagitAlg(*iterProc); if( STTime < 0 ) SyncAlgorithm::ExecuteSTAlg(*iterProc); } //increment clocks of all processes (also switches the buffers) m_ProcHolder->incClocks(); m_CurrTime++;//the last thing to do } if( m_Scenario == TIME_PROC || m_Scenario == NEW_PROC ){ sprintf(pbuf,"%d \t %d \t %d \r\n",m_ProcHolder->NumOfProc(), HagitTime,STTime); outputFile.Write( pbuf, strlen(pbuf)); //cout << HagitTime << " " << STTime << endl; } }