pborawski

pipes

Oct 29th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <signal.h>
  5.  
  6.  
  7. void main ()
  8.  
  9. {
  10.  
  11. int filedes[2];
  12.  
  13. if (pipe(filedes) == -1)
  14. {
  15.  
  16. perror("Tworzenie potoku");
  17.  
  18. exit(1);
  19. }
  20.  
  21. switch(fork())
  22. {
  23.  
  24. case -1: // blad w tworzeniu procesu
  25.  
  26. perror("Tworzenie procesu");
  27.  
  28. exit(1);
  29.  
  30. case 0: // proces potomny
  31. if (write(filedes[1], "Hello world!", 12) == -1)
  32. {
  33. perror("Zapis do potoku");
  34. exit(1);
  35. }
  36. exit(0);
  37.  
  38. default:
  39. {// proces macierzysty
  40. char buf[12];
  41. if (read(filedes[0], buf, 12) == -1)
  42. {
  43. perror("Odczyt z potoku");
  44. exit(1);
  45. }
  46. printf("Odczytano z potoku: %s\n", buf);
  47.  
  48. }
  49.  
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment