Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. /* Jordan Lambert & Kevin Stys
  2. * CSC345-01
  3. * Project 2: Multi-threaded Programming
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <pthread.h>
  9. #include <unistd.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #define SIZE 256
  14.  
  15. char *stringstorage[SIZE];
  16. int lines[SIZE];
  17. int numlines = 0;
  18. int tokcnt;
  19. int linecnt;
  20.  
  21. char *parseval(char *line, int num){
  22. char *token;
  23. char *temp = strdup(line);
  24. token = strtok(temp, ",\n ");
  25. while(num > 0){
  26. token = strtok(NULL, ",\n ");
  27. num--;
  28. }
  29. return token;
  30. }
  31.  
  32. void readfile(const char *filename, int *tokcnt, int *linecnt){
  33. *tokcnt = 0;
  34. *linecnt = 1;
  35. FILE* stream = fopen(filename, "r");
  36.  
  37. char line[1024];
  38. while(fgets(line, 1024, stream)){
  39. char *temp = strdup(line);
  40. char *token;
  41. int curcnt = 0;
  42. lines[0] = 0;
  43. token = parseval(line, curcnt);
  44. while(token != NULL){
  45. // printf("%d: %s| ", count, token);
  46. stringstorage[*tokcnt] = strdup(token);
  47. curcnt++;
  48. *tokcnt = *tokcnt + 1;
  49. token = parseval(line, curcnt);
  50. }
  51. lines[*linecnt] = *tokcnt;
  52. *linecnt = *linecnt + 1;
  53. numlines++;
  54. }
  55. }
  56.  
  57. void *task2(void *param){
  58. clock_t start, end;
  59. start = clock();
  60. char *filename = (char *)param;
  61. int numNums = 0;
  62. double average = -1.2345;
  63. double maximum = -1.2345;
  64. double minimum = -1.2345;
  65. double variance = -1.2345;
  66. int i;
  67.  
  68. for(i = 0; i < tokcnt; i++){
  69. int num = atoi(stringstorage[i]);
  70. if(num != 0){
  71. if(maximum == -1.2345) maximum = num;
  72. else if(num > maximum) maximum = num;
  73. if(minimum == -1.2345) minimum = num;
  74. else if(num < minimum) minimum = num;
  75. if(average == -1.2345) average = num;
  76. else average += num;
  77.  
  78. numNums++;
  79. }
  80. }
  81. average /= numNums;
  82. variance = 0;
  83. for(i = 0; i < tokcnt; i++){
  84. int num = atoi(stringstorage[i]);
  85. if(num != 0){
  86. variance += pow(num - average, 2);
  87. }
  88. }
  89. variance /= numNums;
  90. end = clock();
  91. double elapsedtime = (double)(end - start) / CLOCKS_PER_SEC;
  92. printf("=== T2 Completed ===\n");
  93. printf("T2 RESULT: File %s.csv: Max = %f, Min = %f, Avg = %f, Var = %f\n", filename, maximum, minimum, average, variance);
  94. printf("T2 RESULT: Total elapsed time: %f seconds\n", elapsedtime);
  95. printf("=== T2 report end ===\n");
  96. }
  97.  
  98. int main(int argc, char **argv){
  99. pthread_t tid[3];
  100. pthread_attr_t attr;
  101. pthread_attr_init(&attr);
  102. //pthread_attr_setschedpolicy(&attr, SCHED_RR);
  103.  
  104. char *threadmode[2];
  105. char *priority_op[2];
  106. char *sched_op;
  107.  
  108. /* parsing options */
  109. if(argc < 2){
  110. printf("Usage: ./main multi|single <task #> [priority <task #> <low|high>] [sched <RR|FIFO|OTHER>]\n");
  111. return 0;
  112. }
  113. else{
  114. for(int i = 1; i < argc; i++){
  115. /* handle mode */
  116. if(!strcmp(argv[i], "single") || !strcmp(argv[i], "multi")){
  117. threadmode[0] = argv[i];
  118. if(!strcmp(threadmode[0], "single")){
  119. if(atoi(argv[i+1]) >= 1 && atoi(argv[i+1]) <= 3){
  120. threadmode[1] = argv[i+1];
  121. }
  122. else{
  123. printf("Usage: ./main multi|single <task #> [priority <task #> <low|high>] [sched <RR|FIFO|OTHER>]\n");
  124. return 0;
  125. }
  126. }
  127. }
  128. /* handle scheduling policy option */
  129. if(!strcmp(argv[i], "sched")){
  130. sched_op = argv[i+1];
  131. if(strcmp(sched_op, "FIFO") && strcmp(sched_op, "OTHER") && strcmp(sched_op, "RR")){
  132. printf("Scheduling option usage: sched <RR|FIFO|OTHER>\n");
  133. return 1;
  134. }
  135. }
  136. /* handle priority option */
  137. if(!strcmp(argv[i], "priority")){
  138. priority_op[0] = argv[i+1];
  139. priority_op[1] = argv[i+2];
  140. if((atoi(priority_op[0]) < 1 || atoi(priority_op[0]) > 3) || (strcmp(priority_op[1], "low") && strcmp(priority_op[1], "high"))){
  141. printf("Priority option usage: priority <task #> <low|high>\n");
  142. return 1;
  143. }
  144. }
  145. }
  146. }
  147. /* /parsing options */
  148.  
  149. char *filename = "test.csv";
  150.  
  151. printf("Reading file.\n");
  152.  
  153. readfile(filename, &tokcnt, &linecnt);
  154.  
  155. printf("File read.\n");
  156.  
  157. printf("Creating thread.\n");
  158.  
  159. /* task 2 */
  160. pthread_create(&tid[1], &attr, task2, filename);
  161. pthread_join(tid[1], NULL);
  162.  
  163. printf("Thread completed.\n");
  164.  
  165. for(int i = 0; i < tokcnt; i++){
  166. //printf("%s\n", stringstorage[i]);
  167. free(stringstorage[i]);
  168. }
  169.  
  170. //printf("Mode: %s\nScheduling: %s\nPriority: %s %s\n", threadmode, sched_op, priority_op[0], priority_op[1]);
  171.  
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement