views:

290

answers:

5

Who in the kernel is responsible for killing a process.

What if a "kill" comes, and the process is in the blocked state. Does the kill waits until the process comes to running state to clean himself.

If someone can answer more in terms of kernel, like when a SIGINT from the kill command is generated, what all is invoked by the kernel until the TCBs (task control blocks) are cleared in the end.

+1  A: 

Running kill simply sends a signal to the process (SIGINT) asking it nicely to terminate. If it won't respond that's it's business. However, you can choose to send any one of several different signals commanding it to go away. What you may be interested in is kill -9 (SIGKILL) which kills it without giving it a choice in the matter.

joeslice
Hi Joeslice, i asked more in terms of kernel, who will pick the SIGINT, and what-all comes into picture when "Mr. ABC process" in the kernel receives the signal.
Vivek Sharma
A: 

Killing (rather than interrupting) is usually performed by the SIGKILL signal in UNIX systems (CTRL-C sends SIGINT).

These systems usually provide a method of interrupting blocking system calls by a signal, which allows the signal handler to execute without waiting on a system call to complete (This is where the EINTR error comes into play). So normally, the call is just cancelled, without waiting for it to complete.

Matthew Iselin
+5  A: 

Killing it with a signal other than SIGKILL, just causes a signal to be sent. This can be masked or ignored, but assuming it isn't (or after it's unmasked), then it interrupts the normal running of the program.

If an IPC-type system call is in progress (e.g. reading from a socket, select(), poll(), sleep() etc), then it will be interrupted and fail with EINTR in errno. A properly written application will re-issue the call after handling the signal.

The process then immediately executes a call to the signal handler, which may return to allow processing to continue, or it could call longjmp (in C), or it could exit the process, which is normally the default.

SIGKILL is completely different, none of the above happens. Instead it just quits the system call (which would presumably leave EINTR in errno, if the process was allowed to read it), then causes the task to exit immediately with no possibility to handle it.

But either of them I think waits for a "D" "uninterruptable sleep" state to finish. This would normally be something like a blocking disc read, page fault demand-load or something.

MarkR
longjmp() is not async-safe and should not be called from a signal handler.
sigjuice
`SIGSTOP` can't be masked or ignored either.
ephemient
sigjuice: some people seem to call longjmp from a signal handler anyway :)
MarkR
A: 

Each process can recieve many types of signal, which it can ignore to handle but few aren't delivered to process but "Proceess scheduler" terminates the process.... see this for more explanation http://www.linux-tutorial.info/modules.php?name=MContent&pageid=289

Anurag Uniyal
+2  A: 

I presume you are talking about SIGKILL, so I will confine the discussion to that signal only.

When a process raises a SIGKILL on another process, SIGKILL is added as a pending signal on the victim process, and any pending SIGSTOP, SIGTSTP, SIGTTOU or SIGTTIN signals are cleared. The victim is woken up (made runnable) if it is stopped or in an interruptible sleep state.

When the victim process next attempts to go from Kernel mode to User mode, the pending signals are checked. This is where the pending SIGKILL is found, and the Kernel calls do_exit() instead of going back to User mode.

The transition from Kernel mode to User mode will be when the process is next scheduled (unless it was in an uninterruptible sleep - this is the infamous D state). If it's in an uninterrutible sleep, the process won't try to go back to User mode until its woken.

caf