I\'m doing fork in my main program,and doing exec in the child process which will run another program. Now i want to terminate the child(i.e., the program invoked by exec) and r
Either in or outside your program, it is possible to use kill
. By including
, you can kill a process with a given PID (use the fork
return value to do this).
#include
int pid;
switch (pid = fork())
{
case -1:
/* some stuff */
break;
case 0:
/* some stuff */
break;
default:
/* some stuff */
kill(pid, SIGTERM);
}
It is also possible to use kill
command in the shell. To find the PID of your child process, you can run ps
command.
man kill
Thekill()
function shall send a signal to a process or a group of processes specified by pid. The signal to be sent is specified by sig and is either one from the list given inor 0. If sig is 0 (the null signal), error checking is performed but no signal is actually sent. The null signal can be used to check the validity of pid.