c - How execv gets the output from pipe? -


referring old homework question : /* implementing "/usr/bin/ps -ef | /usr/bin/more" */ using pipes.

#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main() { int fds[2]; int child[2]; char *argv[3]; pipe(fds); if (fork()== 0) { close(fds[1]); close(stdin_fileno); dup(fds[0]); /* redirect standard input fds[1] */ argv[0] = "/bin/more"; argv[1] = null; /* check how argv array set */ execv(argv[0], argv);// here how execv reads stdin ?? exit(0); } if (fork() == 0) { close(fds[0]); close(stdout_fileno); dup(fds[1]); /* redirect standard output fds[0] */ argv[0] = "/bin/ps"; argv[1] = "-e"; argv[2] = null; execv(argv[0], argv); exit(0); } close(fds[1]); wait(&child[0]); wait(&child[0]); } 

after redirecting fd standard output, how execv reads it. inbuilt in execv reads standard input before executing command? unable concept.

your question based on false premise -- execv doesn't read anywhere, nor need to. more reads stdin inherits across call execv. reason more reads stdin because it's filter and, filters, defaults reading stdin if input source isn't specified on command line. (otherwise, /usr/bin/ps -ef | /usr/bin/more wouldn't work.)


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -