Implement Pipe in C -
i implementing pipe in c. when try command 'cat aa | grep "something" ' in program. grep process hanging there, seems waiting input. don't know why. here core code. take executecommand call execve function , arguments correctly passed.
if ((pid = fork()) < 0) { perror("fork failed\n"); exit(1); } if (pid) { // parent pipe writer close(pd[0]); close(1); // replace input pipe dup(pd[1]); // recursively call next commands executecommand(cmds, env); freecommandsarray(&cmds); exit(0); } else { // child pipe reader close(pd[1]); close(0); // close read end dup(pd[0]); executecommand(*(++splitcmds), env); freecommandsarray(&cmds); exit(0); }
the full code open. problem have use full path of command file first parameter execve (e.g. /bin/ls ls), otherwise, got error message, no such file existed.
it quotation mark @ first argument of grep cause problem. works if rid of on input. e.g 'cat aa | grep drw' instead of 'cat aa | grep "something"'
Comments
Post a Comment