0

I'm looking for a method to call a specific command line call from my c++ program. This command need to be called mulitple times async. and then be canceled from user input.

Program flow is as follows:

  • Start Program waits for user to push a certain button.
  • When pressed, needs to fire the command.

    bmdcapture -C 0 -m 2 -F nut -m 11 -V 3 -A 2 -f pipe:1 | avconv -y -i - -strict experimental -q:v 1 -c:v mpeg4 '/var/www/recvideos/mytest0.mp4'
    
  • Then wait for the user to press the same button and terminate the above command.

I have the logic for the button press, but cant find a way to execute the command without it taking over the program. I have looked at fork(), but I'm uncertain it will work with the button press logic.

Any help would be appreciated

Here is the keypress watcher and at the time of this post I was playing with pthread.

while(1)
{
if (kbhit()) {
    int keypressed = getch();
    //printw("End Capture Key pressed! It was: %d\n", keypressed);
    printw("Capture will begin again when ctrl+r is pressed. \n");
    if(keypressed == 18)
    {
        //exit capture logic here
        int j;
        for(j=0; j < 1; j++)
        {
            pthread_cancel(threads[j]);
        }
        captureActive = false;
        break;
    }
}
else if(!captureActive)
{
    printw("Starting Capture...\n");
    //Begin Capture on all active cards
    int rc;
    int i;
    for(i=0; i < 1; i++)
    {
        rc = pthread_create(&threads[i], NULL, Capture, (void*)&threads[i]);

        if(rc)
        {
            cout << "ERROR: unable to create thread: " << rc << endl;
            exit(-1);
        }
    }

    captureActive = true;
}
else {
    //printw("capturing...\n");
    refresh();
    usleep(1000000);
}
}

1 Answer 1

2

If you append the & symbol to your command, then execute your command using system(), then your program will continue to run, while your command runs simultaneously in the background. For example:

sprint(cmd, "/path/to/your/command &");
system(cmd);
Sign up to request clarification or add additional context in comments.

5 Comments

Ok, so an extension to that conversation would be how do I terminate the execution of that command at the user interaction?
There are a few ways this can be done. One way is using signals. If the user is still interacting with the main program while the command is running in the background, and the user presses a key to indicate that the command should stop, then the main program can send a signal to the command to tell it to stop.
can you provide an example of this.
I have now gotten to the point that the system() does execute and the I am able to ineract in the original program. Problem now is that system() continues to execute. even after calling pthread_kill. pastebin link >> link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.