Difference between Evaluation ► Quit Kernel and Quit[]
You cannot Quit
kernel while evaluation is still running: the Quit[]
command will be placed in the queue and executed only after finishing of evaluation of all the previous inputs. In contrast, Evaluation ► Quit Kernel
will quit the kernel immediately even if it is still running.
UPDATE
As Kuba noticed in the comments, via "Preemptive"
link it is possible to inject the Quit[]
command into running kernel, for example via Button
:
Button["test", Quit[]]
But even in this case Quit[]
will be evaluated only when the running kernel reaches some point in its computation where such evaluation is possible: for example, if the running code is wrapped with PreemptProtect
, the Quit[]
command will be queued and will not be evaluated before evaluation of the PreemptProtect
ed code block finishes.
In contrast, Evaluation ► Quit Kernel
always quits the kernel immediately.
The difference is the following:
When you call Quit[]
as input in a notebook, then the front end sends this command over the main MathLink connection to the Kernel like any other input. If you are already evaluating a long-running (or hanging) command, the Kernel won't quit because (as Alexey already pointed out) the quit command is queued and waits until the current evaluation has finished.
The front end menu Quit works completely differently: It uses inter-process signals to tell the kernel that it needs to quit. Btw, the same approach is used to interrupt an evaluation. Let me give an example and show you, that you can send those signals yourself. From here, I'm using LinkSnooper to show you the communication between Kernel and Front End. I have started my LinkSnooper-Kernel and I'm evaluating While[True]
which runs and runs and runs... The input is sent to the Kernel as EnterExpressionPacket
:
EnterExpressionPacket[MakeExpression[BoxData[RowBox[{"While", "[", "True","]"}]], StandardForm]]
If you now click the menu entry Evaluation -> Interrupt Evaluation you see that the LinkSnooper shows that the Front End has send the signal 2 to the Kernel
****** Message FE ---> K: on Main: 2
Signal 2 is
SIGINT: The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Control-C, but on some systems, the "delete" character or "break" key can be used.
The Kernel interrupts his evaluation and sends the following packet back to the Front End MenuPacket[1, "Interrupt> "]
which make the Front End to show you this nice dialog
If you press Abort
then the Front End does nothing more than sending a plain "a" to the Kernel. You can see the exact same communication when you work in a terminal Mathematica session. Let me show you
~$ math
Mathematica 10.0 for Linux x86 (64-bit)
Copyright 1988-2014 Wolfram Research, Inc.
In[1]:= While[True]
^C
Interrupt> a
Out[1]= $Aborted
In[2]:=
Note that the ^C
is the Ctrl+C I was pressing which is described in the SIGINT doc above.
When you make a menu-quit, you see through the same inspection that the Front End sends the signal 1 to the Kernel which is
SIGABRT: The SIGABRT signal is sent to a process to tell it to abort, i.e. to terminate. The signal is usually initiated by the process itself when it calls abort function of the C Standard Library, but it can be sent to the process from outside as well as any other signal.
The really helpful thing is, that you can always send those signals to the kernel process, even if the Front End hangs completely. Unfortunately, I don't whether this works in Windows the same way it does in OSX or Linux, but I'm sure someone can find out.
Anyway, let's try this in Mathematica V10 (which doesn't has the interrupt button anymore). First, I'm extracting the process id that the Kernel has with $ProcessID
(it's 7005 currently here). Now I go
While[True]
and then I type in a terminal (a terminal is the thing you get under Windows when you go to Start and type cmd
in the command-line)
kill -2 7005
and bang, the above Evaluation Control dialog pops up. Clicking on Continue and using kill -1 7005
and the Kernel quits.
Summary
While Quit[]
is a normal kernel function which makes that the kernel exits by himself, the menu uses signals that are send to the kernel process.