Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <unistd.h>
  2.  
  3. #include <fcntl.h>
  4.  
  5. #include <stdio.h>
  6.  
  7. #include <stdlib.h>
  8.  
  9. #include <fcntl.h>
  10.  
  11. #include <string.h>
  12.  
  13. #include <signal.h>
  14.  
  15. #include <sys/types.h>
  16.  
  17. #include <sys/ipc.h>
  18.  
  19. #include <semaphore.h>
  20.  
  21. #include <sys/shm.h>
  22.  
  23. #include <sys/msg.h>
  24.  
  25.  
  26.  
  27. #define QKEY 0x1111
  28.  
  29.  
  30.  
  31. struct msq_que
  32.  
  33. {
  34.  
  35. long msg_type;
  36.  
  37. char buffer[2];
  38.  
  39. //buffer[size-1] - polecenie zakonczenia pracy
  40.  
  41. //buffer[size-2] - ilosc zmiennych, ktore zostaly przeslane
  42.  
  43. }msq;
  44.  
  45.  
  46.  
  47. void koniec(int sign)
  48.  
  49. {
  50.  
  51. fprintf(stderr, "\nKoniec. Czyszczenie pamieci.\n");
  52.  
  53. }
  54.  
  55.  
  56.  
  57. int main(int argc, char *arg[]){
  58.  
  59.  
  60.  
  61. char cho,c,d;
  62.  
  63. int i;
  64.  
  65.  
  66.  
  67. int pid=getpid();
  68.  
  69. printf("Proces macierzysty, PID: %d\n",pid);
  70.  
  71.  
  72.  
  73. int msqid;
  74.  
  75. msqid=msgget(QKEY, 0666 | IPC_CREAT);
  76.  
  77. msq.msg_type = 1;
  78.  
  79. signal(SIGQUIT, koniec);
  80.  
  81.  
  82.  
  83.  
  84.  
  85. if (fork()==0){
  86.  
  87.  
  88.  
  89. FILE *file=fopen(arg[1],"r"); //plik wejściowy
  90.  
  91. FILE *file2=fopen(arg[2],"r"); //plik wejściowy
  92.  
  93.  
  94.  
  95. if (file == NULL)
  96.  
  97. {
  98.  
  99. perror("Nie udalo sie otworzyc pliku_1");
  100.  
  101. }
  102.  
  103.  
  104.  
  105. if (file2 == NULL)
  106.  
  107. {
  108.  
  109. perror("Nie udalo sie otworzyc pliku_2");
  110.  
  111. }
  112.  
  113.  
  114.  
  115. while ((c=fgetc(file)) !=EOF){
  116.  
  117.  
  118.  
  119. d=fgetc(file2);
  120.  
  121. msq.buffer[0]=c;
  122.  
  123. msq.buffer[1]=d;
  124.  
  125.  
  126.  
  127. //fprintf(stderr,"%c",msq.buffer[0]);
  128.  
  129. msgsnd(msqid, &msq, sizeof(msq), 0);
  130.  
  131. }
  132.  
  133. c=fgetc(file);
  134.  
  135. msq.buffer[0]=c;
  136.  
  137. msgsnd(msqid, &msq, sizeof(msq), 0);
  138.  
  139. fclose(file);
  140.  
  141. fclose(file2);
  142.  
  143.  
  144.  
  145. return 0;
  146.  
  147. }
  148.  
  149.  
  150.  
  151. if (fork()==0){
  152.  
  153.  
  154.  
  155. FILE *output=fopen("test","w"); //plik wyjściowy
  156.  
  157. msgrcv(msqid, &msq, sizeof(msq), 1, 0);
  158.  
  159. while(msq.buffer[0]!=EOF){
  160.  
  161.  
  162.  
  163. cho=msq.buffer[0];
  164.  
  165. //fprintf(stderr,"%c",cho);
  166.  
  167. fprintf(output,"%c",cho);
  168.  
  169.  
  170.  
  171. cho=msq.buffer[1];
  172.  
  173. //fprintf(stderr,"%c",cho);
  174.  
  175. fprintf(output,"%c",cho);
  176.  
  177.  
  178.  
  179. msgrcv(msqid, &msq, sizeof(msq), 1, 0);
  180.  
  181.  
  182.  
  183. }
  184.  
  185. fclose(output);
  186.  
  187. msgctl(msqid, IPC_RMID, NULL); //usuniecie queue
  188.  
  189. return 0;
  190.  
  191. }
  192.  
  193.  
  194.  
  195. return 0;
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement