Advertisement
YellowAfterlife

Unix, named pipe communication (ru)

May 2nd, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. /// Unix, лабораторная работа №3, вариант №6
  2. /// 2012/05/03 0:32
  3. /// Компиляция:
  4. /// * Запустить два терминала
  5. /// * gcc server.c -o server.out
  6. /// * gcc client.c -o client.out
  7. /// * В первом терминале запустить "./server.out"
  8. /// * Во втором терминале запустить "./client.out"
  9. /// * После работы программы может понадобится удалить файл "fifo", появляющийся в рабочей папке программы.
  10. /// Файлы:
  11. /// server.c:
  12. #include <unistd.h>
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. int fdata, fout;
  16. char nextch[1];
  17. int main() {
  18.     printf("Server up.\n");
  19.     if (mkfifo("fifo", 0666, 0) < 0) {
  20.         perror("Error in server/mkfifo"); return 1;
  21.     }
  22.     //
  23.     fout = open("fifo", O_WRONLY);
  24.     if (fout < 0) {
  25.         perror("Error in server/open/fifo"); return 2;
  26.     }
  27.     //
  28.     fdata = open("data.txt", O_RDONLY);
  29.     if (fdata < 0) {
  30.         perror("Error in server/open/data.txt"); return 3;
  31.     }
  32.     //
  33.     while (read(fdata, nextch, 1)) {
  34.         if (nextch[0] == EOF) break;
  35.         printf("> %c\n", nextch[0]);
  36.         write(fout, nextch, 1);
  37.     }
  38.     close(fdata);
  39.     return 0;
  40. }
  41. /// client.c:
  42. #include <stdio.h>
  43. #include <fcntl.h>
  44. #include <string.h>
  45. char nextch[2], lines[2][256];
  46. int filein, chnum = 0, line = 0;
  47.  
  48. int main() {
  49.     printf("Client up.\n");
  50.     filein = open("fifo", O_RDONLY);
  51.     FILE* fileout = fopen("out.txt", "w");
  52.     if (filein < 0) {
  53.         perror("Error in client/open/fifo"); return 1;
  54.     }
  55.     //
  56.     nextch[1] = 0; // нужно для того чтобы делать strcat с символом
  57.     while (read(filein, nextch, 1) > 0) {
  58.         if (nextch[0] == 10) { // конец строки
  59.             if (line & 1) { // если это конец четной строки
  60.                 fprintf(fileout, "%s\n%s\n", lines[1], lines[0]); // выводим строки
  61.                 strcpy(lines[1], ""); // сбрасываем строки
  62.                 strcpy(lines[0], "");
  63.             }
  64.             line++; // инкрементируем индекс
  65.         } else { // просто символ, дописываем в соотв. строку:
  66.             strcat(lines[line & 1], nextch);
  67.         }
  68.         chnum++;
  69.     }
  70.     if (line & 1) { // если это четная строка которая не завершилась
  71.         fprintf(fileout, "%s\n%s", lines[1], lines[0]);
  72.     } else if (strlen(lines[0])) { // если это нечетная не пустая строка
  73.         fprintf(fileout, "%s", lines[0]);
  74.     }
  75.     // немного статистики:
  76.     printf("Total symbols: %d.\nTotal lines: %d.\n", chnum, line);
  77.     close(filein);
  78.     fclose(fileout);
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement