开发者

2-way communication with background process (I/O)

开发者 https://www.devze.com 2023-04-03 19:43 出处:网络
I have a program that runs in the command line (i.e. $ run program starts up a prompt) that runs mathematical calculations. It has it\'s own prompt that takes in text input and responds back through s

I have a program that runs in the command line (i.e. $ run program starts up a prompt) that runs mathematical calculations. It has it's own prompt that takes in text input and responds back through standard-out/error (or creates a separate x-window if needed, but this can be disabled). Sometimes I would like to send it small input, and other times I send in a large text file filled with a series of input on each line. This program takes a lot of resources and also has a large startup time, so it would be best to only have one instance of it running at a time. I could keep open the program-prompt and supply the input this way, or I can send the process with an exit command (to leave prompt) which just prints the output. The problem with sending the request with an exit command is that the program must startup each time (slow ...). Furthermore, the output of this program is sometimes cryptic and it would be helpful to filter the output in some way (eg. simplify output, apply ANSI colors, etc).

This all makes me want to put some 2-way IO filter (or is that "pipe"? or "wrapper"?) around the program so that the program can开发者_C百科 run in the background as single process. I would then communicate with it without having to restart. I would also like to have this all while filtering the output to be more user friendly. I have been looking all over for ideas and I am stumped at how to accomplish this in some simple shell accessible manor.

Some things I have tried were redirecting stdin and stdout to files, but the program hangs (doesn't quit) and only reads the file once making me unable to continue communication. I think this was because the prompt is waiting for some user input after the EOF. I thought that this could be setup as a local server, but I am uncertain how to begin accomplishing that.

I would love to find some simple way to accomplish this. Additionally, if you can think of a way to perform this, do you think there is a way to also allow for attaching or detaching to the prompt by request? Any help and ideas would be greatly appreciated.


You could create two named pipes (man mkfifo) and redirect input and output:

myprog < fifoin > fifoout

Then you could open new terminal windows and do this in one:

cat > fifoin

And this in the other:

cat < fifoout

(Or use tee to save the input/output as well.)

To dump a large input file into the program, use:

cat myfile > fifoin
0

精彩评论

暂无评论...
验证码 换一张
取 消