Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <sys/wait.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. void producent(char const* FILERS ,int pipefd)
  12. {
  13.  
  14. char bufor[100];
  15. FILE *file;
  16. file=fopen(FILERS, "r");
  17. int in= fopen("potok", "r"); //// dodane
  18.  
  19. if(file==NULL){
  20. perror("Error");
  21. exit(1);
  22. }
  23. while(fgets(bufor, 100, file) != NULL)
  24. {
  25. printf("Przesyłam dane : %s", bufor);
  26. write(pipefd, bufor, 100);
  27. }
  28. /////
  29. if( write( in , bufor , 100 ) == -1)
  30.             {
  31.             perror("Funckja write zwrocila blad ");
  32.             exit(2);
  33.             }
  34. //////
  35. printf("Send..\n");
  36. }
  37.  
  38. void konsument(char const* FILERS, int pipefd)
  39. {
  40. char bufor[100];
  41. FILE * file;
  42. file= fopen(FILERS, "w");
  43. int out= fopen("potok", "w");
  44. if(file==NULL){
  45. perror("Error");
  46. exit(1);
  47. }
  48.  
  49.  
  50. read(out, bufor,100);
  51.  
  52.  
  53.  
  54. int x;
  55. do{
  56. x=read(pipefd,bufor,100);
  57. if(x!=0)
  58. {
  59. fputs(bufor,file);
  60. printf("Odbieram dane : %s", bufor);
  61. }
  62.  
  63. }while(x>0);
  64. sleep(1);
  65. printf("Accepted\n");
  66. }
  67.  
  68.  
  69. ////////main/////////////////////////////
  70.  
  71.  
  72. #include <stdio.h>
  73. #include <unistd.h>
  74. #include <sys/types.h>
  75. #include <sys/stat.h>
  76. #include <fcntl.h>
  77. #include <limits.h>
  78. #include <sys/wait.h>
  79. #include <stdlib.h>
  80.  
  81. void producent( int );
  82. void konsument( int );
  83. char const* in="input.txt";
  84. char const* out="output.txt";
  85.  
  86. int main()
  87. {
  88. mkfifo("potok", 0644);
  89. int pipefd[2];
  90.     if (pipe(pipefd) == -1){
  91.         perror("Pipe error");
  92.         exit(EXIT_FAILURE);
  93.     }
  94.  
  95. switch(fork()) {
  96. case -1:
  97. perror("ErroR");
  98. exit(EXIT_FAILURE);
  99. break;
  100.  
  101. case 0:
  102. close(pipefd[0]);
  103. producent(in,pipefd[1]);
  104. close(pipefd[1]);
  105. break;
  106.  
  107. default:
  108. close(pipefd[1]);
  109. konsument(out,pipefd[0]);
  110. close(pipefd[0]);
  111. wait(NULL);
  112. break;
  113. }
  114. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement