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 <sys/types.h>
- #include <semaphore.h>
- #include <dirent.h>
- #include <math.h>
- #include <string.h>
- #include <time.h>
- #define MAX_SIZE 1024
- typedef struct INFO{
- char *fileName;
- char *stringstorage[MAX_SIZE];
- int lines[MAX_SIZE];
- int numlines;
- } INFO;
- int numFiles;
- sem_t sem;
- 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(INFO info, int *tokcnt, int *linecnt){
- *tokcnt = 0;
- *linecnt = 1;
- printf("Opening file: %s\n",info.fileName);
- FILE* stream = fopen(info.fileName, "r");
- if(stream==NULL) printf("FAILED TO OPEN FILE\n");
- char line[1024];
- while(fgets(line, 1024, stream)){
- char *temp = strdup(line);
- char *token;
- int curcnt = 0;
- info.lines[0] = 0;
- token = parseval(line, curcnt);
- while(token != NULL){
- //printf("%d: %s| \n", curcnt, token);
- info.stringstorage[*tokcnt] = strdup(token);
- //printf("%s\n", info.stringstorage[*tokcnt]);
- curcnt++;
- *tokcnt = *tokcnt + 1;
- token = parseval(line, curcnt);
- }
- info.lines[*linecnt] = *tokcnt;
- *linecnt = *linecnt + 1;
- info.numlines++;
- }
- printf("READ DONE. THE FILE HAD: %i LINES\n", info.numlines);
- fclose(stream);
- }
- void task1(INFO *info){
- clock_t start = clock(), end;
- int i, j, idx, uniques[numFiles];
- for(i=0;i<numFiles;i++){
- uniques[i]=0;
- for(j=0; j<info[i].numlines; j++){
- //printf("TASK1\n");
- if(atoi(info[i].stringstorage[j])>0 || info[i].stringstorage[j]=="," || info[i].stringstorage[j]=='\0') continue;
- if(strcmp(info[i].stringstorage[j],info[i].stringstorage[j+1])==0) continue;
- else uniques[i]++;
- }
- }
- printf("=== T1 completed ===\n");
- printf("=== T1 report start ===\n");
- for(i=0;i<numFiles;i++){
- printf("T1 RESULT: File %s: Total number of unique words: %d\n",info[i].fileName,uniques[i]);
- }
- end = clock();
- double time_used = end-start;
- printf("T1 RESULT: Total elapsed time: %f seconds\n",time_used);
- printf("=== T1 report end ===\n");
- }
- void task3(const char* filename, int tokenAmt){
- }
- int main(int argc, char **argv){
- char *threadmode;
- char *priority_op[2];
- char *sched_op;
- int task;
- int i,j;
- /* parsing options */
- if(argc < 2){
- printf("Usage: ./main multi|single <task #> [priority <task #> <low|high>] [sched <RR|FIFO|OTHER>]\n");
- return 0;
- }
- else{
- for(i = 1; i < argc; i++){
- if(!strcmp(argv[i], "single") || !strcmp(argv[i], "multi")){
- threadmode = argv[i];
- }
- if(!strcmp(argv[i], "sched")){
- sched_op = argv[i+1];
- }
- 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;
- }
- }
- }
- }
- if(strcmp(threadmode,"single")==0) task=atoi(argv[2]);
- /* /parsing options */
- /* TEST PRINT FOR ARGS
- printf("ARGV[0]: %s \n ARGV[1]: %s \n ARGV[2]: %s \n", argv[0],argv[1],argv[2]);
- 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]);
- END TEST PRINT */
- DIR *d;
- char *names[MAX_SIZE];
- char *per;
- numFiles=0;
- struct dirent *dir;
- d=opendir("analcatdata");
- if(d){
- while((dir=readdir(d))!=NULL){
- per=strrchr(dir->d_name, '.');
- if(per && !strcmp(per,".csv")){
- char name[MAX_SIZE];
- strcpy(name, "analcatdata/");
- strcat(name,dir->d_name);
- names[numFiles]=strdup(name);
- numFiles++;
- }
- }
- closedir(d);
- } else printf("No directory named 'analcatdata' included. Please include directory with '.csv' files.");
- INFO files[numFiles];
- printf("All %i files opened\n",numFiles);
- if(strcmp(threadmode,"single")==0 && task==1){
- for(i=0;i<numFiles;i++){
- int tokcnt=0;
- int linecnt=0;
- files[i].fileName = names[i];
- printf("READING FILE %i: %s\n",i,files[i].fileName);
- readfile(files[i], &tokcnt, &linecnt);
- //for(j=0;j<tokcnt;j++) free(files[i].stringstorage[j]);
- }
- task1(files);
- }
- /* TODO
- else if(strcmp(threadmode,"single")==0 && task==2)
- task2("AIDS.csv", tokcnt);
- else if(strcmp(threadmode,"single")==0 && task==3)
- task3("AIDS.csv", tokcnt);
- else{ printf("Well some kind of error occurred\n"); return 1; }
- */
- /* print every token on new line */
- /*for(i = 0; i < linecnt-1; i++){
- for(j = 0; j < lines[i+1] - lines[i]; j++){
- printf("%s\n", stringstorage[lines[i]+j]);
- }
- }*/
- /*for(i=0;i<numFiles;i++){
- for(j=0;j<tokcnt;j++){
- //printf("%s\n", stringstorage[i]);
- free(files[i].stringstorage[j]);
- }
- }*/
- //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