I sometimes write program like this to process offline data:
load_model() //this may cost lots of time
while(cin >> inputs)
{
result = process_input(inputs)
cout << result
}
release_model()
This works fine if I only have to process offline data. However, when the data comes one by one I am in trouble. Since I have to load the model everytime which is time consuming.
I wonder if there is any way to CONVERT this program into a service WITHOUT modify the program itself. For example, I can redirect the cin and cout to two named pipes:
program < namedpipe_in > namedpipe_out
The I can put the inputs into the namedpipe_in like this
cat input > namedpipe_in
and read the result in another pipe:
cat namedpipe_out
However, this solution will not work since once I cat something to the namedpipe_in, the pipe will be close after cat operation and the program exits.
My question is how to fix this problem and make the pipes looks more like a queue instead of a memory buffer.
Thanks for your time reading.
Perhaps I am misunderstanding your question; please correct me if this is not what you are looking for.
To simulate your example, I wrote a simple C++ program which just takes in each input string and reverses it:
The output binary of my example program is
strflipper
.I have another file called
in.log
which is just an input file which I created viatouch in.log
.Now I call
and if I add something to the input log in a different terminal, the output log gets adjusted as such:
which of course is my expected output. As long as I don't kill the program, anything I add into
in.log
will automatically be processed within that loop without killing or restartingstrflipper
.