Advertisement
LazySenpai

lab8

Dec 5th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. /*Pierwszy*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8.  
  9. int main()
  10. {
  11.     int fd;
  12.     char * myfifo = "fifo1_2";
  13.  
  14.  
  15.     mkfifo(myfifo, 0666);
  16.  
  17.     char arr1[80], arr2[80];
  18.  
  19.     while(1) {
  20.         fd = open(myfifo, O_WRONLY);
  21.         fgets(arr2, 80, stdin);
  22.         write(fd, arr2, strlen(arr2)+1);
  23.         close(fd);
  24.         if(arr2[0] == '@'){
  25.             return 0;
  26.         }
  27.     }
  28.  
  29.     return 0;
  30. }
  31. /*Drugi*/
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <fcntl.h>
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38.  
  39. int main() {
  40.     int fd1;
  41.     int fd2;
  42.  
  43.     char * myfifo = "fifo1_2";
  44.     char * myfifo2 = "fifo2_3";
  45.     mkfifo(myfifo, 0666);
  46.     mkfifo(myfifo2,0666);
  47.  
  48.     char str1[80], str2[80];
  49.     while(1) {
  50.         fd1 = open(myfifo,O_RDONLY);
  51.         read(fd1, str1, 80);
  52.         int liczba = strlen(str1);
  53.         liczba--;
  54.         if(str1[0] != '@')
  55.             printf("%s\n", str1);
  56.         close(fd1);
  57.         fd2 = open(myfifo2,O_WRONLY);
  58.  
  59.         if(str1[0] == '@'){
  60.             liczba=0;
  61.             sprintf(str2, "%d", liczba);
  62.             write(fd2, str2, 80);
  63.             close(fd2);
  64.             return 0;
  65.         }
  66.  
  67.         else{
  68.             sprintf(str2, "%d", liczba);
  69.             write(fd2, str2, 80);
  70.             close(fd2);
  71.         }
  72.  
  73.         close(fd2);
  74.     }
  75.     return 0;
  76. }
  77. /*Trzeci*/
  78. #include <stdio.h>
  79. #include <string.h>
  80. #include <fcntl.h>
  81. #include <sys/stat.h>
  82. #include <sys/types.h>
  83. #include <unistd.h>
  84.  
  85. int main() {
  86.     int fd2;
  87.  
  88.     char * myfifo2 = "fifo2_3";
  89.  
  90.     char str1[80], str2[80];
  91.     while(1){
  92.         fd2 = open(myfifo2,O_RDONLY);
  93.         read(fd2, str1, 80);
  94.         if(str1[0] != '0')
  95.             printf("%s\n", str1);
  96.         close(fd2);
  97.         if(str1[0] == '0'){
  98.             remove("fifo2_3");
  99.             remove("fifo1_2");
  100.             return 0;
  101.         }
  102.     }
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement