您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

2 年前
1234567891011121314151617181920212223242526272829303132333435
  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. }