![]() |
|
[C] pipe redirection of child's stdin and stdout doesnt work - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] pipe redirection of child's stdin and stdout doesnt work (/showthread.php?tid=4740300) |
pipe redirection of child's stdin and stdout doesnt work - mr.cracker - Mar 21, 2015 I was trying to redirect childs STDIN and STDOUT to pipes using dup2(). child has : execl("/bin/sh") I want to give commands from Parent. Child executes it. Write o/p to stdout, which is redirected to pipe. Then parent read from pipe, o/p to stdout of parent. my code is, Code #include<stdio.h>it got stuck on here, Code while((n = read(pipe2[0], buff, 4096)) > 0){Why child does not write into pipe2[1] ? I think thats why system waits for some data in read() function. Where i got mistake? How can i make it work? * - g4143 - Mar 21, 2015 I'm pretty sure you can't redirect a terminal's stdin and stdout that way. * - mr.cracker - Mar 21, 2015 (Mar 21, 2015 02:00 AM)g4143 Wrote: I'm pretty sure you can't redirect a terminal's stdin and stdout that way. sorry, now its working, the problem was here. Code if(buff[len - 1] == '\n')Since file stream is line buffered in terminal , i need to keep that '\n' to get output. * - g4143 - Mar 21, 2015 (Mar 21, 2015 05:05 AM)mr.cracker Wrote: sorry, now its working, the problem was here. Since file stream is line buffered in the shell. For some reason I thought you were attempting to do this. Code execl("/usr/bin/xterm", "/usr/bin/xterm", (char *)0);/*open a terminal as a child*/BTW, your program should have a return 0 at the end of main and it should #include <sys/types.h>. * - mr.cracker - Mar 21, 2015 (Mar 21, 2015 08:56 AM)g4143 Wrote: For some reason I thought you were attempting to do this. i tried above code. It doesn't redirect stdin and stdout.!!! Can you please tell me the reason ? I have updated my code with working code above in my question. now when i get a prompt i entered "LLL" : that is a wrong command. then i got, Code /bin/sh: 1: LLL: not foundAfter that it asks for input continuously. I think the child ask for input. Can you just check my code once more. Just run it. It is a working code. and try a invalid input. I could not find the reason. * - g4143 - Mar 21, 2015 (Mar 21, 2015 09:11 AM)mr.cracker Wrote: i tried above code. It doesn't redirect stdin and stdout.!!! Well is LLL a valid bash command or program on your system? Try opening a terminal and typing LLL at the prompt. * - mr.cracker - Mar 21, 2015 Can you please tell me, why below code doesn't redirect stdin and stdout ? Code execl("/usr/bin/xterm", "/usr/bin/xterm", (char *)0);/*open a terminal as a child*/ |