Find the answer to your Linux question:
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.
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Location
    SC
    Posts
    54

    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:

    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 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?

    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 04:31 PM.

  2. #2
    Just 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.

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Location
    SC
    Posts
    54
    Ok here is what I have. I want to use this script to interact with another process, using the python script as an interface.

    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
    But, I get an error message saying:
    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 08:52 PM.

  4. $spacer_open
    $spacer_close
  5. #4
    Just 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"

  6. #5
    Just Joined!
    Join Date
    Sep 2008
    Location
    SC
    Posts
    54
    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.

  7. #6
    Just Joined!
    Join Date
    Sep 2008
    Location
    SC
    Posts
    54
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •