Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Jordan Lambert & Kevin Stys
- * CSC345-01
- * Project 2: Multi-threaded Programming
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <math.h>
- #include <string.h>
- #include <time.h>
- #define SIZE 256
- char *stringstorage[SIZE];
- int lines[SIZE];
- int numlines = 0;
- int tokcnt;
- int linecnt;
- char *parseval(char *line, int num){
- char *token;
- char *temp = strdup(line);
- token = strtok(temp, ",\n ");
- while(num > 0){
- token = strtok(NULL, ",\n ");
- num--;
- }
- return token;
- }
- void readfile(const char *filename, int *tokcnt, int *linecnt){
- *tokcnt = 0;
- *linecnt = 1;
- FILE* stream = fopen(filename, "r");
- char line[1024];
- while(fgets(line, 1024, stream)){
- char *temp = strdup(line);
- char *token;
- int curcnt = 0;
- lines[0] = 0;
- token = parseval(line, curcnt);
- while(token != NULL){
- // printf("%d: %s| ", count, token);
- stringstorage[*tokcnt] = strdup(token);
- curcnt++;
- *tokcnt = *tokcnt + 1;
- token = parseval(line, curcnt);
- }
- lines[*linecnt] = *tokcnt;
- *linecnt = *linecnt + 1;
- numlines++;
- }
- }
- void *task2(void *param){
- clock_t start, end;
- start = clock();
- char *filename = (char *)param;
- int numNums = 0;
- double average = -1.2345;
- double maximum = -1.2345;
- double minimum = -1.2345;
- double variance = -1.2345;
- int i;
- for(i = 0; i < tokcnt; i++){
- int num = atoi(stringstorage[i]);
- if(num != 0){
- if(maximum == -1.2345) maximum = num;
- else if(num > maximum) maximum = num;
- if(minimum == -1.2345) minimum = num;
- else if(num < minimum) minimum = num;
- if(average == -1.2345) average = num;
- else average += num;
- numNums++;
- }
- }
- average /= numNums;
- variance = 0;
- for(i = 0; i < tokcnt; i++){
- int num = atoi(stringstorage[i]);
- if(num != 0){
- variance += pow(num - average, 2);
- }
- }
- variance /= numNums;
- end = clock();
- double elapsedtime = (double)(end - start) / CLOCKS_PER_SEC;
- printf("=== T2 Completed ===\n");
- printf("T2 RESULT: File %s.csv: Max = %f, Min = %f, Avg = %f, Var = %f\n", filename, maximum, minimum, average, variance);
- printf("T2 RESULT: Total elapsed time: %f seconds\n", elapsedtime);
- printf("=== T2 report end ===\n");
- }
- int main(int argc, char **argv){
- pthread_t tid[3];
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- //pthread_attr_setschedpolicy(&attr, SCHED_RR);
- char *threadmode[2];
- char *priority_op[2];
- char *sched_op;
- /* parsing options */
- if(argc < 2){
- printf("Usage: ./main multi|single <task #> [priority <task #> <low|high>] [sched <RR|FIFO|OTHER>]\n");
- return 0;
- }
- else{
- for(int i = 1; i < argc; i++){
- /* handle mode */
- if(!strcmp(argv[i], "single") || !strcmp(argv[i], "multi")){
- threadmode[0] = argv[i];
- if(!strcmp(threadmode[0], "single")){
- if(atoi(argv[i+1]) >= 1 && atoi(argv[i+1]) <= 3){
- threadmode[1] = argv[i+1];
- }
- else{
- printf("Usage: ./main multi|single <task #> [priority <task #> <low|high>] [sched <RR|FIFO|OTHER>]\n");
- return 0;
- }
- }
- }
- /* handle scheduling policy option */
- if(!strcmp(argv[i], "sched")){
- sched_op = argv[i+1];
- if(strcmp(sched_op, "FIFO") && strcmp(sched_op, "OTHER") && strcmp(sched_op, "RR")){
- printf("Scheduling option usage: sched <RR|FIFO|OTHER>\n");
- return 1;
- }
- }
- /* handle priority option */
- if(!strcmp(argv[i], "priority")){
- priority_op[0] = argv[i+1];
- priority_op[1] = argv[i+2];
- if((atoi(priority_op[0]) < 1 || atoi(priority_op[0]) > 3) || (strcmp(priority_op[1], "low") && strcmp(priority_op[1], "high"))){
- printf("Priority option usage: priority <task #> <low|high>\n");
- return 1;
- }
- }
- }
- }
- /* /parsing options */
- char *filename = "test.csv";
- printf("Reading file.\n");
- readfile(filename, &tokcnt, &linecnt);
- printf("File read.\n");
- printf("Creating thread.\n");
- /* task 2 */
- pthread_create(&tid[1], &attr, task2, filename);
- pthread_join(tid[1], NULL);
- printf("Thread completed.\n");
- for(int i = 0; i < tokcnt; i++){
- //printf("%s\n", stringstorage[i]);
- free(stringstorage[i]);
- }
- //printf("Mode: %s\nScheduling: %s\nPriority: %s %s\n", threadmode, sched_op, priority_op[0], priority_op[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement