Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.30 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 <sys/types.h>
  11. #include <semaphore.h>
  12. #include <dirent.h>
  13. #include <math.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #define MAX_SIZE 1024
  17.  
  18. typedef struct INFO{
  19.   char *fileName;
  20.   char *stringstorage[MAX_SIZE];
  21.   int lines[MAX_SIZE];
  22.   int numlines;
  23. } INFO;
  24.  
  25. int numFiles;
  26. sem_t sem;
  27.  
  28. char *parseval(char *line, int num){
  29.   char *token;
  30.   char *temp = strdup(line);
  31.   token = strtok(temp, ",\n");
  32.   while(num > 0){
  33.     token = strtok(NULL, ",\n");
  34.     num--;
  35.   }
  36.   return token;
  37. }
  38.  
  39. void readfile(INFO info, int *tokcnt, int *linecnt){
  40.   *tokcnt = 0;
  41.   *linecnt = 1;
  42.   printf("Opening file: %s\n",info.fileName);
  43.   FILE* stream = fopen(info.fileName, "r");
  44.   if(stream==NULL) printf("FAILED TO OPEN FILE\n");
  45.   char line[1024];
  46.   while(fgets(line, 1024, stream)){
  47.     char *temp = strdup(line);
  48.     char *token;
  49.     int curcnt = 0;
  50.     info.lines[0] = 0;
  51.     token = parseval(line, curcnt);
  52.     while(token != NULL){
  53.       //printf("%d: %s| \n", curcnt, token);
  54.       info.stringstorage[*tokcnt] = strdup(token);
  55.       //printf("%s\n", info.stringstorage[*tokcnt]);
  56.       curcnt++;
  57.       *tokcnt = *tokcnt + 1;
  58.       token = parseval(line, curcnt);
  59.     }
  60.     info.lines[*linecnt] = *tokcnt;
  61.     *linecnt = *linecnt + 1;
  62.     info.numlines++;
  63.   }
  64.   printf("READ DONE. THE FILE HAD: %i LINES\n", info.numlines);
  65.   fclose(stream);
  66. }
  67.  
  68. void task1(INFO *info){
  69.   clock_t start = clock(), end;
  70.   int i, j, idx, uniques[numFiles];
  71.   for(i=0;i<numFiles;i++){
  72.     uniques[i]=0;
  73.     for(j=0; j<info[i].numlines; j++){
  74.     //printf("TASK1\n");
  75.       if(atoi(info[i].stringstorage[j])>0 || info[i].stringstorage[j]=="," || info[i].stringstorage[j]=='\0') continue;
  76.       if(strcmp(info[i].stringstorage[j],info[i].stringstorage[j+1])==0) continue;
  77.       else uniques[i]++;
  78.     }
  79.   }
  80.   printf("=== T1 completed ===\n");
  81.   printf("=== T1 report start ===\n");
  82.   for(i=0;i<numFiles;i++){
  83.     printf("T1 RESULT: File %s: Total number of unique words: %d\n",info[i].fileName,uniques[i]);
  84.   }
  85.   end = clock();
  86.   double time_used = end-start;
  87.   printf("T1 RESULT: Total elapsed time: %f seconds\n",time_used);
  88.   printf("=== T1 report end ===\n");
  89. }
  90.  
  91. void task3(const char* filename, int tokenAmt){
  92.  
  93. }
  94.  
  95. int main(int argc, char **argv){
  96.   char *threadmode;
  97.   char *priority_op[2];
  98.   char *sched_op;
  99.   int task;
  100.   int i,j;
  101.  
  102.   /* parsing options */
  103.   if(argc < 2){
  104.     printf("Usage: ./main multi|single <task #> [priority <task #> <low|high>] [sched <RR|FIFO|OTHER>]\n");
  105.     return 0;
  106.   }
  107.   else{
  108.     for(i = 1; i < argc; i++){
  109.       if(!strcmp(argv[i], "single") || !strcmp(argv[i], "multi")){
  110.         threadmode = argv[i];
  111.       }
  112.       if(!strcmp(argv[i], "sched")){
  113.         sched_op = argv[i+1];
  114.  
  115.       }
  116.       if(!strcmp(argv[i], "priority")){
  117.         priority_op[0] = argv[i+1];
  118.         priority_op[1] = argv[i+2];
  119.         if((atoi(priority_op[0]) < 1 || atoi(priority_op[0]) > 3) || (strcmp(priority_op[1], "low") && strcmp(priority_op[1], "high"))){
  120.           printf("Priority option usage: priority <task #> <low|high>\n");
  121.           return 1;
  122.         }
  123.       }
  124.     }
  125.   }
  126.   if(strcmp(threadmode,"single")==0) task=atoi(argv[2]);
  127.  
  128.  
  129.   /* /parsing options */
  130.  
  131.   /* TEST PRINT FOR ARGS
  132.  
  133.   printf("ARGV[0]: %s \n ARGV[1]: %s \n ARGV[2]: %s \n", argv[0],argv[1],argv[2]);
  134.   printf("priorioty_op[0]: %s \n priority_op[1]: %s \n priority_op[2]: %s \n", priority_op[0],priority_op[1],priority_op[2]);
  135.  
  136.   END TEST PRINT */
  137.  
  138.   DIR *d;
  139.   char *names[MAX_SIZE];
  140.   char *per;
  141.   numFiles=0;
  142.   struct dirent *dir;
  143.   d=opendir("analcatdata");
  144.   if(d){
  145.     while((dir=readdir(d))!=NULL){
  146.       per=strrchr(dir->d_name, '.');
  147.       if(per && !strcmp(per,".csv")){
  148.         char name[MAX_SIZE];
  149.         strcpy(name, "analcatdata/");
  150.         strcat(name,dir->d_name);
  151.         names[numFiles]=strdup(name);
  152.         numFiles++;
  153.       }
  154.     }
  155.     closedir(d);
  156.   } else printf("No directory named 'analcatdata' included. Please include directory with '.csv' files.");
  157.  
  158.   INFO files[numFiles];
  159.   printf("All %i files opened\n",numFiles);
  160.  
  161.   if(strcmp(threadmode,"single")==0 && task==1){
  162.     for(i=0;i<numFiles;i++){
  163.       int tokcnt=0;
  164.       int linecnt=0;
  165.       files[i].fileName = names[i];
  166.       printf("READING FILE %i: %s\n",i,files[i].fileName);
  167.       readfile(files[i], &tokcnt, &linecnt);
  168.       //for(j=0;j<tokcnt;j++) free(files[i].stringstorage[j]);
  169.     }
  170.     task1(files);
  171.   }
  172.   /* TODO
  173.   else if(strcmp(threadmode,"single")==0 && task==2)
  174.     task2("AIDS.csv", tokcnt);
  175.   else if(strcmp(threadmode,"single")==0 && task==3)
  176.     task3("AIDS.csv", tokcnt);
  177.   else{ printf("Well some kind of error occurred\n"); return 1; }
  178.   */
  179.  
  180.   /* print every token on new line */
  181.   /*for(i = 0; i < linecnt-1; i++){
  182.     for(j = 0; j < lines[i+1] - lines[i]; j++){
  183.       printf("%s\n", stringstorage[lines[i]+j]);
  184.     }
  185.   }*/
  186.   /*for(i=0;i<numFiles;i++){
  187.     for(j=0;j<tokcnt;j++){
  188.     //printf("%s\n", stringstorage[i]);
  189.       free(files[i].stringstorage[j]);
  190.     }
  191.   }*/
  192.  
  193.   //printf("Mode: %s\nScheduling: %s\nPriority: %s %s\n", threadmode, sched_op, priority_op[0], priority_op[1]);
  194.   return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement