Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #ifdef Posix_compile
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <signal.h>
  5. #include <pthread.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/mman.h>
  9. #include <sys/sem.h>
  10. #include <semaphore.h>
  11. #include <fcntl.h>
  12. #else
  13. #include <windows.h>
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20.  
  21. typedef struct _thread {
  22. pthread_t tid;
  23. char my_str[1024];
  24. int occ;
  25. } thread;
  26.  
  27.  
  28. char buffer[1024];
  29. FILE *file;
  30. thread *list;
  31. int N, sem_m, sem_th;
  32. struct sembuf oper_m;
  33.  
  34.  
  35. void error_handler(char *message) {
  36. fprintf(stderr, "%s\n", message);
  37. perror("ERROR");
  38. exit(EXIT_FAILURE);
  39. }
  40.  
  41.  
  42. void SIGINT_handler(int a, siginfo_t *b, void *c) {
  43. int i;
  44. printf("\n");
  45. for (i = 0; i < N; i++) {
  46. printf("Thread %ld has %d occurrences\n",list[i].tid, list[i].occ);
  47. }
  48. }
  49.  
  50.  
  51. void *run(void *index) {
  52. list[(long)index].tid = pthread_self();
  53. list[(long)index].occ = 0;
  54. struct sembuf oper_t;
  55. int ret;
  56.  
  57. oper_t.sem_num = (long)index;
  58. oper_t.sem_op = -1;
  59. oper_t.sem_flg = 0;
  60.  
  61. while(1) {
  62. semop1: ret = semop(sem_th, &oper_t, 1);
  63. if (ret == -1) {
  64. if (errno == EINTR)
  65. goto semop1;
  66. error_handler("Semaphore operation failed");
  67. }
  68. if (strstr(list[(long)index].my_str, buffer) != NULL) {
  69. printf("Thread %ld has found a new occurrence\n",list[(long)index].tid);
  70. ret = fprintf(file, "%s\n", buffer);
  71. fflush(file);
  72. if (ret < 0) {
  73. error_handler("File write has failed");
  74. }
  75. list[(long)index].occ += 1;
  76. }
  77. oper_m.sem_num = 0;
  78. oper_m.sem_op = 1;
  79. oper_m.sem_flg = 0;
  80.  
  81. semop2: ret = semop(sem_m, &oper_m, 1);
  82. if (ret == -1) {
  83. error_handler("Semaphore operation has failed");
  84. if(errno == EINTR)
  85. goto semop2;
  86. }
  87. }
  88. }
  89.  
  90.  
  91. int main(int argc,char *argv[]) {
  92.  
  93. int ret;
  94. long i;
  95. pthread_t tid;
  96. sigset_t set;
  97. struct sigaction act;
  98. struct sembuf oper_t;
  99.  
  100. if(argc < 3) {
  101. error_handler("Not valid input");
  102. }
  103. N = argc - 2;
  104. file = fopen(argv[1], "w+");
  105. if (file == NULL) {
  106. error_handler("Unable to open the file");
  107. }
  108. list = malloc(N*sizeof(thread*));
  109. sem_m = semget(IPC_PRIVATE, 1, IPC_CREAT|0666);
  110. if (sem_m == -1) {
  111. error_handler("Unable to get a semaphore");
  112. }
  113. sem_th = semget(IPC_PRIVATE, N, IPC_CREAT|0666);
  114. if (sem_th == -1) {
  115. error_handler("Unable to get a semaphore");
  116. }
  117. ret = semctl(sem_m, 0, SETVAL, N);
  118. if (ret == -1) {
  119. error_handler("semctl has failed");
  120. }
  121. for (i = 0; i < N; i++) {
  122. ret = semctl(sem_th, i, SETVAL, 0);
  123. if (ret == -1) {
  124. error_handler("semctl has failed");
  125. }
  126. }
  127. for (i = 0; i < N; i++) {
  128. strcpy(list[i].my_str, argv[i+2]);
  129. ret = pthread_create(&tid, NULL, run, (void *)i);
  130. if (ret != 0) {
  131. error_handler("Creation thread has failed");
  132. }
  133. }
  134. ret = sigfillset(&set);
  135. act.sa_sigaction = SIGINT_handler;
  136. act.sa_mask = set;
  137. act.sa_flags = 0;
  138.  
  139. sigaction(SIGINT, &act, NULL);
  140.  
  141. oper_m.sem_num = 0;
  142. oper_m.sem_op = -N;
  143. oper_m.sem_flg = 0;
  144.  
  145. printf("Main thread is ready to receive strings\n");
  146. while(1) {
  147. step1: ret = semop(sem_m, &oper_m, 1);
  148. if (ret == -1) {
  149. if (errno == EINTR)
  150. goto step1;
  151. error_handler("Semaphore operation failed");
  152. }
  153. step2: ret = scanf("%s", buffer);
  154. if (ret == -1) {
  155. if (errno == EINTR)
  156. goto step2;
  157. error_handler("scanf has failed");
  158. }
  159. if (ret == 0) {
  160. while(getchar() != '\n') {}
  161. printf("Not valid input. Retry\n");
  162. goto step2;
  163. }
  164. oper_t.sem_op = 1;
  165. oper_t.sem_flg = 0;
  166. for (i = 0; i < N; i++) {
  167. oper_t.sem_num = i;
  168. step3: ret = semop(sem_th, &oper_t, 1);
  169. if (ret == -1) {
  170. if (errno == EINTR)
  171. goto step3;
  172. error_handler("Semaphore operation failed");
  173. }
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement