Advertisement
999ms

Untitled

Dec 10th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int main() {
  7. int fp[2], b = 0;
  8. char buf[512] = "\0";
  9.  
  10. if (pipe(fp) == 0) {
  11. switch (fork()) {
  12. case -1:
  13. perror("Fork failed");
  14. exit(1);
  15. case 0:
  16. close(fp[0]);//конец канала для чтения
  17. printf("child process %d\n", getpid());
  18.  
  19. b = read(0, buf, sizeof(buf));
  20. printf("child read %d bytes \n", b);
  21. b = write(fp[1], buf, b);
  22.  
  23. close(fp[1]);
  24. exit(0);
  25. default:
  26. close(fp[1]);//конец канала для записи
  27. printf("PARENT process %d\n", getpid());
  28.  
  29. while((b = read(fp[0], buf, 1)) != 0){
  30. write(1, buf, b);
  31. printf("\n");
  32. }
  33.  
  34. close(fp[0]);
  35. exit(0);
  36. }
  37. } else perror("Pipe failed");
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement