#include #include #include #include #include #include void signalHandler(int); void getTime(char*); void addDot(char*); int cFlag = 0, alarmFlag = 0, childFinishedFlag = 0; int main() { char outputFileName[100], parentFileName[100], childFileName[100]; int parentPID = getpid(), childPID, fd[2]; char timeString[30]; const int MAXLINE = 4096; char line[MAXLINE]; //Set up signals signal(SIGINT, signalHandler); signal(SIGCHLD, signalHandler); signal(SIGALRM, signalHandler); //Populate timeString array with current time getTime(timeString); //Prompt user for output filename scanf("%100s", outputFileName); addDot(outputFileName); if(cFlag == 1) { strcpy(outputFileName, "Output."); } //Populate other filename strings strcpy(parentFileName, "ParentStatus."); strcat(parentFileName, timeString); strcpy(childFileName, "ChildStatus."); strcat(childFileName, timeString); //File pointers FILE *outputFile; FILE *parentLog; FILE *childLog; //Open outputfile if((outputFile = fopen(strcat(outputFileName,timeString), "w")) == NULL) { printf("Error opening output file.\n"); return 1; } else { //Write output file info here fprintf(outputFile, "Opened: %s\n", timeString); } //Create pipe if (pipe(fd) < 0) { printf("Pipe Error, program closing"); return 1; } //Fork returns the PID of the child process if ((childPID = fork()) < 0) { printf("Fork Error"); return 1; } //Parent process else if (childPID > 0) { //Open parent log file if((parentLog = fopen(parentFileName,"w")) == NULL) { printf("Error opening output file.\n"); return 1; } else { fprintf(parentLog, "Opened: %s\n", timeString); //Set standard output dup2(fd[1], STDOUT_FILENO); //Close the reading end of the pipe close(fd[0]); //Write output file info here fprintf(parentLog, "Parent Log\nPID: %i\n", parentPID); //Seed for random number srand((int)time(NULL)); int num; //Wait for child to finish while(childFinishedFlag == 0) { //Generate new number num = (int)(5.0 * rand() / (RAND_MAX + 1.0)); //Write to pipe write(STDOUT_FILENO, &num, 1); } //When child is finished, close parent log and output file getTime(timeString); fprintf(parentLog, "Parent finished at: %s", timeString); } } //Child process else { //Open child log file if((childLog = fopen(childFileName,"w")) == NULL) { printf("Error opening output file.\n"); return 1; } else { fprintf(childLog, "Opened: %s\n", timeString); //Set standard input dup2(fd[0], STDIN_FILENO); //Seed rand and set alarm srand((int)time(NULL)); int randTime = 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0)); alarm(randTime); int n1, n2, num1, num2; while(alarmFlag == 0) { //Get 2 random numbers n1 = read(STDIN_FILENO, &num1, 1); n2 = read(STDIN_FILENO, &num2, 1); //DEBUG: printf("Number: %i, %i\n",num1, num2); //If the first number is greater than 2, multiply, otherwise divide if(num1 > 2) { int result = num1 * num2; fprintf(childLog, "%i * %i = %i\n", num1, num2, result); } else { //int result = num1 / num2; //fprintf(childLog, "%i / %i = %i\n", num1, num2, result); } } //When alarm goes off close child log getTime(timeString); fprintf(childLog, "Child finished at: %s", timeString); fclose(childLog); } } fprintf(outputFile, "Output finished at: %s", timeString); fclose(parentLog); fclose(outputFile); return 0; } /* * signalHandler(int) - Handles the signals generated by the program */ void signalHandler(int signo) { //Ctrl-C Handler if(signo == SIGINT) { cFlag = 1; } //Signal when child is finished if(signo == SIGCHLD) { childFinishedFlag = 1; } //Signal when alarm goes off in child program if(signo == SIGALRM) { alarmFlag = 1; } } /* * getTime(char*) - Gets the current time and populates the string * passed as a parameter. */ void getTime(char *timeString) { time_t clock; struct tm *theTime; time(&clock); theTime = localtime(&clock); strftime(timeString,29,"%T",theTime); } /* * addDot(char*) - A support function. Simply adds a dot to the filename. */ void addDot(char *string) { strcat(string, "."); }