Question

I've got a process that reads console input from user (readline, raw_input, etc. - depending on platform). There is another process that wants to feed the first process with a given input.

How to do this in linux? Is it possible? PS the language I'm interested is Python, but hints on any language/platform are appreciated.

Was it helpful?

Solution

The first option:

Python provides subprocess package to perform this task. You need to use pipes provided with subprocess package. Reference here.

The second option:

You can use multiprocessing package for better control and more options; pipes still available. Additionally you can use Queue, Array and Lock to facilitate interprocess communication. Reference here.

OTHER TIPS

In general pipes work well on unix-like systems. In C you would call popen which returns both ends of the pipe and then fork to produce a process that writes to the pipe and one that reads from it. The code is standard boilerplate:

int pdes[2];

pipe(pdes);
if ( fork() == 0 ) { 
    close(pdes[1]); 
    read( pdes[0]); /* read from parent */
    .....
}
else {          
         close(pdes[0]); 
     write( pdes[1]); /* write to child */
         .....
}

In Python you can use subprocesses and communicate using pipes as described in the python documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top