cmd_test.c 701 B

1234567891011121314151617181920212223242526272829303132
  1. #include "cmd_test.h"
  2. #include <libc/src/stdio.h>
  3. #include <libc/src/stdlib.h>
  4. #include <libc/src/string.h>
  5. #include <libc/src/unistd.h>
  6. int shell_pipe_test(int argc, char **argv)
  7. {
  8. int ret = -1;
  9. int fd[2];
  10. pid_t pid;
  11. char buf[512] = {0};
  12. char *msg = "hello world";
  13. ret = pipe(fd);
  14. if (-1 == ret) {
  15. printf("failed to create pipe\n");
  16. return -1;
  17. }
  18. pid = fork();
  19. if (0 == pid) {
  20. // close(fd[0]);
  21. ret = write(fd[1], msg, strlen(msg));
  22. exit(0);
  23. } else {
  24. // close(fd[1]);
  25. ret = read(fd[0], buf, sizeof(buf));
  26. printf("parent read %d bytes data: %s\n", ret, buf);
  27. }
  28. return 0;
  29. }