Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. /* I pledge my Honor that I have abided by the Stevens Honor System -Paul Chua
  2. * cs392_thread.c
  3. *
  4. * Created on: Apr 17, 2019
  5. * Author: StevensUser
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <pthread.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13.  
  14. int item1_counter,item2_counter,item3_counter = 0;
  15. pthread_mutex_t m;
  16.  
  17.  
  18. void* cs392_run_thread(void * arg){
  19. //Read each line
  20. FILE *fp;
  21. int s;
  22. char * buffer;
  23. ssize_t line;
  24. size_t line_buf_size = 0;
  25.  
  26. //Open file and give error if you can't open it
  27. fp = fopen(arg,"r");
  28. if(fp ==NULL){
  29. perror("Error opening file");
  30. }
  31. pthread_mutex_lock(&m);
  32. line = getline(&buffer, &line_buf_size, fp);
  33. //While there is still content in the file, adjust counters based off of what it reads
  34. while(line >= 0){
  35. if (strcmp(buffer,"+item3\n") == 0){
  36. item3_counter++;;
  37. }
  38. else if( strcmp(buffer,"-item3\n") == 0){
  39. item3_counter--;
  40. }
  41. else if( strcmp(buffer,"+item2\n") == 0){
  42. item2_counter++;
  43. }
  44. else if( strcmp(buffer,"-item2\n") == 0){
  45. item2_counter--;
  46. }
  47. else if( strcmp(buffer,"+item1\n") == 0){
  48. item1_counter++;
  49. }
  50. else if( strcmp(buffer,"-item1\n") == 0){
  51. item1_counter--;
  52. }
  53. //If for some reason, you dont get the +item1 stuff, exit the program and tell the user something is unreadable
  54. else{
  55. printf("Content is wrong");
  56. exit(1);
  57. }
  58. //Move onto the next line
  59. line = getline(&buffer, &line_buf_size, fp);
  60. }
  61.  
  62. pthread_mutex_unlock(&m);
  63. pthread_exit(NULL);
  64. return NULL;
  65. }
  66.  
  67. int main(int argc, char **argv){
  68. //Error for when not enough arguments
  69. if (argc != 4){
  70. perror("Not enough arguments");
  71. }
  72. //Error if mutex fails
  73. if(pthread_mutex_init(&m, NULL) != 0){
  74. printf("Mutex init failed");
  75. }
  76. //Create threads and run the threads using the various arguments
  77. pthread_t thread1,thread2,thread3;
  78. int iret1,iret2,iret3;
  79. iret1 = pthread_create(&thread1,NULL,cs392_run_thread,argv[1]);
  80. iret2 = pthread_create(&thread2,NULL,cs392_run_thread,argv[2]);
  81. iret3 = pthread_create(&thread3,NULL,cs392_run_thread,argv[3]);
  82.  
  83. //Join them
  84. pthread_join(thread1,NULL);
  85. pthread_join(thread2,NULL);
  86. pthread_join(thread3,NULL);
  87. //Print the result and destroy mutex
  88. pthread_mutex_destroy(&m);
  89. printf("The final value of item1_counter, item2_counter, and item3_counter are %d, %d, and %d", item1_counter, item2_counter, item3_counter);
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement