GoralWMoro

Untitled

Dec 16th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #define SIZE_BUFFOR 100
  7. int main() {
  8.  
  9. pid_t proces1, proces2;
  10. char buffor[SIZE_BUFFOR];
  11. int pipe1[2];
  12. int pipe2[2];
  13. int pipe3[2];
  14. int pipe4[2];
  15.  
  16.  
  17. if(pipe(pipe1) < 0){
  18. exit(10);
  19. }
  20.  
  21. if(pipe(pipe2) < 0){
  22. exit(10);
  23. }
  24.  
  25. if(pipe(pipe3) < 0){
  26. exit(10);
  27. }
  28.  
  29. if(pipe(pipe4) < 0){
  30. exit(10);
  31. }
  32.  
  33.  
  34.  
  35. proces1 = fork();
  36.  
  37. switch(proces1){
  38. case 0:
  39. {
  40. printf("PP1:%d\n", getpid());
  41. char tekst[SIZE_BUFFOR];
  42. printf("Podaj tekst: ");
  43. scanf("%s", tekst);
  44. close(pipe1[0]);
  45. write(pipe1[1], tekst, SIZE_BUFFOR);
  46.  
  47. char odczyt_konwersji[SIZE_BUFFOR];
  48. close(pipe4[1]);
  49. read(pipe4[0], odczyt_konwersji, SIZE_BUFFOR);
  50. printf("WYNIK: %s\n", odczyt_konwersji);
  51. exit(0);
  52.  
  53. }
  54. case -1:
  55. printf("Blad funkcji fork\n");
  56. exit(1);
  57. default:
  58.  
  59.  
  60. close(pipe1[1]);
  61. read(pipe1[0], buffor, SIZE_BUFFOR);
  62.  
  63. write(pipe2[1], buffor, SIZE_BUFFOR);
  64.  
  65.  
  66. }
  67.  
  68.  
  69. proces2 = fork();
  70. switch(proces2){
  71. case 0:
  72. {
  73.  
  74. char tekst_do_konwersji[50];
  75. close(pipe2[1]);
  76. read(pipe2[0], tekst_do_konwersji, SIZE_BUFFOR);
  77.  
  78.  
  79. for(int i = 0; i < sizeof(tekst_do_konwersji); i++){
  80. tekst_do_konwersji[i] = toupper(tekst_do_konwersji[i]);
  81. }
  82.  
  83. close(pipe3[0]);
  84. write(pipe3[1], tekst_do_konwersji, SIZE_BUFFOR);
  85.  
  86. exit(0);
  87. }
  88. case -1:
  89. printf("Blad funkcji fork\n");
  90. exit(1);
  91. default:
  92. wait(NULL);
  93. close(pipe3[1]);
  94. read(pipe3[0], buffor, SIZE_BUFFOR);
  95.  
  96. close(pipe4[0]);
  97. write(pipe4[1], buffor, SIZE_BUFFOR);
  98.  
  99.  
  100. }
  101.  
  102. wait(NULL);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment