Reputation: 259
Below is the code which I am trying to get it working....
I was expecting the output as
OUTPUT from PipeAttempt(args1, args2)
followed by
I am here
OUTPUT from PipeAttempt(args3, args4)
But in reality, I get only output from PipeAttempt(args1, args2);
and the program waits for input from me, when I hit the enter key the program terminates
Can you please let me know what am I missing here ?
int main () {
char* args1 [] = {"/usr/bin/head", "/etc/passwd", NULL};
char* args2 [] = {"/bin/sort", NULL};
char* args3 [] = {"/bin/cat", "piped.input", NULL};
char* args4 [] = {"/usr/bin/wc", NULL};
PipeAttempt(args1, args2);
printf("I am here\n");
PipeAttempt(args3, args4);
return 0;
}
void PipeAttempt(char* args1[], char* args2[]) {
int pfildes[2]; <br>
pid_t cpid1, cpid2; <br>
char *envp[] = { NULL };<br>
if (pipe(pfildes) == -1) {perror("demo1"); exit(1);}
if ((cpid1 = fork()) == -1) {perror("demo2"); exit(1);}
else if (cpid1 == 0) { /* child: "cat a" */
close(pfildes[0]); /* close read end of pipe */
dup2(pfildes[1],1); /* make 1 same as write-to end of pipe */
close(pfildes[1]); /* close excess fildes */
execve(args1[0], args1, envp);
perror("demo3"); /* still around? exec failed */
exit(1); /* no flush */
}
else { /* parent: "/usr/bin/wc" */
close(pfildes[1]); /* close write end of pipe */
dup2(pfildes[0],0); /* make 0 same as read-from end of pipe */
close(pfildes[0]); /* close excess fildes */
execve(args2[0], args2, envp);
perror("demo4"); /* still around? exec failed */
exit(1); /* parent flushes */
}
}
Upvotes: 1
Views: 508
Reputation: 985
Your original parent was overwritten by the exec call.So you were only able to get the output of /usr/bin/head /etc/passwd | /bin/sort and you did not get the get the output of /bin/cat piped.input|/usr/bin/wc.
The program actually did not wait for you for keyboard input. What happended is that the shell prompt was not shown.The parent exited before the child and the prompt was immediatly shown by the shell(which waits for the parent) and the output from the child came after that, which made you think that the program is waiting for user input.
Here to solve this you can fork another child in the function, so that effectively for each function (PipeAttempt) call two child will handle the pipe and in main you can wait for all the four child to complete, so that the parent exits at last and you get back the shell prompt. Given the modified code below, below pl. include the header files for wait, strlen etc before compiling. Hope this solves your query.
void PipeAttempt(char* args1[], char* args2[]) ;
int main () {
int i;
char* args1 [] = {"/usr/bin/head", "/etc/passwd", NULL};
char* args2 [] = {"/bin/sort", NULL};
char* args3 [] = {"/bin/cat", "piped.input", NULL};
char* args4 [] = {"/usr/bin/wc", NULL};
PipeAttempt(args1, args2);
write(1, "I am here\n", strlen("I am here\n"));
PipeAttempt(args3, args4);
for (i=0; i < 4; i++)
{
wait(NULL);
}
return 0;
}
void PipeAttempt(char* args1[], char* args2[])
{
int pfildes[2];
pid_t cpid1, cpid2;
char *envp[] = { NULL };
if (pipe(pfildes) == -1) {perror("demo1"); exit(1);}
if ((cpid1 = fork()) == -1) {perror("demo2"); exit(1);}
else if (cpid1 == 0)
{ /* child1 */
close(pfildes[0]); /* close read end of pipe */
dup2(pfildes[1],1); /* make 1 same as write-to end of pipe */
close(pfildes[1]); /* close excess fildes */
execve(args1[0], args1, envp);
perror("demo3"); /* still around? exec failed */
exit(1); /* no flush */
}
else {
/* child2: "/usr/bin/wc" */
if (0==fork())
{
close(pfildes[1]); /* close write end of pipe */
dup2(pfildes[0],0); /* make 0 same as read-from end of pipe */
close(pfildes[0]); /* close excess fildes */
execve(args2[0], args2, envp);
perror("demo4"); /* still around? */
exit(1);
}
//Parent
close(pfildes[0]);
close(pfildes[1]);
}
}
Upvotes: 1
Reputation: 5722
Your PipeAttempt() function does a fork/exec - how can you expect that it returns to the main function?
Upvotes: 0