int main(){ printf("GSC arriving at rendezvous...\n"); r1.Wait(); log_file.open("..\\log_file.txt", ios::out | ios::app); p1.Resume(); p2.Resume(); p3.Resume(); p4.Resume(); t1.Resume(); t2.Resume(); t3.Resume(); t4.Resume(); printGSCScreen(); p1.~printPumpStatus(); p2.~printPumpStatus(); p3.~printPumpStatus(); p4.~printPumpStatus(); t1.~printFuelTankStatus(); t2.~printFuelTankStatus(); t3.~printFuelTankStatus(); t4.~printFuelTankStatus(); log_file.close(); return 0; }
int main(int argc, char *argv[]) { // Waiting for init rendezvous cout << "Waiting for init data rendezvous" << endl; r_init1.Wait(); /* CSemaphore ps1("Prod1",0); // e1 datapool semaphore producer CSemaphore ps3("Prod3",0); // e2 datapool semaphore producer //CSemaphore ps3("Prod3",0,1); // IO pipeline semaphore producer CSemaphore cs1("Cons1",1); // e1 datapool semaphore consumer CSemaphore cs3("Cons3",1); // e2 datapool semaphore consumer //CSemaphore cs3("Cons3",1,1); // IO pipeline semaphore consumer printf("IO Process Creating the Pipeline.....\n") ; CPipe pipe("MyPipe", 1024) ; // Create a pipe 'p1' with the name "MyPipe" //cs3.Wait(); printf("Writing struct to pipe from IO to dispatcher with values mystruct.request = %d, mystruct.priority = %d.\n", mypip1.request, mypip1.priority); pipe.Write(&mypip1, sizeof(mypip1)) ; // write the structure to the pipeline //ps3.Signal(); // IO initializing e1 and e2 datapools printf("IO attempting to create/use the datapool.....\n") ; CDataPool dp1("Elevator1", sizeof(struct mydatapooldata)) ; struct mydatapooldata *MyDataPool1 = (struct mydatapooldata *)(dp1.LinkDataPool()) ; printf("IO linked to e1 datapool at address %p.....\n", MyDataPool1) ; CDataPool dp2("Elevator2", sizeof(struct mydatapooldata)) ; struct mydatapooldata *MyDataPool2 = (struct mydatapooldata *)(dp2.LinkDataPool()) ; printf("IO linked to e2 datapool at address %p.....\n", MyDataPool2) ; */ // Sleep(3000); //// Waiting for rendezvous //cout << "Waiting for data rendezvous" << endl; //r1.Wait(); //detect for keyboard hit /* while (!_kbhit()) { if (ps1.Read() > 0) { ps1.Wait(); // Print off datapool values for e1 printf("\nIO Read e1 value for Floor = %d\n", MyDataPool1->floor); printf("IO Read e1 value for Direction = %d\n", MyDataPool1->direction); printf("My e1 onNOMOMOMOM value is = %d\n", MyDataPool1->onNOMOMOMOM); printf("IO Read e1 values for floor array = "); for (int i = 0; i < 10; i++) printf("%d ", MyDataPool1->floors[i]); cout << endl; cs1.Signal(); } if (ps3.Read() > 0) { ps3.Wait(); // Print off datapool values for e2 printf("\nIO Read e2 value for Floor = %d\n", MyDataPool2->floor); printf("IO Read e2 value for Direction = %d\n", MyDataPool2->direction); printf("My e2 onNOMOMOMOM value is = %d\n", MyDataPool2->onNOMOMOMOM); printf("IO Read e2 values for floor array = "); for (int i = 0; i < 10; i++) printf("%d ", MyDataPool2->floors[i]); cs3.Signal(); } } */ CThread c1(elevator_console, ACTIVE, NULL); CThread c2(elevator1, ACTIVE, NULL); CThread c3(elevator2, ACTIVE, NULL); c1.WaitForThread(); c2.WaitForThread(); c3.WaitForThread(); cout << endl << endl; // Waiting for terminate rendezvous cout << "Waiting for terminate data rendezvous" << endl; r_term.Wait(); //system( "Pause" ); return 0; }
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; }
//@ MAIN int main(){ char KeyData; char KeyData1; int iKeyData; int preflag = 1; int termination_code = 99; //@ SEQ: STARTUP RENDEZVOUS printf("Pretending to rendezvous by sleeping 3 seconds...\n"); Sleep(3000); r1.Wait(); printf("let's go!"); Sleep(1000); MOVE_CURSOR(0,0); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,4); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,8); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,12); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,16); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,20); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,24); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,28); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,32); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,36); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,40); printf("----------+---+---------------------+---+--------------------"); fflush(stdout); //@ INIT: THREADS CThread t1(IOToElevator1, ACTIVE, NULL) ; CThread t2(IOToElevator2, ACTIVE, NULL) ; //printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); //MOVE_CURSOR(0,0); //printf("Press x at any time to exit... "); //fflush(stdout); while(flag) { // Master Termination. if (preflag == 0) { pipe3.Write(&termination_code, sizeof(int)); pipe4.Write(&termination_code, sizeof(int)); flag = 0; Sleep(50); pipe1.Write(&termination_code, sizeof(int)); break; } if (TEST_FOR_KEYBOARD() != 0) { KeyData = getch() ; // read next character from keyboard m1->Wait(); MOVE_CURSOR(0,50); printf("first character = %c \n", KeyData); printf("second character? \n"); printf(" \n"); printf(" \n"); fflush(stdout); m1->Signal(); while (TEST_FOR_KEYBOARD() == 0) {} KeyData1 = getch(); m1->Wait(); MOVE_CURSOR(0,51); printf("second character = %c \n", KeyData1); printf("command entered is: %c%c \n", KeyData, KeyData1); //Command Organization. if (KeyData == 'e' && KeyData1 == 'e'){ preflag = 0; printf("terminating. (2 seconds) \n "); Sleep(2000); } else if (KeyData == '-' && KeyData1 == '1') { iKeyData = 70; pipe1.Write(&iKeyData, sizeof(int)); printf("FREEZING Elevator 1! "); } else if (KeyData == '-' && KeyData1 == '2') { iKeyData = 71; pipe1.Write(&iKeyData, sizeof(int)); printf("FREEZING Elevator 2! "); } else if (KeyData == '+' && KeyData1 == '1') { iKeyData = 75; pipe1.Write(&iKeyData, sizeof(int)); printf("FAULT FIXED for Elevator 1! "); } else if (KeyData == '+' && KeyData1 == '2') { iKeyData = 76; pipe1.Write(&iKeyData, sizeof(int)); printf("FAULT FIXED for Elevator 2! "); } else if (KeyData == 'u'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 ) ) { iKeyData = 10 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Outside Floor %d wants to go UP. ", (KeyData1 - '0') ); } else if (KeyData == 'd'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 )) { iKeyData = 20 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Outside Floor %d wants to go DOWN. ", (KeyData1 - '0') ); } else if (KeyData == '1'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 )) { iKeyData = 50 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Inside Elevator 1 Pressed Floor %d. ", (KeyData1 - '0') ); } else if (KeyData == '2'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 )) { iKeyData = 60 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Inside Elevator 2 Pressed Floor %d. ", (KeyData1 - '0') ); } else{ printf("ERROR: illegible code. "); } fflush(stdout); m1->Signal(); KeyData = '0'; KeyData1 = '0'; iKeyData = 0; } m1->Wait(); // 9 MOVE_CURSOR(20,2); printf(" |"); if (Ele1Status.updir[9] == 1 || Ele2Status.updir[9] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[9] == 1 || Ele2Status.downdir[9] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //8 MOVE_CURSOR(20,6); printf(" |"); if (Ele1Status.updir[8] == 1 || Ele2Status.updir[8] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[8] == 1 || Ele2Status.downdir[8] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //7 MOVE_CURSOR(20,10); printf(" |"); if (Ele1Status.updir[7] == 1 || Ele2Status.updir[7] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[7] == 1 || Ele2Status.downdir[7] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //6 MOVE_CURSOR(20,14); printf(" |"); if (Ele1Status.updir[6] == 1 || Ele2Status.updir[6] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[6] == 1 || Ele2Status.downdir[6] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //5 MOVE_CURSOR(20,18); printf(" |"); if (Ele1Status.updir[5] == 1 || Ele2Status.updir[5] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[5] == 1 || Ele2Status.downdir[5] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //4 MOVE_CURSOR(20,22); printf(" |"); if (Ele1Status.updir[4] == 1 || Ele2Status.updir[4] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[4] == 1 || Ele2Status.downdir[4] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //3 MOVE_CURSOR(20,26); printf(" |"); if (Ele1Status.updir[3] == 1 || Ele2Status.updir[3] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[3] == 1 || Ele2Status.downdir[3] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //2 MOVE_CURSOR(20,30); printf(" |"); if (Ele1Status.updir[2] == 1 || Ele2Status.updir[2] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[2] == 1 || Ele2Status.downdir[2] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //1 MOVE_CURSOR(20,34); printf(" |"); if (Ele1Status.updir[1] == 1 || Ele2Status.updir[1] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[1] == 1 || Ele2Status.downdir[1] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //0 MOVE_CURSOR(20,38); printf(" |"); if (Ele1Status.updir[0] == 1 || Ele2Status.updir[0] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[0] == 1 || Ele2Status.downdir[0] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); fflush(stdout); MOVE_CURSOR(0,50); fflush(stdout); m1->Signal(); Sleep(1); } t1.WaitForThread() ; t2.WaitForThread() ; printf("\nExiting... (waiting on other processes)"); r2.Wait(); return 0; }