Example #1
0
int ShmMgr::getStatSem()
{
	vector<SEMINFO>::iterator it;   		
	int iRtn = 0;
	int ii=0;
	CSemaphore *poInfoDataLock = new CSemaphore();
	char sTemp[32]={0};

	for (it = m_vSEMInfo.begin();it != m_vSEMInfo.end();it ++)
	{
		memset(sTemp,0,sizeof(sTemp));
		sprintf (sTemp, "%ld", it->m_lIpcKey);
    poInfoDataLock->getSem (sTemp, 1, 1);
    if(poInfoDataLock->P(false))
    	{
    		poInfoDataLock->V();
    		it->m_iState=0;
    	}
    else{
    	  it->m_iState=1;
    	}
	}
	delete poInfoDataLock ;
	return m_vSEMInfo.size();
}	
Example #2
0
void CTestSemaphoreApp::Consume(int Num)
{
    for (int i = Num; i > 0; --i ) {
        // we can only consume one by one
        s_semContent.Wait();
        s_semStorage.Wait();
        --s_Counter;
        NcbiCout << "-1=" << s_Counter << NcbiEndl;
        s_semStorage.Post();
        SleepMilliSec(500);
    }
}
Example #3
0
bool CTestSemaphoreApp::TestApp_Exit(void)
{
    NcbiCout
        << "Test completed"
        << NcbiEndl
        << " counter = " << s_Counter
        << NcbiEndl;
    // storage must be available
    assert( s_semStorage.TryWait() );
    // content must be empty
    assert( !s_semContent.TryWait() );
	assert( s_Counter == 0 );
    return true;
}
Example #4
0
void tskLED2(CThread *p_thread, xHandle p_params) {
    CPin led2(LED_PIN_1);
    led2.output();
    while( p_thread->isAlive() ) {
        if ( m_semButton.wait() ) {	// wait for the semaphore
            led2.toggle();
        }
    }
}
Example #5
0
//
// Main Routine
//
int main(void) {
#ifdef DEBUG
    CSerial ser;		// declare a UART object
    ser.enable();
    CDebug dbg(ser);	// Debug stream use the UART object
    dbg.start();
#endif

    //
    // Optional: Enable tickless technology
    //
#ifndef DEBUG
    CPowerSave::tickless(true);
#endif

    //
    // Your setup code here
    //

//	m_semButton.counting(2, 2);
    m_semButton.binary();

    CThread t1(tskLED1);
    t1.start("t1");

    CThread t2(tskLED2);
    t2.start("t2");

    // button
    CButton btn(BUTTON_PIN_0);

    //
    // Enter main loop.
    //
    while(1) {
        //
        // Your loop code here
        //
        if ( btn.isPressed()==BTN_PRESSED ) {
            m_semButton.release();	// signal the semaphore
        }
    }
}
Example #6
0
bool creatSem(vector<SEMINFO> m_vSEMInfo)
{
	
	 CSemaphore * pSemLock =0;
	 char sTemp[32]={0};
	 int iSize=m_vSEMInfo.size();
	 for(int i=0;i<iSize;i++)
	 {

	 	 	pSemLock = new CSemaphore();   
	 	 	if(!pSemLock) return false ;
	 	 	memset(sTemp,0,sizeof(sTemp));
      sprintf (sTemp, "%d", m_vSEMInfo[i].m_lIpcKey);
      if(!pSemLock->getSem (sTemp, 1, 1))
      	LOG.log(MBC_SEMP_CREATE_FAIL,LOG_LEVEL_FATAL,
                LOG_TYPE_SYSTEM,"创建信号量失败,请检查key值[%s]!",sTemp);
       delete pSemLock ;
       pSemLock =0;
     }
}
Example #7
0
void InitReg()
{
    static volatile bool inited;

    inited = false;

    s_SemIn.Lock();

    if (!inited)
	{
		s_regThread = AfxBeginThread(RUNTIME_CLASS(CRegThread));

		while (!inited)
		{
			PostToRegThread(WM_INITCHECK, 0, &inited);
			Sleep(1);
		}
	}

    s_SemIn.Unlock();
}
Example #8
0
UINT __stdcall elevator2(void *args) {
	while (1) {
		if (ps3.Read() > 0) {
			ps3.Wait();
			sema_io_console.Wait();
			MOVE_CURSOR(ELEV2_DISPLAYX, 10);
			printf("Elevator 2\n");
			MOVE_CURSOR(ELEV2_DISPLAYX, 12);
			printf("\t\t\t\t\t");
			MOVE_CURSOR(ELEV2_DISPLAYX, 12);
			if (MyDataPool2->general_status == 0) {
				printf("- Offline\n");
			}
			else {
				printf("- Online\n");
			}

			// clears elevator
			for (int i = 0; i < 10;i++) {
				MOVE_CURSOR(ELEV2_DISPLAYX, 10 + (13 - i));
				printf("\t\t\t\t\t");
			}

			// clears elevator
			for (int i = 0; i < 10;i++) {
				MOVE_CURSOR(ELEV2_DISPLAYX, 10 + (13 - i));
				printf("__ __ F%d", i);
			}

			// displays elevator location
			MOVE_CURSOR(ELEV2_DISPLAYX, 10 + (13 - MyDataPool2->floor));
			printf("[|_|] F%d", MyDataPool2->floor);

			sema_io_console.Signal();
			cs3.Signal();
		}
	}
}
Example #9
0
void CTestSemaphoreApp::Produce(int Num)
{
    // Storage semaphore acts as a kind of mutex - its only purpose
    // is to protect Counter
    s_semStorage.Wait();
    s_Counter += Num;
    NcbiCout << "+" << Num << "=" << s_Counter << NcbiEndl;
    s_semStorage.Post();

    // Content semaphore notifies consumer threads of how many items can be
    // consumed. Slow consumption with fast production causes Content semaphore
    // to overflow from time to time. We catch exception and wait for consumers
    // to consume something
    for (bool Posted=false; !Posted;) {
        try {
            s_semContent.Post(Num);
            Posted = true;
        }
        catch (exception& e) {
            NcbiCout << e.what() << NcbiEndl;
            SleepMilliSec(500);
        }
    }
}
Example #10
0
	int main(){
		while (endSimulation.Read() == 0){
			switch (tank){
			case 1:
				tank1_GSC_Can_Read.Wait();
				break;
			case 2: 
				tank2_GSC_Can_Read.Wait();
				break;
			case 3:
				tank3_GSC_Can_Read.Wait();
				break;
			case 4:
				tank4_GSC_Can_Read.Wait();
				break;
			}
			
			unsigned int fuelAmount = *tankPtr;

			if (fuelAmount > 200){
				GSCLogFileSemaphore.Wait();
				printTime();
				log_file << "******************\nTANK " << tank << ":\nAvailable amount (liters) = " << fuelAmount << "\n\n";
				GSCLogFileSemaphore.Signal();
			}
			else {
				GSCLogFileSemaphore.Wait();
				printTime();
				log_file << "******************\n";
				log_file << "WARNING!!!\nTANK " << tank
					<< ":\nAvailable amount (liters) = " << fuelAmount
					<< "\nWARNING!!!" << "\n\n";
				GSCLogFileSemaphore.Signal();
			}
		}
		return 0;
	}
