void readCmdline(const int argc, char* argv[]){ int start = clock(); //计时器 for (int arg_i = 0; arg_i < argc; arg_i++){ m_cmdInfo.push_back(argv[arg_i]); m_cmdInfo.push_back(" "); } m_cmdInfo.push_back("\n"); if (1 == argc){ printHelpInfo(); DisplayCMDUsage(); }else if (argc == 2 && (stringProcess::bMatchingFix(argv[1], ".cfg", true, true) || stringProcess::bMatchingFix(argv[1], ".para", true, true))){ readPara(argv); }else{ //readCmd(); cout << "Todo: 准备加入从命令行读取参数的运行方式" << endl; getchar(); } int seconds = (clock() - start) / CLOCKS_PER_SEC; cout << "\t == == == Time elapsed: " << seconds << " seconds. == == ==" << endl; }
int main(int argc, char *argv[]) { /*-------------initialization---------------*/ int retcode = 0, flag = 0; int threadNum; int *totalShareN = nullptr, *periodT = nullptr, *threadFinishChecked; double *alpha = nullptr, *pi = nullptr, *P0 = nullptr; HANDLE *pThread; unsigned *pthreadID; bag **pbags; HANDLE consolemutex; HANDLE *mastermutexes; char jstatus = RUNNING; int finishedThreads = 0; //check input if(argc != 2){ printf("Usage: Final.exe datafilename\n"); retcode = 100; goto BACK; } /*-------------Read parametmer from data file---------------*/ readPara(argv[1], &threadNum, &totalShareN, &periodT, &alpha, &pi, &P0); /*-------------Initialize multi threads---------------*/ pbags = (bag **)calloc(threadNum, sizeof(bag *)); pThread = (HANDLE *)calloc(threadNum, sizeof(HANDLE)); pthreadID= (unsigned *)calloc(threadNum, sizeof(unsigned)); mastermutexes = (HANDLE *)calloc(threadNum, sizeof(HANDLE)); threadFinishChecked = (int *)calloc(threadNum, sizeof(int)); if(pthreadID == NULL || pThread == NULL || pbags == NULL || mastermutexes == NULL){ printf("cannot allocate %d threads and their bags", threadNum); retcode = 1; goto BACK; } consolemutex = CreateMutex(NULL, 0, NULL); for(int j = 0; j < threadNum; j++){ pbags[j] = new bag(totalShareN[j],periodT[j],alpha[j], pi[j],j+1, P0[j]); //create new bags(like argument list) for threads /*pbags[j]->setConsoleMutex(consolemutex);*/ threadFinishChecked[j] = 0; } for (int j = 0; j < threadNum; j++){ mastermutexes[j] = CreateMutex(NULL, 0, NULL); pbags[j]->setMasterMutex(mastermutexes[j]); } /*-------------Threads work begin---------------*/ for(int j = 0; j < threadNum; j++){ pThread[j] = (HANDLE)_beginthreadex( NULL, 0, &comp_wrapper, (void *) pbags[j], 0, &pthreadID[j] ); } //master program while (true){ Sleep(1000);//check the workers at every 1000 milliseconds printf("\n----master will now check on workers----\n"); fflush(stdout); for(int j = 0; j < threadNum; j++){ WaitForSingleObject(mastermutexes[j], INFINITE);//thread j wait for the master to get status jstatus = pbags[j]->getStatus(); ReleaseMutex(mastermutexes[j]);//release thread j if(jstatus == FINISHED){//if the status is FINISHED, we print out to tell user that the thread is finished. printf("Worker %d finished work.\n", j + 1); if (threadFinishChecked[j] == 0){//if it is the first time we find it finished, change value of variables finishedThreads += 1; threadFinishChecked[j] = 1; } /*if the number of finished threads equals to the total number of threads, we consider all the work finished and get out of the loop*/ if (finishedThreads == threadNum) { printf("All workers finished work.\n"); flag = 1; break; } } else{ printf("Worker %d is still working\n", j + 1); } } if (flag == 1) break; } /*---------------Threads exit and output result----------------*/ for(int j = 0; j < threadNum; j++){ WaitForSingleObject(pThread[j], INFINITE); printf("\n==============Result Output================\n"); printf("--> thread %d done\n", j); pbags[j] -> getPrintParameter(); printf("optimal value for trade sequencing = %g\n", pbags[j] -> getOptimalResult()); } printf("\n"); //free the space for(int j = 0; j < threadNum; j++){ delete pbags[j]; } free(pthreadID); free(pThread); free(alpha); free(pi); free(pbags); free(totalShareN); free(periodT); free(threadFinishChecked); free(P0); BACK: return retcode; }
void performCommandPLC() { UINT8 ui8NameStrDL[15]; UINT32 pos; UINT32 err = SUCCESS; UINT32 ui32dstDirPos; switch(ucHMI_addr) { case PLC_TIME: stime.tm_sec = uiHMIWord[PLC_TIME+5]; stime.tm_min = uiHMIWord[PLC_TIME+4]; stime.tm_hour = uiHMIWord[PLC_TIME+3]; stime.tm_mday = uiHMIWord[PLC_TIME+2]; stime.tm_mon = uiHMIWord[PLC_TIME+1]; stime.tm_year = uiHMIWord[PLC_TIME]-1900; timeSet(&stime); #if C_LEVI_DEBUG printf("time=%d/%d/%d %d:%d:%d ",uiHMIWord[PLC_TIME],uiHMIWord[PLC_TIME+1],uiHMIWord[PLC_TIME+2],uiHMIWord[PLC_TIME+3],uiHMIWord[PLC_TIME+4],uiHMIWord[PLC_TIME+5]); #endif if((uiHMIWord[PLC_TIME]!=2000)||(uiHMIWord[PLC_TIME+1]!=01)) ui8TimeFlag = 1; timeGet(&ptime); sio_psprintf(ui8NameStrDL, "%02d%02d%02dDC.CSV", (UINT32)(ptime->tm_year-100)%100, ptime->tm_mon,ptime->tm_mday); xdcfCurRootDirSet(ui8TermModule); //mp3_GetFileList(); pos = myFindFile(ui8NameStrDL); myGetSingleDayDL(pos, &ui32SuccessNum); break; case PLC_PARA_BUF: memcpy((UINT8 *)&curDevice, (UINT8 *)&uiHMIWord[PLC_PARA_BUF], C_PLC_PARA_NUM*sizeof(UINT16)); #if C_LEVI_DEBUG printf("device=%d, class=%d ",uiHMIWord[PLC_MODULE_NO],uiHMIWord[PLC_MODULE_NO+1]); #endif getModuleName(ui16CurModule, ui8TermModule); writePara(); //readPara(); break; case PLC_MODULE_NO: #if C_LEVI_DEBUG printf("device=%d, class=%d ",uiHMIWord[PLC_MODULE_NO],uiHMIWord[PLC_MODULE_NO+1]); #endif ui16CurModule = uiHMIWord[PLC_MODULE_NO]; ui16CurClass = uiHMIWord[PLC_MODULE_NO+1]; getModuleName(ui16CurModule, ui8TermModule); readPara(); writePara(); readParadelaytime(); writeParadelaytime(); break; case TESTSTEP0: test_step0=(~uiHMIWord[TESTSTEP0])&0xffff; test_step1=(~uiHMIWord[TESTSTEP1])&0xff; test_mode=(uiHMIWord[TestMode]&0x01); printf("test_step0=%d, test_step1=%d, test_mode=%d ",test_step0,test_step1,test_mode); break; case delay_time: memcpy((UINT8 *)&delay,(UINT8 *)&uiHMIWord[delay_time],C_delay_NUM*sizeof(UINT16)); //memcpy((UINT8 *)&uiHMIWord[delay_time], (UINT8 *)&delay, C_delay_NUM*sizeof(UINT16)); getModuleName(ui16CurModule, ui8TermModule); writeParadelaytime(); //readParadelaytime(); break; case PLC_BAKCMD: gpioConfig(GPIO_CARD_DET, 0); gpioSet(GPIO_CARD_DET, 1); if(gpioGet(GPIO_CARD_DET)==0) { err = xdcfActiveDevIdSet(DRIVE_SD); xdcfInit(imageDirNameStr, imageFileNameStr, 0); dirInit(); if(err==SUCCESS) { xdcfChange2Root(); vfsMkdir(ui8TermModule); xdcfCurRootDirSet(ui8TermModule); xdcfCurDirPosGet(&ui32dstDirPos); myXdcfDelFile(); xdcfActiveDevIdSet(DRIVE_NAND); xdcfCurRootDirSet(ui8TermModule); xdcfChange2Root(); vfsChdir(ui8TermModule); err |= myXdcfCopyDisk(ui32dstDirPos); if(uiHMIWord[PLC_BAKCMD]) { myXdcfDelFile(); } if(err==SUCCESS) uiHMIWord[PLC_BAKINFO] = 2; else uiHMIWord[PLC_BAKINFO] = 4; // ¶ÁSD¿¨´íÎó } else { uiHMIWord[PLC_BAKINFO] = 4; // ¶ÁSD¿¨´íÎó } } else { uiHMIWord[PLC_BAKINFO] = 3; // ûÓÐSD¿¨ } #if C_FILE_DEBUG printf("backup end "); #endif break; default: break; } }