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
You need to do the following:
kill(pid, SIGTERM
) first - this gives the child process an opportunity to terminate gracefullysleep
). The period of time depends on the time the child process takes to close down gracefully.waitpid(pid, &status, WNOHANG)
checking the return value. If the process has not aborted do step 4kill(pid, SIGKILL
) then harvest the zombie by doing waitpid(pid, &status, 0)
.These steps ensure that you give the child process to have a signal handler to close down and also ensures that you have no zombie processes.