Advertisement
Guest User

1318 without restofthesignals

a guest
Jan 20th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <signal.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <sys/ipc.h>
  11. #include <err.h>
  12. #include <sys/sem.h>
  13.  
  14. key_t key1, key2, key3; // klucz dla semaforow
  15. int semid1, semid2, semid3; // ID semaforow
  16. union semun ctl; // unia do kontroli semafora
  17. int pid1, pid2, pid3, ppid;
  18. FILE* input;
  19. FILE* temporary;
  20. int pdes[2];
  21.  
  22.  
  23. union semun
  24. {
  25. int val;
  26. struct semid_ds *buf;
  27. unsigned short int *array;
  28. struct seminfo *__buf;
  29. };
  30.  
  31. int semlock(int semid)
  32. {
  33.  
  34. struct sembuf opr;
  35. opr.sem_num = 0;
  36. opr.sem_op = -1;
  37. opr.sem_flg = 0;
  38.  
  39. if (semop(semid, &opr, 1) == -1){
  40. warn("Blad blokowania semafora!");
  41. return 0;
  42. }else{
  43. return 1;
  44. }
  45. }
  46.  
  47. int semunlock(int semid)
  48. {
  49. struct sembuf opr;
  50.  
  51. opr.sem_num = 0;
  52. opr.sem_op = 1;
  53. opr.sem_flg = 0;
  54.  
  55. if (semop(semid, &opr, 1) == -1)
  56. {
  57. warn("Blad odblokowania semafora!");
  58. return 0;
  59. }
  60. else
  61. {
  62. return 1;
  63. }
  64. }
  65. void charToHex(unsigned char c, char * hex) {
  66. sprintf(hex, "%x", c);
  67. }
  68. void process1Interactive() {
  69. char data[256] = "\n\0"; char c; int i;
  70. while(1){
  71. sleep(1);
  72. fprintf(stderr, "\nPodaj dane (EXIT, by wyjsc):");
  73. fgets(data,256,stdin);
  74. if( strcmp( data , "EXIT\n" ) == 0 ) break;
  75. i = 0;
  76. while(data[i]!= '\n'){
  77. semlock(semid1);
  78. fputc(data[i], temporary);
  79. fflush(temporary);
  80. fseek(temporary, -1, SEEK_CUR);
  81. i++;
  82. semunlock(semid2);
  83. }
  84. }
  85. fprintf(stderr,"\nKoniec wczytywania danych");
  86. kill(getpid(), 1);
  87. }
  88. void process1File() {
  89. char c; int i;
  90. while((c = fgetc(input))!= EOF) {
  91. semlock(semid1);
  92. fputc(c, temporary);
  93. fflush(temporary);
  94. fseek(temporary, -1, SEEK_CUR);
  95. semunlock(semid2);
  96. }
  97. fprintf(stderr,"\nKoniec wczytywania danych");
  98. kill(getpid(), 1);
  99. }
  100. void process1Random() {
  101.  
  102. unsigned char c; int i;
  103. while((c = fgetc(input))!= EOF) {
  104. semlock(semid1);
  105. fputc(c, temporary);
  106. fflush(temporary);
  107. fseek(temporary, -1, SEEK_CUR);
  108. semunlock(semid2);
  109. }
  110.  
  111. }
  112. void process2() {
  113. unsigned char c;
  114. char hex[3];
  115. close(pdes[0]);
  116. while(1) {
  117. semlock(semid2);
  118. c = fgetc(temporary);
  119. charToHex(c, hex);
  120. write(pdes[1], hex, sizeof(hex));
  121. semunlock(semid3);
  122. }
  123. }
  124. void process3() {
  125. unsigned char c; char heks[3]; int k = 0;
  126. close(pdes[1]);
  127. while(1) {
  128. semlock(semid3);
  129. read(pdes[0], heks, sizeof(heks));
  130. fprintf(stderr, "%s ", heks);
  131. k++;
  132. if( k == 15 ) {
  133. fprintf(stderr,"\n");
  134. k = 0;
  135. }
  136. semunlock(semid1);
  137. }
  138.  
  139. }
  140. void signalHandler(int signal) {
  141. if(signal == 2) {
  142. fprintf(stderr, "\nZabijam proces o pid %d", getpid());
  143. exit(0);
  144. }
  145. else if(signal == 1) { // getpid() == pid1 &&
  146. kill(pid2, 2);
  147. kill(pid3, 2);
  148. kill(ppid, 2);
  149. kill(pid1, 2);
  150. }
  151.  
  152. }
  153. void signalMotherHandler(int signal) {
  154. if(signal == 2 ) {
  155. fprintf(stderr, "\nZabijam proces macierzysty o pid: %d", ppid);
  156. close((int)input);
  157. close((int)temporary);
  158. remove("file");
  159. exit(0);
  160. }
  161. }
  162. void signalInit() {
  163. signal(1, signalHandler);
  164. signal(2, signalHandler);
  165.  
  166. }
  167. int main(int argc, char **argv)
  168. { //////////////
  169. if ((key1 = ftok(".", 'A')) == -1)
  170. errx(1, "Blad tworzenia klucza!");
  171. if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1)
  172. errx(2, "Blad tworzenia semafora!");
  173. ctl.val = 1;
  174. if (semctl(semid1, 0, SETVAL, ctl) == -1)
  175. errx(3, "Blad ustawiania semafora!");
  176.  
  177. if ((key2 = ftok(".", 'B')) == -1)
  178. errx(1, "Blad tworzenia klucza!");
  179. if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1)
  180. errx(2, "Blad tworzenia semafora!");
  181. ctl.val = 1;
  182. if (semctl(semid2, 0, SETVAL, ctl) == -1)
  183. errx(3, "Blad ustawiania semafora!");
  184.  
  185. if ((key3 = ftok(".", 'C')) == -1)
  186. errx(1, "Blad tworzenia klucza!");
  187. if ((semid3 = semget(key3, 1, IPC_CREAT | 0600)) == -1)
  188. errx(2, "Blad tworzenia semafora!");
  189. ctl.val = 1;
  190. if (semctl(semid3, 0, SETVAL, ctl) == -1)
  191. errx(3, "Blad ustawiania semafora!");
  192.  
  193. semlock(semid3);
  194. semlock(semid2);
  195.  
  196. pipe(pdes);
  197.  
  198. if(argc>1) {
  199. input = fopen(argv[1], "r");
  200. if(input == NULL) {
  201. fprintf(stderr, "Blad przy otwieraniu pliku do odczytu!");
  202. return -1;
  203. }
  204. }
  205. temporary = fopen("file", "w+");
  206. if(temporary == NULL) {
  207. fprintf(stderr, "Blad przy otwieraniu pliku do zapisu!");
  208. return -2;
  209. }
  210.  
  211. ppid = getpid();
  212. fprintf(stderr, "Ppid = %d\n", ppid);
  213. signal(2, signalMotherHandler);
  214.  
  215. if( fork() == 0 ) { /////////////////////////////////////////////// proces 1 -> przekazuje dane do procesu 2 poprzez file
  216. pid1 = getpid();
  217. fprintf(stderr, "Pid1 = %d\n", pid1);
  218. signalInit();
  219. if(argc == 1) { // interaktywny
  220. process1Interactive();
  221. }
  222. else if( strcmp(argv[1], "/dev/urandom") == 0 ){ // random
  223. process1Random();
  224. }
  225. else { // plik
  226. process1File();
  227. }
  228.  
  229. }
  230. else if(fork() == 0) { /////////////////////////////////// proces 2 -> pobiera dane z procesu 1 poprzez file i przekazuje do procesu 3 poprzez pipe
  231. pid2 = getpid();
  232. fprintf(stderr, "Pid2 = %d\n", pid2);
  233. signalInit();
  234. process2();
  235. }
  236. else if(fork() == 0) { ////////////////////////////////// proces 3 -> pobiera dane z procesu 3 poprzez pipe
  237. pid3 = getpid();
  238. fprintf(stderr, "Pid3 = %d\n", pid3);
  239. signalInit();
  240. process3();
  241. }
  242.  
  243. pause();
  244. return 0;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement