Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Killing and restarting a process in a C++ Program

Status
Not open for further replies.

menace212

Programmer
Jul 11, 2003
144
US
How would you kill and restart a process in a C++ program on the Unix platform...Such as

if (pid == "/usr/sbin/in.named") then

kill("/usr/sbin/in.named process")


Then attempt to restart the process. How would I go about running this in a C++ program on Unix.
 
you can kill a process by sending it SIGKILL/SIGTERM from another process.

The only condition is that the signal receiving thread shall not have masked that signal.

You can always start a new process by doing a fork() and then exec().
fork will create a new process and exec() will execute yoiur required process.

Hope this helps.
 
Hei U can use kill(process id,9) to kill the process.
And use execlp("exe_name",(char*)0) to restart the exe named exe_name.But the exe shall be in u r PATH.
Good Luck
 
Ansam is right, you can send a signal to any other process by calling the kill() API. SIGKILL is 9. So,
kill(procId, 9) will send a SIGKILL to your required procId.

After that you can always start a new process.
But dont directly use execlp(). Do a fork first, or the process you are running(this process that wants to monitor & restart a remote process) will be overwritten by the new process.

Actually exec() family of APIs help you overwrite the calling process' code/data segment with that of the process name as an argument. As soon as you call exec() from a process, theat process is overwritten with the address spcea of the argument process. And the argument process starts running. So in case you dont want your monitor process to exist, call exec() directly. Otherwise, do a fork and then call exec().

 
I still don't quite get it...The kill and restart procedure is in the program.How will the program know what pid to kill when it's already running.I think I need the program to figure out what the pid is then kill it(in.named) For instance if I take the program that I'm writing and run it on another box to kill in.named it won't know it's pid. Can you give me a programming example of how this would work if I wanted to stop and restart in.named?
 
Here is a simplest method,
say initially nothing is running,
you have a monitor process say MON and you have a process that can be restarted say PROC.

Let your monitor initially start it,

in MON you have something like this,
while(1)
{
oldpid = pid;
pid = fork();
if(pid == 0)
{
// Child
if(oldpid != 0)
kill(oldpid,SIGKILL);

exec(PROC);
}
else
{
sleep(1 hour);
}
}


Basically the catch is you get a pid when you do a fork. This pid will not change when you do a exec. Only a new process will start executing taking the pid from the process that called exec().

Well if you want tougher times, you can go through the process table entries and actually peek into the process that is running.


Hope this helps.
 
So what your saying is I have a in.named running and if I want to stop and restart the process. I test the condition in.named, find out what the pid is stop it and then restart by using exec().

example:

If ("in.named" = pid) {

while(1)
{
oldpid = pid;
pid = fork();
if(pid == 0)
{
// Child
if(oldpid != 0)
kill(oldpid,SIGKILL);

exec(PROC);
}
else
{
sleep(1 hour);
}
}
}

 
Well not exactly,
I am saying start initially your in.named through your monitor process only. Because your process will keep track of the pid being assigned to the in.named.

See initially neither in.named nor your process is running on a box.

You run your process which will first start the in.named. When it starts the in.named, it will store its pid.Then it will wait for your input to restart the in.named. As you have its pid already, so you can easily send a SIGKILL to it and then start it again. And then again you can store the pid of the restarted in.named.


while(1)
{
oldpid = pid; // Initially = 0 when in.named is not running
pid = fork();
if(pid == 0)
{
// Child, Dont send SIGKILL when in.named is not running
if(oldpid != 0)
kill(oldpid,SIGKILL);


exec(in.named);
}
else
{
sleep(1 hour); // or you can wait for an input.
}
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top