How to kill a child process by the parent process?

前端 未结 3 1933
野的像风
野的像风 2020-12-08 04:32

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?

3条回答
  •  不知归路
    2020-12-08 05:15

    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.

提交回复
热议问题