Reputation: 1
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:
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);
}
}
Upvotes: 0
Views: 633
Reputation: 12037
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);
Upvotes: 2