Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 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. #define SIZE 4096
  22.  
  23. void error_handler(char *message) {
  24. fprintf(stderr, "%s\n", message);
  25. perror("ERROR");
  26. exit(EXIT_FAILURE);
  27. }
  28.  
  29. int N;
  30. pid_t child[64];
  31. int indx = 0;
  32. char *buffer[1024];
  33.  
  34.  
  35. void SIGINT_handler_main(int a, siginfo_t *b, void *c) {
  36. int i;
  37. for(i = 0; i < N; i++) {
  38. kill(child[i], SIGINT);
  39. }
  40. }
  41.  
  42.  
  43.  
  44. void SIGINT_handler_child(int a, siginfo_t *b, void *c) {
  45. int i;
  46. printf("Strings of process %d:\n",getpid());
  47. for (i = 0; i < indx; i++) {
  48. printf("%s ",buffer[i]);
  49. }
  50. printf("\n");
  51. }
  52.  
  53.  
  54.  
  55. int main(int argc, char** argv){
  56.  
  57. N = argc-1;
  58. pid_t pid;
  59. void *shm;
  60. int sem, i, ret;
  61. sigset_t set;
  62. struct sembuf oper;
  63. struct sigaction act;
  64.  
  65. if (argc < 2) {
  66. error_handler("Not valid input");
  67. }
  68. shm = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
  69. if (shm == NULL) {
  70. error_handler("mmap has failed");
  71. }
  72. sem = semget(IPC_PRIVATE, N + 1, IPC_CREAT|0666);
  73. if (sem == -1) {
  74. error_handler("semget has failed");
  75. }
  76. for (i = 0; i < N; i++) {
  77. ret = semctl(sem, i, SETVAL, 0);
  78. if (ret == -1) {
  79. error_handler("semctl has failed");
  80. }
  81. }
  82. ret = semctl(sem, N, SETVAL, N);
  83. if (ret == -1) {
  84. error_handler("semctl has failed");
  85. }
  86. for (i = 0; i < N; i++) {
  87. pid = fork();
  88. if (pid == -1) {
  89. error_handler("fork has failed");
  90. }
  91. if (pid == 0) {
  92. char my_str[1024];
  93. int my_index = i;
  94.  
  95. if (strcpy(my_str, argv[my_index + 1]) == NULL) {
  96. error_handler("strcpy has failed");
  97. }
  98. ret = sigfillset(&set);
  99. if (ret == -1) {
  100. error_handler("sigfillset has failed");
  101. }
  102. act.sa_sigaction = SIGINT_handler_child;
  103. act.sa_mask = set;
  104. act.sa_flags = 0;
  105.  
  106. ret = sigaction(SIGINT, &act, NULL);
  107. if (ret == -1) {
  108. error_handler("sigaction has failed");
  109. }
  110. while (1) {
  111. oper.sem_num = my_index;
  112. oper.sem_op = -1;
  113. oper.sem_flg = 0;
  114.  
  115. step1: ret = semop(sem, &oper, 1);
  116. if (ret == -1) {
  117. if (errno == EINTR) goto step1;
  118. error_handler("semop has failed");
  119. }
  120. if (strstr(my_str,(char*)shm) != NULL) {
  121. step2: buffer[indx] = malloc(strlen((char*)shm)+1);
  122. if (buffer[indx] == NULL) {
  123. if (errno == EINTR) goto step2;
  124. error_handler("malloc has failed");
  125. }
  126. if (strcpy(buffer[indx], (char *)shm) == NULL) {
  127. error_handler("strcpy has failed");
  128. }
  129. indx ++;
  130. }
  131. oper.sem_num = N;
  132. oper.sem_op = 1;
  133. oper.sem_flg = 0;
  134.  
  135. step3: ret = semop(sem, &oper, 1);
  136. if (ret == -1) {
  137. if (errno == EINTR) goto step3;
  138. error_handler("semop has failed");
  139. }
  140. }
  141. }
  142. child[i] = pid;
  143. }
  144. ret = sigfillset(&set);
  145. if (ret == -1) {
  146. error_handler("sigfillset has failed");
  147. }
  148. act.sa_sigaction = SIGINT_handler_main;
  149. act.sa_mask = set;
  150. act.sa_flags = 0;
  151.  
  152. ret = sigaction(SIGINT, &act, NULL);
  153. if (ret == -1) {
  154. error_handler("sigaction has failed");
  155. }
  156. printf("Main process is ready to receive strings\n");
  157.  
  158. while (1) {
  159. oper.sem_num = N;
  160. oper.sem_op = -N;
  161. oper.sem_flg = 0;
  162.  
  163. stepA: ret = semop(sem, &oper, 1);
  164. if (ret == -1) {
  165. if (errno == EINTR) goto stepA;
  166. error_handler("semop has failed");
  167. }
  168. stepB: ret = scanf("%s", (char*)shm);
  169. if (ret == -1) {
  170. if (errno == EINTR) goto stepB;
  171. error_handler("scanf has failed");
  172. }
  173. for(i = 0; i < N; i++) {
  174. oper.sem_num = i;
  175. oper.sem_op = 1;
  176. oper.sem_flg = 0;
  177. stepC: ret = semop(sem, &oper, 1);
  178. if (ret == -1) {
  179. if (errno == EINTR) goto stepC;
  180. error_handler("semop has failed");
  181. }
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement