Guest User

Untitled

a guest
Jan 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. /*Maggie Cao*/
  2. /*CISC3350*/
  3. /*April 20, 2017*/
  4. /*Assignment #4*/
  5. /*Searching files using pthreads
  6. The program doesn't print out all of the lines in the files that has the pattern because
  7. there is a segmentation fault in the program. I can't seem to find where the memory is
  8. leaking. I know that function searchfile is supposed to return number of lines, but
  9. adding that piece of code interfers with the output of the program. Thus I commented
  10. those lines of code out (as well as the total number of matched lines in main)
  11. Updated: The segmentation fault was because I allocated not enough lines for a file,
  12. i shouldn't allocate all the lines at once for a file, but make it read a line at a time
  13. The code mostly works, except that the numoflines return value for searchfile isn't
  14. correctly passed back into main's function call*/
  15.  
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #define NUMFILES 50
  23.  
  24. void *searchfile(void *);
  25.  
  26. /*globally accessible variables and a mutex*/
  27. char *pattern;
  28. pthread_t fileThreadsCall[NUMFILES];
  29. pthread_mutex_t mutexfile=PTHREAD_MUTEX_INITIALIZER;
  30.  
  31. /*Each thread runs this function via a shared resource
  32. using mutex synchronization*/
  33. void *searchfile(void *filename){
  34.  
  35. FILE *fileptr;
  36. char *tmpfilename=filename;
  37. //fprintf(stdout,"print from searchfile %s \n", tmpfilename);
  38.  
  39. char line[1000];
  40.  
  41. if ( (fileptr=fopen(tmpfilename,"r")) == NULL){
  42. perror("Thread failed to open file");
  43. exit (EXIT_FAILURE);
  44. }
  45.  
  46. void *match;
  47. /*for return value to return num of lines with pattern*/
  48. int numMatchlines=match;
  49. numMatchlines=0;
  50.  
  51. while ( !(feof(fileptr)) ){
  52. fgets(line,1024,fileptr); /*read one line at a time*/
  53. strstr(line,pattern);
  54. if ((strstr(line,pattern))!= NULL){
  55. /*synchronization is used to access the shared resource, standard output*/
  56. pthread_mutex_lock(&mutexfile);
  57. /*can print now once thread gets the lock*/
  58. fprintf(stdout,"Filename %s at Line: %s \n", tmpfilename, line);
  59. pthread_mutex_unlock(&mutexfile);
  60. numMatchlines++;
  61. }
  62. }
  63.  
  64. /*close the file after done*/
  65. if ( fclose(fileptr) == EOF )
  66. perror("Failed to close file descriptor");
  67.  
  68. printf("File: %s has a total number of %d matchlines \n", tmpfilename, numMatchlines);
  69. pthread_exit((void*)filename);
  70. return numMatchlines;
  71. }
  72.  
  73.  
  74.  
  75. int main(int argc, char**argv){
  76.  
  77. /*minimum of two arguments for program to work*/
  78. if (argc<2){
  79. errno=EINVAL;
  80. exit (EXIT_FAILURE);
  81. }
  82.  
  83. int fileNum=argc-2;//argc-2 is the number of files being searched
  84. pattern=argv[1];
  85.  
  86. /*main thread calls pthread_create() for each file to be searched
  87. passing the filename as the arg. A cast will be needed*/
  88. void *status;
  89. pthread_attr_t attr;
  90.  
  91. void *filename;
  92.  
  93. char **files=calloc(fileNum, sizeof(char*));
  94. int k;
  95. for (k=0; k<fileNum; k++)
  96. files[k]=malloc(fileNum*sizeof(char));
  97.  
  98. //store argv[] files separately from pattern
  99. int numarg;
  100. for (numarg=0; numarg<fileNum; numarg++)
  101. files[numarg]=filename;//(char*)filename;
  102.  
  103. int y;
  104. for (y=0; y<fileNum; y++)
  105. files[y]=argv[2+y];
  106.  
  107.  
  108. pthread_mutex_init(&mutexfile, NULL); //second arg is attr
  109.  
  110. int i;
  111. /*void *pthreadret;
  112. int totalnumMatchlines=pthreadret;
  113. totalnumMatchlines=0;*/
  114.  
  115. for (i=0; i<fileNum ; i++){
  116. if ( (pthread_create(&fileThreadsCall[i],NULL,searchfile,(void*)files[i])) != 0)
  117. perror("Cannot create pthread \n");
  118. //pthreadret=searchfile(files[j]);
  119. //totalnumMatchlines+=pthreadret;
  120. fprintf(stdout,"Filename %s \n", files[i]);
  121. }
  122.  
  123.  
  124. //printf("pthreadret %d \n", pthreadret);
  125.  
  126. /*main thread joins all the other threads, storing their return values*/
  127. int j;
  128. int threadretval[fileNum];
  129. for (j=0; j<fileNum; j++){
  130. if ( (threadretval[j]=pthread_join(fileThreadsCall[j], &status)) != 0) //not successful
  131. perror("pthread_join doesn't work \n");
  132. fprintf(stdout, "Thread join successfully with return value: %d \n", threadretval[j]);
  133. }
  134.  
  135. /*when all the other threads have terminated, the main() thread writes to standard output
  136. the number of matching lines in total*/
  137. //fprintf(stdout, "Total Number of matching lines: %d \n", totalnumMatchlines);
  138.  
  139. pthread_mutex_destroy(&mutexfile);
  140. pthread_exit(NULL);
  141. }
Add Comment
Please, Sign In to add comment