Example #11
0
//**************************************************************************************************************************************************
void printGSCScreen(){
	while (endSimulation.Read() == 0){
		system("cls");
		cout << "REAL TIME STATUS:";
		printTimeScreen();
		// pumps
		for (int pump = 1; pump < 5; pump++){
			pumpStatus *pumpPtr;

			switch (pump){
			case 1:
				pumpPtr = pump1Ptr;
				break;
			case 2:
				pumpPtr = pump2Ptr;
				break;
			case 3:
				pumpPtr = pump3Ptr;
				break;
			case 4:
				pumpPtr = pump4Ptr;
				break;
			default:
				pumpPtr = NULL;
				break;
			}

			cout << "******************\nPUMP " << pump << ":\nCustomer Name = " << pumpPtr->customerName
				<< "\nCredit Card = " << pumpPtr->customerCC
				<< "\nFuel Type = " << pumpPtr->selectedFuel
				<< "\nFuel Amount = " << pumpPtr->selectedFuelAmount
				<< " liters\nTotal Price = CAD$" << pumpPtr->priceToPay
				<< "\n";

			switch (pumpPtr->status){
			case 0:
				TEXT_COLOUR(GREEN);
				cout << "PUMP FREE\n\n";
				TEXT_COLOUR();
				break;
			case 1:
				TEXT_COLOUR(YELLOW);
				cout << "CUSTOMER SELECTING FUEL\n\n";
				TEXT_COLOUR();
				break;
			case 2:
				TEXT_COLOUR(DARK_YELLOW);
				cout << "CUSTOMER SELECTING FUEL AMOUNT\n\n";
				TEXT_COLOUR();
				break;
			case 3:
				TEXT_COLOUR(CYAN);
				cout << "DISPENSING FUEL\n\n";
				TEXT_COLOUR();
				break;
			case 4:
				TEXT_COLOUR(MAGENTA);
				cout << "WAITING FOR CUSTOMER TO GO AWAY\n\n";
				TEXT_COLOUR();
				break;
			case 5:
				TEXT_COLOUR(BLUE);
				cout << "WAITING FOR PAIMENT\n\n";
				TEXT_COLOUR();
				break;
			default:
				pumpPtr = NULL;
				break;
			}
		}
		// tanks
		for (int tank = 1; tank < 5; tank++) {
			unsigned int *tankPtr;

			switch (tank){
			case 1:
				tankPtr = tank1Ptr;
				break;
			case 2:
				tankPtr = tank2Ptr;
				break;
			case 3:
				tankPtr = tank3Ptr;
				break;
			case 4:
				tankPtr = tank4Ptr;
				break;
			default:
				tankPtr = NULL;
				break;
			}

			unsigned int fuelAmount = *tankPtr;

			if (fuelAmount > 200){
				cout << "******************\nTANK " << tank << ":\nAvailable amount (liters) = " << fuelAmount << "\n\n";
			}
			else {
				cout << "******************\n";
				TEXT_COLOUR(RED);
				cout << "WARNING!!!\nTANK " << tank
					<< ":\nAvailable amount (liters) = " << fuelAmount
					<< "\nWARNING!!!" << "\n\n";
				TEXT_COLOUR();
			}
		}

		SLEEP(FLASH_SPEED);
	}
}
Example #12
0
	int main(){
		switch (pump){
		case 1:
			while (endSimulation.Read() == 0){
				prodPool1.Wait();
				GSCLogFileSemaphore.Wait();
				if (log_file.is_open()){
					printTime();
					log_file	<< "******************\nPUMP 1:\nCustomer Name = " << pump1Ptr->customerName
								<< "\nCredit Card = " << pump1Ptr->customerCC
								<< "\nFuel Type = " << pump1Ptr->selectedFuel
								<< "\nFuel Amount = " << pump1Ptr->selectedFuelAmount
								<< " liters\nTotal Price = CAD$" << pump1Ptr->priceToPay
								<< "\n\n";
				}
				GSCLogFileSemaphore.Signal();
				consPool1.Signal();
			}
				break;
			
		case 2:
			while (endSimulation.Read() == 0){
				prodPool2.Wait();
				GSCLogFileSemaphore.Wait();
				if (log_file.is_open()){
					printTime();
					log_file << "******************\nPUMP 2:\nCustomer Name = " << pump2Ptr->customerName
							 << "\nCredit Card = " << pump2Ptr->customerCC
							 << "\nFuel Type = " << pump2Ptr->selectedFuel
							 << "\nFuel Amount = " << pump2Ptr->selectedFuelAmount
							 << " liters\nTotal Price = CAD$" << pump2Ptr->priceToPay
							 << "\n\n";
				}
				GSCLogFileSemaphore.Signal();
				consPool2.Signal();
			}
				break;
			
		case 3:
			while (endSimulation.Read() == 0){
				prodPool3.Wait();
				GSCLogFileSemaphore.Wait();
				if (log_file.is_open()){
					printTime();
					log_file << "******************\nPUMP 3:\nCustomer Name = " << pump3Ptr->customerName
							 << "\nCredit Card = " << pump3Ptr->customerCC
							 << "\nFuel Type = " << pump3Ptr->selectedFuel
							 << "\nFuel Amount = " << pump3Ptr->selectedFuelAmount
							 << " liters\nTotal Price = CAD$" << pump3Ptr->priceToPay
							 << "\n\n";
				}
				GSCLogFileSemaphore.Signal();
				consPool3.Signal();
			}
			break;
		case 4:
			while (endSimulation.Read() == 0){
				prodPool4.Wait();
				GSCLogFileSemaphore.Wait();
				if (log_file.is_open()){
					printTime();
					log_file << "******************\nPUMP 4:\nCustomer Name = " << pump4Ptr->customerName
							 << "\nCredit Card = " << pump4Ptr->customerCC
							 << "\nFuel Type = " << pump4Ptr->selectedFuel
							 << "\nFuel Amount = " << pump4Ptr->selectedFuelAmount
							 << " liters\nTotal Price = CAD$" << pump4Ptr->priceToPay
							 << "\n\n";
				}
				GSCLogFileSemaphore.Signal();
				consPool4.Signal();
			}
			break;
		default:
			printf("infalid pump\n");
			break;
		}
		return 0;
	}
