How to kill child of fork?
Send a signal.
#include <sys/types.h>
#include <signal.h>
kill(pid, SIGKILL);
/* or */
kill(pid, SIGTERM);
The second form preferable, among other, if you'll handle signals by yourself.
See kill system call. Usually a good idea to use SIGTERM first to give the process an opportunity to die gratefully before using SIGKILL.
EDIT
Forgot you need to use waitpid to get the return status of that process and prevent zombie processes.
A FURTHER EDIT
You can use the following code:
kill(pid, SIGTERM);
bool died = false;
for (int loop; !died && loop < 5 /*For example */; ++loop)
{
int status;
pid_t id;
sleep(1);
if (waitpid(pid, &status, WNOHANG) == pid) died = true;
}
if (!died) kill(pid, SIGKILL);
It will give the process 5 seconds to die gracefully
Issue kill(pid, SIGKILL)
from out of the parent.