int calculatePi(long iter){ long i; long iterations = iter; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; /* Calculate pi using statistical methode across all iterations*/ for(i=0; i<iterations; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } /* Finish calculation */ pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; /* Print result */ fprintf(stdout, "pi = %f\n", piCalc); return 0; }
int main(int argc, char* argv[]){ long i; long iterations; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; /* Process program arguments to select iterations */ /* Set default iterations if not supplied */ if(argc < 2){ iterations = DEFAULT_ITERATIONS; } /* Set iterations if supplied */ else{ iterations = atol(argv[1]); if(iterations < 1){ fprintf(stderr, "Bad iterations value\n"); exit(EXIT_FAILURE); } } /* Calculate pi using statistical methode across all iterations*/ for(i=0; i<iterations; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } /* Finish calculation */ pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; /* Print result */ fprintf(stdout, "pi = %f\n", piCalc); return 0; }
double calcPi(long iter) { long i; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; /* Calculate pi using statistical methode across all iterations*/ for(i=0; i<iter; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; return piCalc; }
int main(int argc, char* argv[]) { // Local variable definitions int rv, iteration;s int inputFD; int outputFD; char inputFilename[MAXFILENAMELENGTH]; char outputFilename[MAXFILENAMELENGTH]; char outputFilenameBase[MAXFILENAMELENGTH]; ssize_t transfersize = 0; ssize_t blocksize = 0; char* transferBuffer = NULL; ssize_t buffersize; ssize_t bytesRead = 0; ssize_t totalBytesRead = 0; int totalReads = 0; ssize_t bytesWritten = 0; ssize_t totalBytesWritten = 0; int totalWrites = 0; int inputFileResets = 0; // Processing input arguments // Setting the default number of iterations if(argc < 2) iterations = LIGHT; // Use the default scheudling policy if policy is not given if(argc < 3) policy = SCHED_OTHER; // Set the number of iterations, if given if(!strcmp(argv[2],light)) iterations = LIGHT; if(!strcmp(argv[2],medium)) iterations = MEDIUM; if(!strcmp(argv[2],heavy)) iterations = HEAVY; if(iterations < 1){ fprintf(stderr, "Bad iterations value\n"); exit(EXIT_FAILURE); } // Parsing the desired scheduler if(argc > 2){ if(!strcmp(argv[2], "SCHED_OTHER")){ policy = SCHED_OTHER; } else if(!strcmp(argv[2], "SCHED_FIFO")) policy = SCHED_FIFO; else if(!strcmp(argv[2], "SCHED_RR")) policy = SCHED_RR; else{ fprintf(stderr, "Unhandeled scheduling policy\n"); exit(EXIT_FAILURE); } } // Set supplied input filename or default if not supplied if(argc < 4){ if(strnlen(DEFAULT_INPUTFILENAME, MAXFILENAMELENGTH) >= MAXFILENAMELENGTH){ fprintf(stderr, "Default input filename too long\n"); exit(EXIT_FAILURE); } strncpy(inputFilename, DEFAULT_INPUTFILENAME, MAXFILENAMELENGTH); } else{ if(strnlen(argv[3], MAXFILENAMELENGTH) >= MAXFILENAMELENGTH){ fprintf(stderr, "Input filename too long\n"); exit(EXIT_FAILURE); } strncpy(inputFilename, argv[3], MAXFILENAMELENGTH); } // Set supplied output filename base or default if not supplied if(argc < 5){ if(strnlen(DEFAULT_OUTPUTFILENAMEBASE, MAXFILENAMELENGTH) >= MAXFILENAMELENGTH){ fprintf(stderr, "Default output filename base too long\n"); exit(EXIT_FAILURE); } strncpy(outputFilenameBase, DEFAULT_OUTPUTFILENAMEBASE, MAXFILENAMELENGTH); } else{ if(strnlen(argv[4], MAXFILENAMELENGTH) >= MAXFILENAMELENGTH){ fprintf(stderr, "Output filename base is too long\n"); exit(EXIT_FAILURE); } strncpy(outputFilenameBase, argv[4], MAXFILENAMELENGTH); } // Set process to max prioty for given scheduler param.sched_priority = sched_get_priority_max(policy); // Set new scheduler policy fprintf(stdout, "Current Scheduling Policy: %d\n", sched_getscheduler(0)); fprintf(stdout, "Setting Scheduling Policy to: %d\n", policy); if(sched_setscheduler(0, policy, ¶m)){ perror("Error setting scheduler policy"); exit(EXIT_FAILURE); } fprintf(stdout, "New Scheduling Policy: %d\n", sched_getscheduler(0)); // Begin testing fprintf(stdout, "Forking %d children processes\n", iterations); for(i = 0; i < iterations; i++){ if((pid = fork()) == -1){ fprintf(stderr, "Child process %d could not be forked, please try again\n", i); exit(EXIT_FAILURE); } else if(pid == 0){ fprintf(stdout, "Process %d forked PID %d\n", i, pid); // Calculate pi using statistical methode across all iterations for(i=0; i<iterations; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } // Finishing pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; // Display the result fprintf(stdout, "pi = %f\n", piCalc); // Confirm blocksize is multiple of and less than transfersize if(blocksize > transfersize){ fprintf(stderr, "blocksize can not exceed transfersize\n"); exit(EXIT_FAILURE); } if(transfersize % blocksize){ fprintf(stderr, "blocksize must be multiple of transfersize\n"); exit(EXIT_FAILURE); } // Allocate buffer space buffersize = blocksize; if(!(transferBuffer = malloc(buffersize*sizeof(*transferBuffer)))){ perror("Failed to allocate transfer buffer"); exit(EXIT_FAILURE); } // Open Input File Descriptor in Read Only mode if((inputFD = open(inputFilename, O_RDONLY | O_SYNC)) < 0){ perror("Failed to open input file"); exit(EXIT_FAILURE); } // Open Output File Descriptor in Write Only mode with standard permissions rv = snprintf(outputFilename, MAXFILENAMELENGTH, "%s-%d", outputFilenameBase, getpid()); if(rv > MAXFILENAMELENGTH){ fprintf(stderr, "Output filenmae length exceeds limit of %d characters.\n", MAXFILENAMELENGTH); exit(EXIT_FAILURE); } else if(rv < 0){ perror("Failed to generate output filename"); exit(EXIT_FAILURE); } if((outputFD = open(outputFilename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) < 0){ perror("Failed to open output file"); exit(EXIT_FAILURE); } // Print Status fprintf(stdout, "Reading from %s and writing to %s\n", inputFilename, outputFilename); // Read from input file and write to output file do{ // Read transfersize bytes from input file bytesRead = read(inputFD, transferBuffer, buffersize); if(bytesRead < 0){ perror("Error reading input file"); exit(EXIT_FAILURE); } else{ totalBytesRead += bytesRead; totalReads++; } // If all bytes were read, write to output file if(bytesRead == blocksize){ bytesWritten = write(outputFD, transferBuffer, bytesRead); if(bytesWritten < 0){ perror("Error writing output file"); exit(EXIT_FAILURE); } else{ totalBytesWritten += bytesWritten; totalWrites++; } } // Otherwise assume we have reached the end of the input file and reset else{ if(lseek(inputFD, 0, SEEK_SET)){ perror("Error resetting to beginning of file"); exit(EXIT_FAILURE); } inputFileResets++; } } while(totalBytesWritten < transfersize); // Output some possibly helpfull info to make it seem like we were doing stuff fprintf(stdout, "Read: %zd bytes in %d reads\n", totalBytesRead, totalReads); fprintf(stdout, "Written: %zd bytes in %d writes\n", totalBytesWritten, totalWrites); fprintf(stdout, "Read input file in %d pass%s\n", (inputFileResets + 1), (inputFileResets ? "es" : "")); fprintf(stdout, "Processed %zd bytes in blocks of %zd bytes\n", transfersize, blocksize); // Free Buffer free(transferBuffer); // Close Output File Descriptor if(close(outputFD)){ perror("Failed to close output file"); exit(EXIT_FAILURE); } // Close Input File Descriptor if(close(inputFD)){ perror("Failed to close input file"); exit(EXIT_FAILURE); } } // Exiting exit(0); } }
int main(int argc, char* argv[]){ long i; long iterations = DEFAULT_ITERATIONS; struct sched_param param; int policy, num_processes, status; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; pid_t pid, wpid; int j = 0; int rv; int inputFD; int outputFD; char inputFilename[MAXFILENAMELENGTH]; char outputFilename[MAXFILENAMELENGTH]; char outputFilenameBase[MAXFILENAMELENGTH]; ssize_t transfersize = DEFAULT_TRANSFERSIZE; ssize_t blocksize = DEFAULT_BLOCKSIZE; char* transferBuffer = NULL; ssize_t buffersize; ssize_t bytesRead = 0; ssize_t totalBytesRead = 0; int totalReads = 0; ssize_t bytesWritten = 0; ssize_t totalBytesWritten = 0; int totalWrites = 0; int inputFileResets = 0; /* Set input and output filename and size */ strncpy(inputFilename, DEFAULT_INPUTFILENAME, MAXFILENAMELENGTH); strncpy(outputFilenameBase, DEFAULT_OUTPUTFILENAMEBASE, MAXFILENAMELENGTH); /* Set default policy if not supplied */ if(argc < 2){ policy = SCHED_OTHER; } /* Set default number of processes */ if(argc < 3){ num_processes = LOW; } /* Set policy if supplied */ if(argc > 1){ if(!strcmp(argv[1], "SCHED_OTHER")){ policy = SCHED_OTHER; } else if(!strcmp(argv[1], "SCHED_FIFO")){ policy = SCHED_FIFO; } else if(!strcmp(argv[1], "SCHED_RR")){ policy = SCHED_RR; } else{ fprintf(stderr, "Unhandeled scheduling policy\n"); exit(EXIT_FAILURE); } } /* Set # Of Processes */ if(argc > 2){ if(!strcmp(argv[2], "LOW")){ num_processes = LOW; } else if(!strcmp(argv[2], "MEDIUM")){ num_processes = MEDIUM; } else if(!strcmp(argv[2], "HIGH")){ num_processes = HIGH; } else{ fprintf(stderr, "Unhandeled number of processes\n"); exit(EXIT_FAILURE); } } /* Set process to max prioty for given scheduler */ param.sched_priority = sched_get_priority_max(policy); /* Set new scheduler policy */ fprintf(stdout, "Current Scheduling Policy: %d\n", sched_getscheduler(0)); fprintf(stdout, "Setting Scheduling Policy to: %d\n", policy); if(sched_setscheduler(0, policy, ¶m)){ perror("Error setting scheduler policy"); exit(EXIT_FAILURE); } fprintf(stdout, "New Scheduling Policy: %d\n", sched_getscheduler(0)); /* Fork number of processes specified */ printf("Number of processes to be forked %d \n", num_processes); for(i = 0; i < num_processes; i++){ if((pid = fork())==-1){ fprintf(stderr, "Error Forking Child Process"); exit(EXIT_FAILURE); /*Fork Failed*/ } if(pid == 0){ /* Child Process */ /* Calculate pi using statistical methode across all iterations*/ for(i=0; i<iterations; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } /* Finish calculation */ pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; /* Print result */ fprintf(stdout, "pi = %f\n", piCalc); /* Confirm blocksize is multiple of and less than transfersize*/ if(blocksize > transfersize){ fprintf(stderr, "blocksize can not exceed transfersize\n"); exit(EXIT_FAILURE); } if(transfersize % blocksize){ fprintf(stderr, "blocksize must be multiple of transfersize\n"); exit(EXIT_FAILURE); } /* Allocate buffer space */ buffersize = blocksize; if(!(transferBuffer = malloc(buffersize*sizeof(*transferBuffer)))){ perror("Failed to allocate transfer buffer"); exit(EXIT_FAILURE); } /* Open Input File Descriptor in Read Only mode */ if((inputFD = open(inputFilename, O_RDONLY | O_SYNC)) < 0){ perror("Failed to open input file"); exit(EXIT_FAILURE); } /* Open Output File Descriptor in Write Only mode with standard permissions*/ rv = snprintf(outputFilename, MAXFILENAMELENGTH, "%s-%d", outputFilenameBase, getpid()); if(rv > MAXFILENAMELENGTH){ fprintf(stderr, "Output filenmae length exceeds limit of %d characters.\n", MAXFILENAMELENGTH); exit(EXIT_FAILURE); } else if(rv < 0){ perror("Failed to generate output filename"); exit(EXIT_FAILURE); } if((outputFD = open(outputFilename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) < 0){ perror("Failed to open output file"); exit(EXIT_FAILURE); } /* Print Status */ fprintf(stdout, "Reading from %s and writing to %s\n", inputFilename, outputFilename); /* Read from input file and write to output file*/ do{ /* Read transfersize bytes from input file*/ bytesRead = read(inputFD, transferBuffer, buffersize); if(bytesRead < 0){ perror("Error reading input file"); exit(EXIT_FAILURE); } else{ totalBytesRead += bytesRead; totalReads++; } /* If all bytes were read, write to output file*/ if(bytesRead == blocksize){ bytesWritten = write(outputFD, transferBuffer, bytesRead); if(bytesWritten < 0){ perror("Error writing output file"); exit(EXIT_FAILURE); } else{ totalBytesWritten += bytesWritten; totalWrites++; } } /* Otherwise assume we have reached the end of the input file and reset */ else{ if(lseek(inputFD, 0, SEEK_SET)){ perror("Error resetting to beginning of file"); exit(EXIT_FAILURE); } inputFileResets++; } }while(totalBytesWritten < transfersize); /* Output some possibly helpfull info to make it seem like we were doing stuff */ fprintf(stdout, "Read: %zd bytes in %d reads\n", totalBytesRead, totalReads); fprintf(stdout, "Written: %zd bytes in %d writes\n", totalBytesWritten, totalWrites); fprintf(stdout, "Read input file in %d pass%s\n", (inputFileResets + 1), (inputFileResets ? "es" : "")); fprintf(stdout, "Processed %zd bytes in blocks of %zd bytes\n", transfersize, blocksize); /* Free Buffer */ free(transferBuffer); /* Close Output File Descriptor */ if(close(outputFD)){ perror("Failed to close output file"); exit(EXIT_FAILURE); } /* Close Input File Descriptor */ if(close(inputFD)){ perror("Failed to close input file"); exit(EXIT_FAILURE); } exit(0); } } /* Parent Process Waits For All Of the children to terminate */ while((wpid = wait(&status)) > 0){ if(WIFEXITED(status)){ /*Process terminated normally */ printf("Exit status of child process %d was normal \n", wpid); j++; } } printf("Total # of Processes terminated was %d\n", j); return EXIT_SUCCESS; }
int main(int argc, char* argv[]){ long i; int j; int numProcesses; long iterations; struct sched_param param; int policy; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; pid_t pid; /* Process program arguments to select iterations and policy */ /* Set default iterations if not supplied */ if(argc < 2){ iterations = DEFAULT_ITERATIONS; } /* Set default policy if not supplied */ if(argc < 3){ policy = SCHED_OTHER; } /* Set default number of forks if not supplied */ if(argc < 4){ numProcesses = DEFAULT_FORKS; } /* Set iterations if supplied */ if(argc > 1){ iterations = atol(argv[1]); if(iterations < 1){ fprintf(stderr, "Bad iterations value\n"); exit(EXIT_FAILURE); } } /* Set policy if supplied */ if(argc > 2){ if(!strcmp(argv[2], "SCHED_OTHER")){ policy = SCHED_OTHER; } else if(!strcmp(argv[2], "SCHED_FIFO")){ policy = SCHED_FIFO; } else if(!strcmp(argv[2], "SCHED_RR")){ policy = SCHED_RR; } else{ fprintf(stderr, "Un-handled scheduling policy\n"); exit(EXIT_FAILURE); } } /* Set number of processes if supplied */ if(argc > 3){ numProcesses = atol(argv[3]); } /* Set process to max priority for given scheduler */ param.sched_priority = sched_get_priority_max(policy); /* Set new scheduler policy */ fprintf(stdout, "Current Scheduling Policy: %d\n", sched_getscheduler(0)); fprintf(stdout, "Setting Scheduling Policy to: %d\n", policy); if(sched_setscheduler(0, policy, ¶m)){ perror("Error setting scheduler policy"); exit(EXIT_FAILURE); } fprintf(stdout, "New Scheduling Policy: %d\n", sched_getscheduler(0)); for(j=0; j<numProcesses; j++){ pid = fork(); if(pid == 0){ /* Calculate pi using statistical methode across all iterations*/ for(i=0; i<iterations; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } /* Finish calculation */ pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; /* Print result */ fprintf(stdout, "pi = %f\n", piCalc); exit(EXIT_SUCCESS); } else if(pid > 0) { printf("Forked child %d \n", pid); } else { fprintf(stderr, "Forking failed"); exit(EXIT_FAILURE); } } for(j=0; j<numProcesses; j++){ wait(NULL); } return 0; }
int main(int argc, char* argv[]) { long i; long iterations; struct sched_param param; int policy; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; int bat, j; //variables for for loops for forking int status; // variable used for wait /* Process program arguments to select iterations and policy */ /* Set default iterations if not supplied */ if (argc < 2) { iterations = DEFAULT_ITERATIONS; } /* Set default policy if not supplied */ if (argc < 3) { policy = SCHED_OTHER; } /* Set iterations if supplied */ if (argc > 1) { iterations = atol(argv[1]); if (iterations < 1) { fprintf(stderr, "Bad iterations value\n"); exit(EXIT_FAILURE); } } /* Set policy if supplied */ if (argc > 2) { if (!strcmp(argv[2], "SCHED_OTHER")) { policy = SCHED_OTHER; } else if (!strcmp(argv[2], "SCHED_FIFO")) { policy = SCHED_FIFO; } else if (!strcmp(argv[2], "SCHED_RR")) { policy = SCHED_RR; } else { fprintf(stderr, "Unhandeled scheduling policy\n"); exit(EXIT_FAILURE); } } /* Set process to max prioty for given scheduler */ param.sched_priority = sched_get_priority_max(policy); /* Set new scheduler policy */ fprintf(stdout, "Current Scheduling Policy: %d\n", sched_getscheduler(0)); fprintf(stdout, "Setting Scheduling Policy to: %d\n", policy); if (sched_setscheduler(0, policy, ¶m)) { perror("Error setting scheduler policy"); exit(EXIT_FAILURE); } fprintf(stdout, "New Scheduling Policy: %d\n", sched_getscheduler(0)); /* Calculate pi using statistical methode across all iterations*/ //Begin forking for (bat = 0; bat < N; bat++) { pid_t c1 = fork(); //Error handling if (c1 == -1) { perror("unable to fork first child"); exit(1); } //Child else if (c1 == 0) { printf("test"); for (i=0; i<iterations; i++) { x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if (zeroDist(x, y) < RADIUS) { inCircle++; } inSquare++; } /* Finish calculation */ pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; /* Print result */ fprintf(stdout, "pi = %f\n", piCalc); exit(0); } } //must wait for every time you fork for (j = 0; j < N; j++) { wait(&status); } return 0; }
int main(int argc, char* argv[]){ char *c = argv[argc - 1]; int forks = atoi(c); int number, pid; int pids[forks]; for (number=1;number<= forks;number++){ pid = fork(); if (pid == 0){ printf("BREAKING OUT %d\n",number ); break; } else { pids[number-1] = pid; } } // Fork Implementation if (pid == 0){ long i; long iterations; struct sched_param param; int policy; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; // Process program arguments to select iterations and policy if(argc < 2){ iterations = DEFAULT_ITERATIONS; } // Set default policy if not supplied if(argc < 3){ policy = SCHED_OTHER; } // Set iterations if supplied if(argc > 1){ iterations = atol(argv[1]); if(iterations < 1){ fprintf(stderr, "Bad iterations value\n"); exit(EXIT_FAILURE); } } // Set policy if supplied if(argc > 2){ if(!strcmp(argv[2], "SCHED_OTHER")){ policy = SCHED_OTHER; } else if(!strcmp(argv[2], "SCHED_FIFO")){ policy = SCHED_FIFO; } else if(!strcmp(argv[2], "SCHED_RR")){ policy = SCHED_RR; } else{ fprintf(stderr, "Unhandeled scheduling policy\n"); exit(EXIT_FAILURE); } } // Set Process to Max Prioty for given Scheduler param.sched_priority = sched_get_priority_max(policy); // Scheduler Policies: fprintf(stdout, "Current Scheduling Policy: %d\n", sched_getscheduler(0)); fprintf(stdout, "Setting Scheduling Policy to: %d\n", policy); if(sched_setscheduler(0, policy, ¶m)){ perror("Error setting scheduler policy"); exit(EXIT_FAILURE); } fprintf(stdout, "New Scheduling Policy: %d\n", sched_getscheduler(0)); // Calculate Pi using statistical methode across all iterations for(i=0; i<iterations; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; fprintf(stdout, "Pi = %f\n", piCalc); fflush(stdout); } else { int status; int i = 0; for(i = 0; i < forks ; i++) { pid = pids[i]; do{ pid = waitpid(pid, &status, WNOHANG); if (WIFEXITED(status)) { //Creates too much noise //printf("Child Exited. Status: %d\n", WEXITSTATUS(status)); } } while(pid == 0); } } return 0; }
int main(int argc, char* argv[]){ long i; long iterations = DEFAULT_ITERATIONS; struct sched_param param; int policy, num_processes, status; double x, y; double inCircle = 0.0; double inSquare = 0.0; double pCircle = 0.0; double piCalc = 0.0; pid_t pid, wpid; int j = 0; /* Set default policy if not supplied */ if(argc < 2){ policy = SCHED_OTHER; } /* Set default number of processes */ if(argc < 3){ num_processes = LOW; } /* Set policy if supplied */ if(argc > 1){ if(!strcmp(argv[1], "SCHED_OTHER")){ policy = SCHED_OTHER; } else if(!strcmp(argv[1], "SCHED_FIFO")){ policy = SCHED_FIFO; } else if(!strcmp(argv[1], "SCHED_RR")){ policy = SCHED_RR; } else{ fprintf(stderr, "Unhandeled scheduling policy\n"); exit(EXIT_FAILURE); } } /* Set # Of Processes */ if(argc > 2){ if(!strcmp(argv[2], "LOW")){ num_processes = LOW; } else if(!strcmp(argv[2], "MEDIUM")){ num_processes = MEDIUM; } else if(!strcmp(argv[2], "HIGH")){ num_processes = HIGH; } else{ fprintf(stderr, "Unhandeled number of processes\n"); exit(EXIT_FAILURE); } } /* Set process to max prioty for given scheduler */ param.sched_priority = sched_get_priority_max(policy); /* Set new scheduler policy */ fprintf(stdout, "Current Scheduling Policy: %d\n", sched_getscheduler(0)); fprintf(stdout, "Setting Scheduling Policy to: %d\n", policy); if(sched_setscheduler(0, policy, ¶m)){ perror("Error setting scheduler policy"); exit(EXIT_FAILURE); } fprintf(stdout, "New Scheduling Policy: %d\n", sched_getscheduler(0)); /* Fork number of processes specified */ printf("Number of processes to be forked %d \n", num_processes); for(i = 0; i < num_processes; i++){ if((pid = fork())==-1){ fprintf(stderr, "Error Forking Child Process"); exit(EXIT_FAILURE); /*Fork Failed*/ } if(pid == 0){ /* Child Process */ /* Calculate pi using statistical methode across all iterations*/ for(i=0; i<iterations; i++){ x = (random() % (RADIUS * 2)) - RADIUS; y = (random() % (RADIUS * 2)) - RADIUS; if(zeroDist(x,y) < RADIUS){ inCircle++; } inSquare++; } /* Finish calculation */ pCircle = inCircle/inSquare; piCalc = pCircle * 4.0; /* Print result */ fprintf(stdout, "pi = %f\n", piCalc); exit(0); } } /* Parent Process Waits For All Of the children to terminate */ while((wpid = wait(&status)) > 0){ if(WIFEXITED(status)){ /*Process terminated normally */ printf("Exit status of child process %d was normal \n", wpid); j++; } } printf("Total # of Processes terminated was %d\n", j); return EXIT_SUCCESS; }