diff --git a/Sim02/sim02 b/Sim02/sim02 index 4f7a00d..9270528 100755 Binary files a/Sim02/sim02 and b/Sim02/sim02 differ diff --git a/Sim02/simtimer.c b/Sim02/simtimer.c index 8e04e54..c3fbd23 100644 --- a/Sim02/simtimer.c +++ b/Sim02/simtimer.c @@ -30,7 +30,7 @@ const char RADIX_POINT = '.'; const char SPACED = ' '; -void *runTimer( int milliSeconds ) +void runTimer( int milliSeconds ) { struct timeval startTime, endTime; int startSec, startUSec, endSec, endUSec; @@ -62,7 +62,7 @@ void *runTimer( int milliSeconds ) secDiff = ( endSec - startSec ) * 1000; timeDiff = secDiff + mSecDiff; } - return NULL; + // return NULL; } double accessTimer( int controlCode, char *timeStr ) diff --git a/Sim02/simtimer.h b/Sim02/simtimer.h index 4539c3b..fe0496a 100644 --- a/Sim02/simtimer.h +++ b/Sim02/simtimer.h @@ -35,7 +35,7 @@ extern const char SPACED; // Function Prototyp ///////////////////////////////////////////////////////// -void *runTimer( int milliSeconds ); +void runTimer( int milliSeconds ); // will run a timer for the amount of ms specified double accessTimer( int controlCode, char *timeStr ); // returns the actual time as a double AND as a time string diff --git a/Sim02/simulator.c b/Sim02/simulator.c index f60797f..ae0ba65 100644 --- a/Sim02/simulator.c +++ b/Sim02/simulator.c @@ -107,7 +107,7 @@ Device Input/Device: none Device Output/Device: none Dependencies: ??? */ -ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer) +ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer) { // initialize function/variables // need a working pointer for the metadata @@ -202,6 +202,7 @@ ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBl currentPCB.totalCycles = ioCycleCount + cpuCycleCount; // set the time left from (ioCycles * ioCycleRate) + (cpuCycles * procCyclesRate) // this gets the number of ms in the process. + timeLeftCount = (ioCycleCount * configPtr->ioCycleRate) + (cpuCycleCount * configPtr->procCycleRate); currentPCB.timeLeft = timeLeftCount; // increment the PID counter processId = processId + 1; @@ -232,7 +233,7 @@ ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBl inOutArg = wkgMetaPtr->inOutArg; } - // return null (temp) (should return the headpointer) + // return headPointer return headPointer; } @@ -250,24 +251,91 @@ Dependencies: sprintf, ??? */ void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd) { - FILE *fileWritePtr; - fileWritePtr = fopen(configPtr->logToFileName, "w"); - if(fileWritePtr != NULL) + if(configPtr->logToCode == LOGTO_BOTH_CODE) + { + // + FILE *fileWritePtr; + fileWritePtr = fopen(configPtr->logToFileName, "w"); + if (fileWritePtr != NULL) + { + // + fprintf(fileWritePtr, "%s", logStr); + // fputs(fileWritePtr, logStr); + fprintf(stdout, "%s", logStr); + } + // no return, void + // close the file, if at the end. + if (isEnd) + { + // + fclose(fileWritePtr); + } + // fclose(fileWritePtr); + } + else if(configPtr->logToCode == LOGTO_FILE_CODE) + { + // + printf("\nSimulator will output to a file.\n"); + FILE *fileWritePtr; + fileWritePtr = fopen(configPtr->logToFileName, "w"); + if (fileWritePtr != NULL) + { + // + fprintf(fileWritePtr, "%s", logStr); + // fputs(fileWritePtr, logStr); + } + // no return, void + // close the file, if at the end. + if (isEnd) + { + // + fclose(fileWritePtr); + } + // fclose(fileWritePtr); + } + else { // - fprintf(fileWritePtr, "%s", logStr); fprintf(stdout, "%s", logStr); } - // no return, void - // close the file, if at the end. - // if(isEnd) - // { - // // - // fclose(fileWritePtr); - // } - fclose(fileWritePtr); } +/* +Name: runCPU +Process: runs the CPU operations for however long it takes +Function Input/Parameters: ??? +Function Output/Parameters: none +Function Output/Returned: none +Device Input/Device: none +Device Output/Device: none +Dependencies: ??? +*/ +void runCPU(); + +/* +Name: runIO +Process: runs the IO operations for however long it takes +Function Input/Parameters: ??? +Function Output/Parameters: none +Function Output/Returned: none +Device Input/Device: none +Device Output/Device: none +Dependencies: ??? +*/ +void runIO(); + +/* +Name: runProcess +Process: runs the process that was selected +Function Input/Parameters: ??? +Function Output/Parameters: none +Function Output/Returned: none +Device Input/Device: none +Device Output/Device: none +Dependencies: ??? +*/ +void runProcess(); + /* Name: runSim Process: primary simulation driver @@ -291,10 +359,11 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) // string variable for nextCode, temp unused // char *nextCode; // string variable for the timer stuff - char *simulatorOutput = "Simulator Start\n"; + char simulatorOutput[MAX_STR_LEN]; + // accessTimer(ZERO_TIMER, NULL); // Operating System start timer goes here, log it. - // sprintf(simulatorOutput, "Simulator start\n"); + sprintf(simulatorOutput, "%f, OS: Simulator start\n", accessTimer(LAP_TIMER, NULL)); logOutput(configPtr, simulatorOutput, false); // int variable for process to run @@ -302,7 +371,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) // create the process list with createProcessList, store the pointed to address pcbList = createProcessList(); // fill the process list given the metadata - pcbList = fillProcessList(metaDataMstrPtr, pcbList); + pcbList = fillProcessList(configPtr, metaDataMstrPtr, pcbList); pcbPtr = pcbList->headPtr; // create temp strings char *command; @@ -319,26 +388,14 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) pcbPtr = pcbPtr->nextPtr; } pcbPtr = pcbList->headPtr; + + pthread_t cpuThread; + pthread_create(&cpuThread, NULL, (void *)&simThread, (void *)totalPCBTime); + pthread_join(cpuThread, NULL); + // set the current process id to the first scheduled process currentPid = scheduleProcess(pcbList, configPtr); - // function holds master loop, loops until sys end opcode found OR wkgPtrMaster nextNode != NULL - // while(wkgPtrMaster->nextNode != NULL && !(compareString(argument, "end") == 0 && compareString(command, "sys") == 0)) - // { - // currentPid = scheduleProcess(pcbList, configPtr); - // // will have a loop that goes from app start to app end for a PCB - // // (checks to see if the next instruction is NOT app end), using getNextProcess? - // // this will getNextOpCode - // // this will checkForIO and run the IO operations, outputting to FILE * and/or monitor - // // checkForIO will need the FILE * passed, will need timer access, and ??? - // // this will checkForCPU and run the CPU operations, outputting to FILE * and/or monitor - // // checkForCPU will need the FILE * passed, will need timer access, and ??? - // // move the wkgPtrMaster to the next metadata block - // wkgPtrMaster = wkgPtrMaster->nextNode; - // // set the command and argument to the current cmd and arg - // command = wkgPtrMaster->command; - // argument = wkgPtrMaster->strArg1; - // } // loop until current process is hit while(pcbPtr != NULL) { @@ -346,10 +403,14 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) if(pcbPtr->uniquePid == currentPid) { // - printf("Process %d running", currentPid); + // printf("Process %d running\n", currentPid); + // needs to set from READY to RUNNING + // when finished, set to terminated pcbPtr->process_state = PROCESS_STATE_TERMINATED; + } pcbPtr = pcbPtr->nextPtr; + currentPid = scheduleProcess(pcbList, configPtr); } // @@ -357,6 +418,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) destroyProcessList(pcbList); // once complete, log the output to FILE if required logOutput(configPtr, "", true); + pthread_exit(NULL); // void function, no return. } @@ -376,7 +438,7 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt ProcessControlBlock *wkgPtr; int returnPid = -1; wkgPtr = headPointer->headPtr; - char *loggingStr; + char loggingStr[MAX_STR_LEN]; // switch case for each scheduling algorithm switch (configPtr->cpuSchedCode) { @@ -391,10 +453,15 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt while(wkgPtr != NULL) { // - wkgPtr->process_state = PROCESS_STATE_READY; - //printf("Process %d set to READY from NEW\n", wkgPtr->uniquePid); - sprintf(loggingStr, "Process %d set to READY state from NEW state\n", wkgPtr->uniquePid); - logOutput(configPtr, loggingStr, false); + if(wkgPtr->process_state != PROCESS_STATE_READY && wkgPtr->process_state != PROCESS_STATE_TERMINATED) + { + // + wkgPtr->process_state = PROCESS_STATE_READY; + // printf("Process %d set to READY from NEW\n", wkgPtr->uniquePid); + sprintf(loggingStr, "%f, OS: Process %d set to READY state from NEW state\n", accessTimer(LAP_TIMER, NULL), wkgPtr->uniquePid); + logOutput(configPtr, loggingStr, false); + } + wkgPtr = wkgPtr->nextPtr; } @@ -406,7 +473,7 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt if (wkgPtr->process_state == PROCESS_STATE_READY) { //printf("Process %d Selected with %.0f ms remaining\n", wkgPtr->uniquePid, wkgPtr->timeLeft); - sprintf(loggingStr, "Process %d selected with %.0f ms remaining\n", wkgPtr->uniquePid, wkgPtr->timeLeft); + sprintf(loggingStr, "%f, OS: Process %d selected with %.0f ms remaining\n", accessTimer(LAP_TIMER, NULL), wkgPtr->uniquePid, wkgPtr->timeLeft); logOutput(configPtr, loggingStr, false); returnPid = wkgPtr->uniquePid; } @@ -430,6 +497,26 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt // return returnPid return returnPid; } + +/* +Name: simThread +Process: wrapper functions for threading in the simulator +Function Input/Parameters: milliseconds (int) +Function Output/Parameters: none +Function Output/Returned: none +Device Input/Device: none +Device Output/Device: none +Dependencies: ??? +*/ +void *simThread(int milSec) +{ + // + // int *milSecPtr = (int *)milSec; + // printf("Running Timer for %d ms, %d sec\n", milSec, milSec / 1000); + runTimer(milSec); + // printf("Ran Timer\n"); + pthread_exit(NULL); +} /* Current Note to programmer: do the scheduler, the runIO and runCPU functions, and then the FILE IO functions this is all that is left so far, hopefully not too bad. diff --git a/Sim02/simulator.h b/Sim02/simulator.h index 69bdff0..167145b 100644 --- a/Sim02/simulator.h +++ b/Sim02/simulator.h @@ -141,7 +141,7 @@ Device Input/Device: none Device Output/Device: none Dependencies: ??? */ -ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer); +ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer); /* Name: logOutput @@ -157,6 +157,30 @@ Dependencies: sprintf, ??? */ void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd); +/* +Name: runCPU +Process: runs the CPU operations for however long it takes +Function Input/Parameters: ??? +Function Output/Parameters: none +Function Output/Returned: none +Device Input/Device: none +Device Output/Device: none +Dependencies: ??? +*/ +void runCPU(); + +/* +Name: runIO +Process: runs the IO operations for however long it takes +Function Input/Parameters: ??? +Function Output/Parameters: none +Function Output/Returned: none +Device Input/Device: none +Device Output/Device: none +Dependencies: ??? +*/ +void runIO(); + /* Name: runSim Process: primary simulation driver @@ -182,4 +206,16 @@ Dependencies: ??? */ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPtr); +/* +Name: simThread +Process: wrapper functions for threading in the simulator +Function Input/Parameters: milliseconds (int) +Function Output/Parameters: none +Function Output/Returned: none +Device Input/Device: none +Device Output/Device: none +Dependencies: ??? +*/ +void *simThread(int milSec); + #endif // SIMULATORX \ No newline at end of file