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