Example #13
0
UINT ThreadProc(LPVOID pParam)
{
	HWND hwnd = AfxGetMainWnd()->GetSafeHwnd();
	LPTHREADDATA pData = (LPTHREADDATA)pParam;

	Mat dst_temp = Mat::zeros(pData->Roi_dst->size(), 16);
	//Vec3b NullPixel = Vec3b(255, 255, 255);
	float x_, y_;
	Point StartPoint;
	Point EndPoint;
	StartPoint = pData->Roi->tl();
	EndPoint = pData->Roi->br();
	int x_s, x_l, y_s, y_l;
	Vec3b* src_tl;
	Vec3b* dst_;
	float* xmap_;
	float* ymap_;
	unsigned char* mask_;
	int CamIdx;
	int i = 1;
	while (TRUE)
	{

		//MessageBox(NULL, _T("checkpoint1"),NULL , MB_OK);
		//WaitForSingleObject(g_event[pData->ID],INFINITE);
		g_Semaphore.Lock();
		//MessageBox(NULL, _T("checkpoint2"), NULL, MB_OK);

		for (int y = StartPoint.y; y < EndPoint.y; y++)
		{
			//Vec3b* dst_ = pData->Obj->m_dst.ptr<Vec3b>(y - StartPoint.y);
			dst_ = dst_temp.ptr<Vec3b>(y - StartPoint.y);
			xmap_ = pData->Obj->m_xmap.ptr<float>(y);
			ymap_ = pData->Obj->m_ymap.ptr<float>(y);
			mask_ = pData->Obj->m_mask.ptr<unsigned char>(y);

			for (int x = StartPoint.x; x < EndPoint.x; x++)
			{
				CamIdx = (int)mask_[x];
				if (CamIdx == 255)
				{
					//dst_[x] = NullPixel;
					dst_[x - StartPoint.x] = Vec3b(0, 0, 0);
				}
				else
				{
					x_ = xmap_[x];
					y_ = ymap_[x];

					x_s = (int)floor(x_);
					x_l = (int)ceil(x_);
					y_s = (int)floor(y_);
					y_l = (int)ceil(y_);
					//float n = y_ - y_s, m = x_ - x_s;
					if (x_s < 0 || y_s < 0 || x_l>(*pData->Images)[CamIdx].cols - 1 || y_l>(*pData->Images)[CamIdx].rows - 1)
					{
						//cout << "out of range" << endl;
						dst_[x - StartPoint.x] = Vec3b(0, 0, 0);
					}
					else
					{

						src_tl = (*pData->Images)[CamIdx].ptr<Vec3b>(y_s, x_s);
						dst_[x - StartPoint.x] = *src_tl;
					}
				}
			}
		}

		CCritical.Lock();
		pData->Obj->m_dst = pData->Obj->m_dst | dst_temp;
		//signal++;
		::PostMessage(hwnd, WM_LOOP, NULL, NULL);
		CCritical.Unlock();
		//MessageBox(NULL, _T("threadend"), NULL, NULL);
		//Sleep(10);

		//i--;



	}

	dst_temp.release();
	return 0;
};
UINT __stdcall IOToElevator2(void *args)
{								
	
	CDataPool	dp2("Ele2", sizeof(struct myDpData)) ;
	struct		myDpData *Ele2DP = (struct myDpData *)(dp2.LinkDataPool()) ;

	while(flag) {
		
		if (ps3.Read()>0) {
			ps3.Wait();
			m1->Wait();
			
			Ele2Status = *Ele2DP;
			
			m1->Signal();
			cs3.Signal();
		}
		
		m1->Wait();

		//FLOOR 9
		MOVE_CURSOR(29,1);
		printf("          |");
		if (Ele2Status.floor == 9) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,2);
		printf("          |");
		if (Ele2Status.floor == 9) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,3);
		printf("          |");
		if (Ele2Status.floor == 9) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 8
		MOVE_CURSOR(29,5);
		printf("          |");
		if (Ele2Status.floor == 8) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,6);
		printf("          |");
		if (Ele2Status.floor == 8) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,7);
		printf("          |");
		if (Ele2Status.floor == 8) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 7
		MOVE_CURSOR(29,9);
		printf("          |");
		if (Ele2Status.floor == 7) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,10);
		printf("          |");
		if (Ele2Status.floor == 7) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,11);
		printf("          |");
		if (Ele2Status.floor == 7) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 6
		MOVE_CURSOR(29,13);
		printf("          |");
		if (Ele2Status.floor == 6) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,14);
		printf("          |");
		if (Ele2Status.floor == 6) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,15);
		printf("          |");
		if (Ele2Status.floor == 6) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 5
		MOVE_CURSOR(29,17);
		printf("          |");
		if (Ele2Status.floor == 5) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,18);
		printf("          |");
		if (Ele2Status.floor == 5) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,19);
		printf("          |");
		if (Ele2Status.floor == 5) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 4
		MOVE_CURSOR(29,21);
		printf("          |");
		if (Ele2Status.floor == 4) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,22);
		printf("          |");
		if (Ele2Status.floor == 4) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,23);
		printf("          |");
		if (Ele2Status.floor == 4) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 3
		MOVE_CURSOR(29,25);
		printf("          |");
		if (Ele2Status.floor == 3) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,26);
		printf("          |");
		if (Ele2Status.floor == 3) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,27);
		printf("          |");
		if (Ele2Status.floor == 3) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 2
		MOVE_CURSOR(29,29);
		printf("          |");
		if (Ele2Status.floor == 2) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,30);
		printf("          |");
		if (Ele2Status.floor == 2) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,31);
		printf("          |");
		if (Ele2Status.floor == 2) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 1
		MOVE_CURSOR(29,33);
		printf("          |");
		if (Ele2Status.floor == 1) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");

		MOVE_CURSOR(29,34);
		printf("          |");
		if (Ele2Status.floor == 1) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");

		MOVE_CURSOR(29,35);
		printf("          |");
		if (Ele2Status.floor == 1) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 0
		MOVE_CURSOR(29,37);
		printf("          |");
		if (Ele2Status.floor == 0) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,38);
		printf("          |");
		if (Ele2Status.floor == 0) {
			printf("|");
			if (Ele2Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(29,39);
		printf("          |");
		if (Ele2Status.floor == 0) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");


		MOVE_CURSOR(29,41);
		printf("           Ele2");
		MOVE_CURSOR(36,42);
		for (int i = 7; i < 10; i++) {
			if (Ele2Status.lights[i] == 1) {
				TEXT_COLOUR(14);
			}
			printf("[%d] ", i);
			TEXT_COLOUR(7);
		}
		MOVE_CURSOR(36,43);
		for (int i = 4; i < 7; i++) {
			if (Ele2Status.lights[i] == 1) {
				TEXT_COLOUR(14);
			}
			printf("[%d] ", i);
			TEXT_COLOUR(7);
		}
		MOVE_CURSOR(36,44);
		for (int i = 1; i < 4; i++) {
			if (Ele2Status.lights[i] == 1) {
				TEXT_COLOUR(14);
			}
			printf("[%d] ", i);
			TEXT_COLOUR(7);
		}
		MOVE_CURSOR(40,45);
		if (Ele2Status.lights[0] == 1) {
			TEXT_COLOUR(14);
		}
		printf("[0]");
		TEXT_COLOUR(7);

		fflush(stdout);
		MOVE_CURSOR(0,50);
		fflush(stdout);
		m1->Signal();


		SLEEP(1);
	}
	return 0 ;									
}
Example #15
0
UINT __stdcall elevator_console(void *args)
{
	char str[3];
	int val[2]; // save str as int
	int priority = 0;

	Sleep(2000);

	while (1) {

		// gets request entry
		sema_io_console.Wait();
		MOVE_CURSOR(0, 2);
		printf("Please enter command:\n");
		sema_io_console.Signal();
		str[0] = _getch();
		// Clears out the enter message
		sema_io_console.Wait();
		MOVE_CURSOR(0, 3);
		printf("\t\t\t\t\t");
		MOVE_CURSOR(0, 3);
		printf("Servicing: %c", str[0]);
		sema_io_console.Signal();
		str[1] = _getch();
		str[2] = '\0';
		sema_io_console.Wait();
		MOVE_CURSOR(0, 3);
		printf("\t\t\t\t\t");
		MOVE_CURSOR(0, 3);
		printf("Entered: %s\n", str);


		// convert input to integers if valid
		if (atoi(&str[1]) && str[1] != '0') {
			val[1] = atoi(&str[1]);
		}
		else if (str[1] == '0') {
			val[1] = 0;
		}

		MOVE_CURSOR(0, 4);
		printf("\t\t\t\t\t\t\t\t\t\t");
		MOVE_CURSOR(0, 4);

		if (!strcmp(str, "ee")) { // exit case
			printf("Shutting down elevators\n");

			// Write request and priority type to pipe
			mypip1 = { 99, 99 }; // exit code
			pipe.Write(&mypip1, sizeof(mypip1));

			// redezvous type here before the return 0
			return 0;
		}
		else if ((str[0] == 'u' && (0 <= val[1] && val[1] < 9)) ||
			(str[0] == 'd' && (0 < val[1] && val[1] < 10))) { // calling the elevator case
			printf("Please wait, going %c from floor %c\n", str[0], str[1]);

			// Write request and priority type to pipe
			// "3-" is an up request and "4-" is a down request
			if (str[0] == 'u') {
				mypip1 = { 30 + val[1],priority }; // exit code
			}
			else if (str[0] == 'd') {
				mypip1 = { 40 + val[1],priority }; // exit code
			}
			pipe.Write(&mypip1, sizeof(mypip1));

			priority++;

		}
		else if ((str[0] == '1' || str[0] == '2') && (0 <= val[1] && val[1] < 10)) { // inside the elevator case
			printf("Closing door, moving elevator %c to floor %c \n", str[0], str[1]);
			if (str[0] == '1') {
				mypip1 = { 10 + val[1],priority }; // exit code
			}
			else if (str[0] == '2') {
				mypip1 = { 20 + val[1],priority }; // exit code
			}
			pipe.Write(&mypip1, sizeof(mypip1));

			priority++;
		}
		else if ((str[0] == '+' || str[0] == '-') && (val[1] == 1 || val[1] == 2)) // servicing elevators and faults
		{
			if (!strcmp(str, "+1"))
			{
				printf("elevator 1 is in service \n");
			}
			else if (!strcmp(str, "-1"))
			{
				printf("elevator 1 is out of service \n");
			}
			else if (!strcmp(str, "+2"))
			{
				printf("elevator 2 is in service \n");
			}
			else if (!strcmp(str, "-2"))
			{
				printf("elevator 2 is out of service \n");
			}

			// Write request and priority type to pipe
			// "5-" is an up request and "6-" is a down request
			if (str[0] == '+') {
				mypip1 = { 50 + val[1],0 }; // exit code
			}
			else if (str[0] == '-') {
				mypip1 = { 60 + val[1],0 }; // exit code
			}
			pipe.Write(&mypip1, sizeof(mypip1));

		}
		else {
			printf("INVALID ENTRY!\n");
		}

		sema_io_console.Signal();

		val[0] = -1; //if val[0] isn't what we want then set it to -1
		val[1] = -1; //if val[1] isn't what we want then set it to -1

	}
}
Example #16
0
		inline void wake()
		{
			wait.signal();
		}
