Reputation:
After incorporating Ben Voigt's answer into the code, it appears to work
Original question:
I'm trying to use dup2 to:
The final output is (blank), the file "in" is (blank) & the file "out" has the output of "ls -al".
Any ideas what could be happening?
int main()
{
pid_t pid;
int i;
int inFileDes,outFileDes;
inFileDes=open("in",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR);
outFileDes=open("out",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR);
for(i=0;i<3;i++)
{
if((pid=fork())==0)
{
switch(i)
{
case 0:
dup2(outFileDes,1);
execl("/bin/ls","ls","-al",0);
break;
case 1:
// originally:
dup2(outFileDes,0); // dup2(outFileDes,1);
dup2(inFileDes,1); // dup2(inFileDes,0);
execl("/bin/grep","grep","foo",0); //lines having foo
break;
case 2:
dup2(inFileDes,0);
execl("/bin/grep","grep","bar",0); //lines having foo & bar
break;
}
exit(-1); //in error
}
waitpid(pid,NULL,0);
}
close(inFileDes);
close(outFileDes);
return(0);
}
Upvotes: 1
Views: 700
Reputation: 283624
Your open
call creates an empty file "in" and none of the programs write to it, so that's expected. Since both instances of grep
read from an empty file, their output is also empty.
What you really want is to use the pipe
function to get a pair of handles, which is written to be one program and read from by the next. You'll need to call it twice because you have two sets of connections between child processes.
Upvotes: 1