Tobiahao

S01_LAB02_03

Nov 13th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. /*
  2. Napisz program, który stworzy potok, i wykorzysta go do przesy ł ania danych
  3. w obr ę bie jednego procesu.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11.  
  12. #define SOME_MESSAGE "Wiadomosc"
  13. #define BUFFER_SIZE 128
  14.  
  15. int main(void)
  16. {
  17.     int fd[2];
  18.     char buffer[BUFFER_SIZE];
  19.  
  20.     if(pipe(fd) == -1){
  21.         perror("pipe");
  22.         return EXIT_FAILURE;
  23.     }
  24.  
  25.     if(write(fd[1], SOME_MESSAGE, strlen(SOME_MESSAGE)) == -1){
  26.         perror("write");
  27.         return EXIT_FAILURE;
  28.     }
  29.  
  30.     printf("Wiadomosc wyslana: %s\n", SOME_MESSAGE);
  31.    
  32.     if(close(fd[1]) == -1){
  33.         perror("close");
  34.         return EXIT_FAILURE;
  35.     }
  36.  
  37.     memset(buffer, '\0', BUFFER_SIZE);
  38.     if(read(fd[0], buffer, BUFFER_SIZE) == -1){
  39.         perror("read");
  40.         return EXIT_FAILURE;
  41.     }
  42.  
  43.     printf("Odczytana wiadomosc: %s\n", buffer);
  44.  
  45.     if(close(fd[0]) == -1){
  46.         perror("close");
  47.         return EXIT_FAILURE;   
  48.     }
  49.  
  50.     return EXIT_SUCCESS;
  51. }
Add Comment
Please, Sign In to add comment