Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1.  
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <fcntl.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <sys/ipc.h>
  12. #include <err.h>
  13. #include <sys/sem.h>
  14.  
  15. key_t key1, key2, key3; // klucz dla semaforow
  16. int semid1, semid2, semid3; // ID semaforow
  17. union semun ctl; // unia do kontroli semafora
  18.  
  19. FILE* input;
  20. FILE* temporary;
  21. int pdes[2];
  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 dane[256];
  70. char c;
  71. while(1){
  72. sleep(1);
  73. printf("Podaj dane: ");
  74. scanf("%s", dane);
  75. int i = 0;
  76. while(dane[i]!= '\0'){
  77. semlock(semid1);
  78. //printf("Process 1: %c\n", dane[i]);
  79. //fflush(stdout);
  80. fputc(dane[i], temporary);
  81. fflush(temporary);
  82. fseek(temporary, -1, SEEK_CUR);
  83. i++;
  84. semunlock(semid2);
  85. }
  86.  
  87. }
  88.  
  89.  
  90. }
  91. void process1File() {
  92. char c; int i;
  93. while((c = fgetc(input))!= EOF) {
  94. semlock(semid1);
  95. //printf("Process 1: %c\n", c);
  96. //fflush(stdout);
  97. fputc(c, temporary);
  98. fflush(temporary);
  99. fseek(temporary, -1, SEEK_CUR);
  100. semunlock(semid2);
  101. }
  102. }
  103. void process1Random() {
  104.  
  105. unsigned char c; int i;
  106. while((c = fgetc(input))!= EOF) {
  107. semlock(semid1);
  108. //printf("Process 1: %c\n", c);
  109. //fflush(stdout);
  110. fputc(c, temporary);
  111. fflush(temporary);
  112. fseek(temporary, -1, SEEK_CUR);
  113. semunlock(semid2);
  114. }
  115.  
  116. }
  117. void process2() {
  118. unsigned char c;
  119. char hex[3];
  120. close(pdes[0]);
  121. while(1) {
  122. semlock(semid2);
  123. c = fgetc(temporary);
  124. charToHex(c, hex);
  125. printf("Hex = %s", hex);
  126. //printf("Process 2: %c\n", c);
  127. fflush(stdout);
  128. write(pdes[1], hex, sizeof(hex));//write(pdes[1], &c, 1);
  129. semunlock(semid3);
  130. }
  131. //remove("file");
  132. }
  133. void process3() { // mozna zakonczyc ten proces normalnie za pomoca sygnalu, jak cos
  134. unsigned char c;
  135. char heks[3];
  136. close(pdes[1]);
  137. while(1) {
  138. semlock(semid3);
  139. read(pdes[0], heks, sizeof(heks));//read(pdes[0], &c, 1);
  140. //printf("In process 3: %c\n", c);
  141. printf(" , %s\n", heks);
  142. fflush(stdout);
  143. semunlock(semid1);
  144. }
  145.  
  146. }
  147.  
  148. int main(int argc, char **argv)
  149. { //////////////
  150. if ((key1 = ftok(".", 'A')) == -1)
  151. errx(1, "Blad tworzenia klucza!");
  152.  
  153. if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1)
  154. errx(2, "Blad tworzenia semafora!");
  155.  
  156. ctl.val = 1;
  157. if (semctl(semid1, 0, SETVAL, ctl) == -1)
  158. errx(3, "Blad ustawiania semafora!");
  159.  
  160. if ((key2 = ftok(".", 'B')) == -1)
  161. errx(1, "Blad tworzenia klucza!");
  162.  
  163. if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1)
  164. errx(2, "Blad tworzenia semafora!");
  165.  
  166. ctl.val = 1;
  167. if (semctl(semid2, 0, SETVAL, ctl) == -1)
  168. errx(3, "Blad ustawiania semafora!");
  169. if ((key3 = ftok(".", 'C')) == -1)
  170. errx(1, "Blad tworzenia klucza!");
  171.  
  172. if ((semid3 = semget(key3, 1, IPC_CREAT | 0600)) == -1)
  173. errx(2, "Blad tworzenia semafora!");
  174.  
  175. ctl.val = 1;
  176. if (semctl(semid3, 0, SETVAL, ctl) == -1)
  177. errx(3, "Blad ustawiania semafora!");
  178.  
  179. semlock(semid3);
  180. semlock(semid2);
  181.  
  182. pipe(pdes);
  183. ///////////////////files opening
  184. if(argc>1) {
  185. input = fopen(argv[1], "r"); //FILE* input = fopen(argv[1], "r"); /dev/urandom
  186. if(input == NULL) {
  187. printf("Blad przy otwieraniu pliku do odczytu!");
  188. return -1;
  189. }
  190. }
  191. temporary = fopen("file", "w+");
  192. if(temporary == NULL) {
  193. printf("Blad przy otwieraniu pliku do zapisu!");
  194. return -2;
  195. }
  196.  
  197.  
  198. /////////////////// processes
  199. if( fork() == 0 ) { /////////////////////////////////////////////// proces 1 -> przekazuje dane do procesu 2 poprzez file
  200. if(argc == 1) { // interaktywny
  201. printf("Interactive");
  202. process1Interactive();
  203. }
  204. else if( strcmp(argv[1], "/dev/urandom") == 0 ){ // random
  205. printf("Random");
  206. process1Random();
  207. }
  208. else { // plik
  209. printf("File");
  210. process1File();
  211. }
  212.  
  213. }
  214. else if(fork() == 0) { /////////////////////////////////// proces 2 -> pobiera dane z procesu 1 poprzez file i przekazuje do procesu 3 poprzez pipe
  215. process2();
  216. }
  217. else if(fork() == 0) { ////////////////////////////////// proces 3 -> pobiera dane z procesu 3 poprzez pipe
  218. process3();
  219. }
  220.  
  221. while(1)
  222. pause();
  223. return 0;
  224. }
  225. // /dev/urandom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement