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?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Try something like this:
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 )
In C, you can do the same thing using the kill syscall:
See the following man page: http://linux.die.net/man/2/kill
In the parent process, fork()'s return value is the process ID of the child process. Stuff that value away somewhere for when you need to terminate the child process. fork() returns zero(0) in the child process.
When you need to terminate the child process, use the kill(2) function with the process ID returned by fork(), and the signal you wish to deliver (e.g. SIGTERM).
Remember to call wait() on the child process to prevent any lingering zombies.