int main(){
	char KeyData;
	int elevatorMoveCounter = 0, doorCounter = 0, counter = 0;
	int elevatorMoveFlag = 0;
	int pEle1data = NO_DESTINATION, pEle1_1data = NO_DESTINATION, pipe8data = 0;
	int pEle1data_floor = NO_DESTINATION;
	int pEle1data_dir = NO_DESTINATION;
	int pEle1_2data = 0;
	int destination = NO_DESTINATION;
	int updateDP = 1;
	int flag = 1;
	
	struct myDpData elevator;
	elevator.floor = 1;
	elevator.usage = NOTUSED;
	elevator.door = CLOSED;
	elevator.direction = STOPPED;
	for (int i = 0; i<10; i++) {
		elevator.floors[i] = 0;
	}
	for (int i = 0; i<10; i++) {
		elevator.lights[i] = 0;
	}
	for (int i = 0; i<10; i++) {
		elevator.updir[i] = 0;
	}
	for (int i = 0; i<10; i++) {
		elevator.downdir[i] = 0;
	}

	CDataPool dp("Ele1", sizeof(struct myDpData)) ;
	struct myDpData	 *MyDataPool = (struct myDpData *)(dp.LinkDataPool()) ;

	CPipe	pEle1	("PipeDispatcherToElevator1", 1024) ;
	CPipe	pEle1_1	("PipeDispatcherToElevator1_1", 1024) ; //only used for lights
	CPipe	pEle1_2 ("PipeDispatcherToElevator1_2", 1024) ; 
	CPipe	pEle1_3 ("PipeDispatcherToElevator1_3", 1024) ; //not used
	CPipe	pEle1_4 ("PipeDispatcherToElevator1_4", 1024) ; //not used
	CPipe	pipe8	("PipeIOToElevator1", 1024);

	
	r1.Wait();

	while(flag) {
		//termination from master
		if ((pipe8.TestForData()) >= sizeof(pipe8data) )	{		// if at least 1 integer in pipeline
			pipe8.Read(&pipe8data , sizeof(pipe8data)) ;		// read data from pipe 
			if (pipe8data % 10 == 9 && pipe8data/10 == 9) {
				break;
			}
		}



		//@@ Pipe 1: Destination
		if ((pEle1.TestForData()) >= sizeof(pEle1data) )	{		// if at least 1 integer in pipeline
			pEle1.Read(&pEle1data , sizeof(pEle1data)) ;		// read data from pipe 
			pEle1data_floor = pEle1data%10;
			pEle1data_dir = pEle1data/10;
			if (elevator.floors[pEle1data_floor] == 0) {
				elevator.floors[pEle1data_floor] = 1;
				updateDP = 1;
			}
			if (pEle1data_dir == UP) {
				elevator.updir[pEle1data_floor] = 1;
			}
			
			else if (pEle1data_dir == DOWN) {
				elevator.downdir[pEle1data_floor] = 1;
			}
			if (destination == NO_DESTINATION) {
				destination = pEle1data_floor;
			}
			else if (pEle1data_dir == UP && elevator.direction == UP && (destination - pEle1data_floor > 0)) {
				destination = pEle1data_floor;
			}
			else if (pEle1data_dir == DOWN && elevator.direction == DOWN && (pEle1data_floor - destination > 0)) {
				destination = pEle1data_floor;
			}			
			pEle1data = NO_DESTINATION;
			pEle1data_floor = NO_DESTINATION;
			pEle1data_dir = NO_DESTINATION;
		}

		// @@ Pipe 2: Lights
		if ((pEle1_1.TestForData()) >= sizeof(pEle1_1data) ){		// if at least 1 integer in pipeline
			pEle1_1.Read(&pEle1_1data , sizeof(pEle1_1data)) ;		// read data from pipe 
			elevator.lights[pEle1_1data] = 1;
		}

		// @@ Update Datapool
		if (updateDP == 1) {
			cs1.Wait();
			cs2.Wait();
			*MyDataPool = elevator;
			ps1.Signal();
			ps2.Signal();
			updateDP = 0;
		}
		
		//START CONDITION - Set elevator to USING. Set direction, and Set MoveFlag if destination != elevator.floor.
		if (elevator.usage == NOTUSED && destination != NO_DESTINATION) {
			elevator.usage = USING;
			updateDP = 1;
			if (destination > elevator.floor) {
				elevator.direction = UP;
				elevatorMoveFlag = 1;
			}
			else if (destination < elevator.floor) {
				elevator.direction = DOWN;
				elevatorMoveFlag = 1;
			}
		}

		//REASON TO OPEN DOOR. Primary reason is destination == elevator.floor && .door == CLOSED. 
		//Elevator.usage is failsafe.
		if (destination == elevator.floor && elevator.usage == USING && elevator.door == CLOSED) {
			elevator.door = OPEN;
			updateDP = 1;
			// destination calculation
			elevator.floors[destination] = 0; //destination met.
			elevator.lights[destination] = 0;

			if (elevator.direction == UP) {
				if (elevator.floor == 9) {
					for (int i = (destination - 1); (elevator.floor == destination) && (i >= 0); i--) {
						if (elevator.floors[i] == 1) {
							destination = i;
							elevator.direction = DOWN;
						}
					}
					if (elevator.floor == destination) {
						elevator.usage = NOTUSED;
						elevator.direction = STOPPED;
						destination = NO_DESTINATION;
					}
				}
			
				else if (elevator.floor < 9) {
					for (int i = (destination + 1); (elevator.floor == destination) && (i < 10); i++) {
						if (elevator.floors[i] == 1) {
							destination = i;
						}
					}
					if (elevator.floor == destination) {
						for (int i = (destination - 1); (elevator.floor == destination) && (i >= 0); i--) {
							if (elevator.floors[i] == 1) {
								destination = i;
								elevator.direction = DOWN;
							}
						}
						if (elevator.floor == destination) {
							elevator.usage = NOTUSED;
							elevator.direction = STOPPED;
							destination = NO_DESTINATION;
						}
					}
				}
				else {
					printf ("samefloor     \n");
				}
			}

			else if (elevator.direction == DOWN) {
				if (elevator.floor == 0) {
					for (int i = (destination + 1); (elevator.floor == destination) && (i < 10); i++) {
						if (elevator.floors[i] == 1) {
							destination = i;
							elevator.direction = UP;
						}
					}
					if (elevator.floor == destination) {
						elevator.usage = NOTUSED;
						elevator.direction = STOPPED;
						destination = NO_DESTINATION;
					}
				}

				else if (elevator.floor > 0) {
					for (int i = (destination - 1); (elevator.floor == destination) && (i >= 0); i--) {
						if (elevator.floors[i] == 1) {
							destination = i;
						}
					}
					if (elevator.floor == destination) {
						for (int i = (destination + 1); (elevator.floor == destination) && (i < 10); i++) {
							if (elevator.floors[i] == 1) {
								destination = i;
								elevator.direction = UP;
							}
						}
						if (elevator.floor == destination) {
							elevator.usage = NOTUSED;
							elevator.direction = STOPPED;
							destination = NO_DESTINATION;
						}
					}
				}
				else { printf("Error 2\n");
				getchar();
				}
			}
			else {
				elevator.usage = NOTUSED;
				destination = NO_DESTINATION;
			}
			if (elevator.direction == UP) { //!potential bug here
				elevator.updir[elevator.floor] = 0;
			}
			else if (elevator.direction == DOWN) {
				elevator.downdir[elevator.floor] = 0;
			}
			else if (elevator.direction == STOPPED) {
				elevator.updir[elevator.floor] = 0;
				elevator.downdir[elevator.floor]= 0;
			}
		}
		
		//door open counter.
		if (elevator.door == OPEN) {
			doorCounter++;
		}

		//close door.
		if (doorCounter >= 1500) { //should be 1501 or > 1500 but w/e
			updateDP = 1;
			elevator.door = CLOSED;
			doorCounter = 0;

			if (elevator.direction == UP || elevator.direction == DOWN) {
				elevatorMoveFlag = 1;
			}
		}


		//move elevator counter.
		if (elevatorMoveFlag == 1) {
			elevatorMoveCounter++;
		}			

		//moved a floor.
		if (elevatorMoveCounter >= 1000) { //should be 1001 or > 1000 but w/e
			updateDP = 1;
			elevatorMoveCounter = 0;
			if (elevator.direction == UP) {
				elevator.floor++;
			}
			else if (elevator.direction == DOWN) {
				elevator.floor--;
			}

			if (elevator.floor == destination) {
				elevatorMoveFlag = 0;
			}
		}



		counter++;

		if (counter >= 100) {
			MOVE_CURSOR(0,0);
			printf("elevator.floor: %d\n", elevator.floor);
			if (elevator.door == CLOSED)
				printf("elevator.door : CLOSED\n");
			else
				printf("elevator.door : OPEN\n");
			printf("elevator.usage: %d\n", elevator.usage);
			if (elevator.direction == DOWN) 
				printf("elevator.direction: DOWN    \n");
			else if (elevator.direction == UP)
				printf("elevator.direction: UP      \n");
			else
				printf("elevator.direction: STOPPED \n");
			for (int i = 0; i < 10; i++) {
				if (elevator.lights[i] == 1)
					TEXT_COLOUR(14);
				printf(" %d  ",i);
				TEXT_COLOUR(7);
			}
			printf("\n");
			for (int i = 0; i < 10; i++) 
				printf("[%d] ", elevator.floors[i]);
			printf("\n");
			for (int i = 0; i < 10; i++) {
				if (elevator.updir[i] == 1)
					TEXT_COLOUR(14);
				else 
					TEXT_COLOUR(7);
				printf("[%d] ", elevator.updir[i]);
			}
			TEXT_COLOUR(7);
			printf("updir\n");
			for (int i = 0; i < 10; i++) {
				if (elevator.downdir[i] == 1)
					TEXT_COLOUR(14);
				else 
					TEXT_COLOUR(7);
				printf("[%d] ", elevator.downdir[i]);
			}
			TEXT_COLOUR(7);
			printf("downdir\n");
			if (destination == 11)
				printf ("\ndestination: NO_DESTINATION\n");
			else
				printf ("\ndestination: %d              \n", destination);
			printf ("elevatorDoorFlag: %d      elevatorMoveFlag: %d     \n", elevator.door, elevatorMoveFlag);
			fflush(stdout);
			counter = 0;
		}

		Sleep(1);

		if (TEST_FOR_KEYBOARD() != 0) {
			KeyData = getch() ;					// read next character from keyboard
			if (KeyData == 'x'){
				break;
			}	
		}
		
		//Freeze

		if ((pEle1_2.TestForData()) >= sizeof(pEle1_2data) )	{		// if at least 1 integer in pipeline
			pEle1_2.Read(&pEle1_2data , sizeof(pEle1_2data)) ;		// read data from pipe 
			if (pEle1_2data == 1) {
				while (pEle1_2data == 1) {
					if ((pEle1_2.TestForData()) >= sizeof(pEle1_2data)) {	
						pEle1_2.Read(&pEle1_2data , sizeof(pEle1_2data)) ;
					}
					if ((pipe8.TestForData()) >= sizeof(pipe8data) )	{		// if at least 1 integer in pipeline
						pipe8.Read(&pipe8data , sizeof(pipe8data)) ;		// read data from pipe 
						if (pipe8data % 10 == 9 && pipe8data/10 == 9) {
							flag = 0;
							break;
						}
					}
					MOVE_CURSOR(0,1);
					printf("FROZEN!");
					fflush(stdout);
					Sleep(1);
				}
			}
		}

	}
	printf("Exiting... (waiting on other processes)");
	r2.Wait();
	return 0;
}
UINT __stdcall IOToElevator1(void *args)			
{
	CDataPool	dp1("Ele1", sizeof(struct myDpData)) ;
	struct		myDpData *Ele1DP = (struct myDpData *)(dp1.LinkDataPool());

	while(flag) {	
		if (ps1.Read()>0) {
			ps1.Wait();
			m1->Wait();

			Ele1Status = *Ele1DP;
			
			m1->Signal();
			cs1.Signal();
		}

		//FLOOR 9
		m1->Wait();
		MOVE_CURSOR(0,1);
		printf("          |");
		if (Ele1Status.floor == 9) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,2);
		printf("Floor 9   |");
		if (Ele1Status.floor == 9) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,3);
		printf("          |");
		if (Ele1Status.floor == 9) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 8
		MOVE_CURSOR(0,5);
		printf("          |");
		if (Ele1Status.floor == 8) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,6);
		printf("Floor 8   |");
		if (Ele1Status.floor == 8) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,7);
		printf("          |");
		if (Ele1Status.floor == 8) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 7
		MOVE_CURSOR(0,9);
		printf("          |");
		if (Ele1Status.floor == 7) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,10);
		printf("Floor 7   |");
		if (Ele1Status.floor == 7) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,11);
		printf("          |");
		if (Ele1Status.floor == 7) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 6
		MOVE_CURSOR(0,13);
		printf("          |");
		if (Ele1Status.floor == 6) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,14);
		printf("Floor 6   |");
		if (Ele1Status.floor == 6) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,15);
		printf("          |");
		if (Ele1Status.floor == 6) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 5
		MOVE_CURSOR(0,17);
		printf("          |");
		if (Ele1Status.floor == 5) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,18);
		printf("Floor 5   |");
		if (Ele1Status.floor == 5) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,19);
		printf("          |");
		if (Ele1Status.floor == 5) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 4
		MOVE_CURSOR(0,21);
		printf("          |");
		if (Ele1Status.floor == 4) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,22);
		printf("Floor 4   |");
		if (Ele1Status.floor == 4) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,23);
		printf("          |");
		if (Ele1Status.floor == 4) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 3
		MOVE_CURSOR(0,25);
		printf("          |");
		if (Ele1Status.floor == 3) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,26);
		printf("Floor 3   |");
		if (Ele1Status.floor == 3) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,27);
		printf("          |");
		if (Ele1Status.floor == 3) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 2
		MOVE_CURSOR(0,29);
		printf("          |");
		if (Ele1Status.floor == 2) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,30);
		printf("Floor 2   |");
		if (Ele1Status.floor == 2) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,31);
		printf("          |");
		if (Ele1Status.floor == 2) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 1
		MOVE_CURSOR(0,33);
		printf("          |");
		if (Ele1Status.floor == 1) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");

		MOVE_CURSOR(0,34);
		printf("Floor 1   |");
		if (Ele1Status.floor == 1) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");

		MOVE_CURSOR(0,35);
		printf("          |");
		if (Ele1Status.floor == 1) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");

		//FLOOR 0
		MOVE_CURSOR(0,37);
		printf("          |");
		if (Ele1Status.floor == 0) 
			printf("._.");
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,38);
		printf("Floor 0   |");
		if (Ele1Status.floor == 0) {
			printf("|");
			if (Ele1Status.door == OPEN) 
				printf("O");
			else
				printf("-");
			printf("|");
		}
		else
			printf("   ");
		printf("|   ");
		MOVE_CURSOR(0,39);
		printf("          |");
		if (Ele1Status.floor == 0) 
			printf("^-^");
		else
			printf("   ");
		printf("|   ");


		MOVE_CURSOR(0,41);
		printf("Buttons in Ele1");
		MOVE_CURSOR(7,42);
		for (int i = 7; i < 10; i++) {
			if (Ele1Status.lights[i] == 1) {
				TEXT_COLOUR(14);
			}
			printf("[%d] ", i);
			TEXT_COLOUR(7);
		}
		MOVE_CURSOR(7,43);
		for (int i = 4; i < 7; i++) {
			if (Ele1Status.lights[i] == 1) {
				TEXT_COLOUR(14);
			}
			printf("[%d] ", i);
			TEXT_COLOUR(7);
		}
		MOVE_CURSOR(7,44);
		for (int i = 1; i < 4; i++) {
			if (Ele1Status.lights[i] == 1) {
				TEXT_COLOUR(14);
			}
			printf("[%d] ", i);
			TEXT_COLOUR(7);
		}
		MOVE_CURSOR(11,45);
		if (Ele1Status.lights[0] == 1) {
			TEXT_COLOUR(14);
		}
		printf("[0]");
		TEXT_COLOUR(7);
		//printf("Ele1 Read value for Floor = %d                                \n", Ele1Status.floor) ;
		//printf("Ele1 Read value for Usage = %d                                \n", Ele1Status.usage);
		//printf("Ele1 Read value for Door = %d                                 \n", Ele1Status.direction) ; 
		//printf("Ele1 Read value for Direction = %d                            \n", Ele1Status.direction) ; 
		//printf("Ele1 Read values for floor array = ") ;
		//for(int i=0; i < 10; i ++) {
		//	printf("%d ", Ele1Status.floors[i]) ;
		//}
		fflush(stdout);
		MOVE_CURSOR(0,50);
		fflush(stdout);
		m1->Signal();

		SLEEP(10);
	}
	return 0 ;									
}