PDA

View Full Version : C++ execute/terminate another application



iharrold
February 3rd, 2009, 08:03 PM
I am wondering if there is a simple way to execute / terminate an application from a control application.

I have an application which communicates to another application over local sockets. Every 1/4 second I get a heartbeat message. If I don't hear that heartbeat message after so many missed instances, I want to try and kill the process then restart it.

I have the two applications written, I am just trying to figure out how to control them. And I don't mean to fork() from my current process another child.

jimi_hendrix
February 3rd, 2009, 08:21 PM
could always use terminal commands...

system("kill process_name");

iharrold
February 3rd, 2009, 09:49 PM
Oh my... Jimi... I honestly didn't even think of doing it like that...

Much appreciated.:popcorn:

Any idea how to grab the pid of the application?

Nevermind... "killall commandname"

Hrmm... system() is a blocking call.

I.e.


std::cout<<"Starting Killapp..."<<std::endl;
std::system("~/workspace/SpawnTester.cpp/Killapp");

std::cout<<"Killing Killapp..."<<std::endl;
std::system("killall Killapp");


waits on the first system call

Edit... this seems to make it nonblocking...


std::cout<<"Starting Killapp..."<<std::endl;
std::system("~/workspace/SpawnTester.cpp/Killapp&");

std::cout<<"Killing Killapp..."<<std::endl;
std::system("killall Killapp");

dribeas
February 3rd, 2009, 10:21 PM
std::cout<<"Starting Killapp..."<<std::endl;
std::system("~/workspace/SpawnTester.cpp/Killapp");

std::cout<<"Killing Killapp..."<<std::endl;
std::system("killall Killapp");



Adding a & character should help there:



std::system("~/workspace/SpawnTester.cpp/Killapp &");


That will make the shell start your Killapp in background and return control to your program right away. Not the cleanest solution, but more of a fast-n-dirty trick.

BTW, you might consider using some scripting language.