I have a compiled program written in C++ for a UNIX environment which has this kind of structure:
int main(){
...
LoadEngine()
...
while(1){
std::cin >> buffer;
...
ExecuteFunction(buffer);
}
}
Loading the engine takes quite a while, so I'm trying to start the program in the background first, then send the input whenever I need to.
Running the program with the standard ampersand appended at the end seemingly does not make the program run in the background, but rather halts at std::cin until an input is received from the console, and stops after an input is accepted from the console.
How do I execute the program such that the program runs continuously in the background and receive input and execute the function whenever it is needed?
EDIT: The final product is a small device (RaspberryPi) which recognizes speech, and does something based on the words recognized. The program that I have is the part where the device does something based on the word input, and the word input corresponds to the variable buffer from the code snippet above.
So the std::cin part is a dummy line code that I'm using for testing out that my part of the code starts up in the background process (to load the engine) and does whatever it is designed to do.
EDIT 2 : To clarify what I’m trying to achieve, the program takes an input from a speech recognizer and does things (e.g. synthesize speech from input, send out signals to LEDs, etc.). The text input can be taken directly from the console (which my code is currently doing), or some other method that I’m not knowledgeable in. The only thing that the input adheres to is that it must be a text, and is sent from another program that recognizes speech (which is handled by some other developer). So the exact method isn’t specified. All I have to worry about is my side of the program, which executes a function from a text input (i.e. buffer from the code snippet). So the general structure would look something like this:
Int main(){
LoadEngine()
while(1){
buffer = ReceiveInput();
ExecuteFunction(buffer);
}}
Where the ReceiveInput() part is currently implemented as std::cin. It really can be any method, as long as the engine is loaded once in the beginning, and the program is able to perform ExecuteFunction from an input until the device is turned off.
std::cin
part is a dummy line code that I’m using for testing … the program … halts atstd::cin
until an input is received from the console … WHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAT? Your program is failing because of a line of code that’s there only for testing? Why don’t you just take it out? What you are saying doesn’t make any sense to me. Please try to explain this better (in the question, as much as is practical). How do you expect your program to get audio input (from the microphone, presumably)? Are you running your program with stdin redirected, or piping something into it?their_program | your_program &
. Since the pipe (from their program) is now the standard input (stdin) to your program, the code you have now to read fromstd::cin
should work, unchanged. Test withyour_testscript | your_program &
, where your test script does things likesleep 600
,echo "The quick brown fox ..."
,sleep 420
,echo "jumps over the lazy dog."
, ...