Results 1 to 6 of 6
Hi,
I am trying to write a program that can start a new process and communicate with it interactively. I want to be able to grab the output as soon ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-03-2010 #1Just Joined!
- Join Date
- Sep 2008
- Location
- SC
- Posts
- 48
pipes for process communication
Hi,
I am trying to write a program that can start a new process and communicate with it interactively. I want to be able to grab the output as soon as it is available and also be able to write to the newly created process's stdin. I am using Python, but I think the help I need is not relevant to the language but rather the lowlevel C functions, which are available in Python. Here is the pseudo code for what I have tried doing:
I think my issue is getting the the stdout and stdin of the child process redirected to the file descriptors returned by the call to pipe(). From what I have read, execvp() will preserve open fd's so perhaps the way I am reassigning stdin and stdout is wrong?Code:childr, parentw = pipe() parentr, childw = pipe() fork() if child stdin = childr stdout = childw execvp(.....) if parent while true readlines from parentr get input write input to parentw
I have looked at the pexpect module but it is not available on the production machines I need to do this on.Last edited by foxcar; 10-03-2010 at 03:31 PM.
- 10-04-2010 #2Just Joined!
- Join Date
- Oct 2009
- Posts
- 85
If i understand correct, are u directly assigning the childr and childw to stdin and stdout..
If you want so.. use the dup call. that will redirect the stdin to childr and stdout to childw.
Hope this helps.
- 10-04-2010 #3Just Joined!
- Join Date
- Sep 2008
- Location
- SC
- Posts
- 48
Ok here is what I have. I want to use this script to interact with another process, using the python script as an interface.
But, I get an error message saying:Code:childr, parentw = pipe() parentr, childw = pipe() fork() if child dup2(childr, 0) dup2(childw, 1) dup2(childw, 2) execvp('...t') if parent while true readlines from parentr get input from stdin write input to parentw
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell. I am trying to run ssh in my tests, btw.Last edited by foxcar; 10-04-2010 at 07:52 PM.
- 10-04-2010 #4Just Joined!
- Join Date
- Oct 2009
- Posts
- 85
Can you give me at what line u r getting the warning..
I dont understand y are u getting "no access to tty"
- 10-04-2010 #5Just Joined!
- Join Date
- Sep 2008
- Location
- SC
- Posts
- 48
There is no line number. Once execvp() runs, ssh throws the error and I read it in my while loop. It seems that calling dup() on stdin is what triggers it.
- 10-05-2010 #6Just Joined!
- Join Date
- Sep 2008
- Location
- SC
- Posts
- 48
I think I may have figured it out. I looked through the source code for a module called pexpect and communication is through a pseudo terminal, not a pipe. Seems to be working decently.


Reply With Quote
