Advertisement
Guest User

FINAL

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