You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

36 lines
721 B

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h>
  8. int main() {
  9. int fd[2];
  10. if (pipe(fd) == -1) {
  11. perror("create pipe");
  12. exit(1);
  13. }
  14. char mes[1024];
  15. switch (fork()) {
  16. case -1:
  17. perror("fork");
  18. exit(1);
  19. case 0:
  20. close(fd[1]);
  21. read(fd[0], mes, 1024);
  22. printf("%s\n", mes);
  23. close(fd[0]);
  24. break;
  25. default:
  26. close(fd[0]);
  27. fgets(mes, 1024, stdin);
  28. write(fd[1], mes, 1024);
  29. close(fd[1]);
  30. int status;
  31. wait(&status);
  32. }
  33. }