Question:
Is it possible to terminate a child process without waiting for it to finish using the wait()
function? For example, how to terminate the following child process:
pid_t pid;
switch(pid=fork()) {
case 0:
for(;;){}
default:
//здесь код, который завершает дочерний процесс
return 0;
PS If you know some adequate, not very tricky material on fork()
, where you can get theoretical knowledge, then direct me, please.
Answer:
Cruel method:
kill(pid, SIGKILL);
Or more gently:
kill(pid, SIGTERM);
In the first case, the child process will simply be killed; in the second case, it will receive a signal that it can process. But SIGKILL
is guaranteed to kill the process, and in the case of SIGTERM
, the process may refuse to terminate.
In the above code, since the process does not seem to contain any signal handlers, either method can be used, since, as @avp rightly pointed out to my mistake, SIGTERM
will also kill the process if no signal handler is present.