Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a child process by the parent process?

Tags:

c

fork

kill

I create a child process using a fork(). How can the parent process kill the child process if the child process cannot complete its execution within 30 seconds? I want to allow the child process to execute up to 30 seconds. If it takes more than 30 seconds, the parent process will kill it. Do you have any idea to do that?

like image 399
miraj Avatar asked Jun 28 '11 04:06

miraj


People also ask

Can a parent process kill a child process?

By default killing a parent process does not kill the children processes.

How can we stop the process of a child?

For killing a child process after a given timeout, we can use the timeout command. It runs the command passed to it and kills it with the SIGTERM signal after the given timeout. In case we want to send a different signal like SIGINT to the process, we can use the –signal flag.

How do you kill a child process in Python?

You can kill all child processes by first getting a list of all active child processes via the multiprocessing. active_children() function then calling either terminate() or kill() on each process instance.


1 Answers

Send a SIGTERM or a SIGKILL to it:

http://en.wikipedia.org/wiki/SIGKILL

http://en.wikipedia.org/wiki/SIGTERM

SIGTERM is polite and lets the process clean up before it goes, whereas, SIGKILL is for when it won't listen >:)

Example from the shell (man page: http://unixhelp.ed.ac.uk/CGI/man-cgi?kill )

kill -9 pid

In C, you can do the same thing using the kill syscall:

kill(pid, SIGKILL); 

See the following man page: http://linux.die.net/man/2/kill

like image 80
Mikola Avatar answered Sep 20 '22 02:09

Mikola