comparison lispref/processes.texi @ 44345:d2f43521efb4

New node Query Before Exit broken out of Deleting Processes. Explain more details of process deletion and sentinels. Document process-query-on-exit-flag and set-process-query-on-exit-flag.
author Richard M. Stallman <rms@gnu.org>
date Tue, 02 Apr 2002 21:20:16 +0000
parents 4520697c8aa5
children 7e1a0877a121
comparison
equal deleted inserted replaced
44344:37801f2191c3 44345:d2f43521efb4
41 * Deleting Processes:: Eliminating an asynchronous subprocess. 41 * Deleting Processes:: Eliminating an asynchronous subprocess.
42 * Process Information:: Accessing run-status and other attributes. 42 * Process Information:: Accessing run-status and other attributes.
43 * Input to Processes:: Sending input to an asynchronous subprocess. 43 * Input to Processes:: Sending input to an asynchronous subprocess.
44 * Signals to Processes:: Stopping, continuing or interrupting 44 * Signals to Processes:: Stopping, continuing or interrupting
45 an asynchronous subprocess. 45 an asynchronous subprocess.
46 * Query Before Exit:: Whether to query if exiting will kill a process.
46 * Output from Processes:: Collecting output from an asynchronous subprocess. 47 * Output from Processes:: Collecting output from an asynchronous subprocess.
47 * Sentinels:: Sentinels run when process run-status changes. 48 * Sentinels:: Sentinels run when process run-status changes.
48 * Transaction Queues:: Transaction-based communication with subprocesses. 49 * Transaction Queues:: Transaction-based communication with subprocesses.
49 * Network:: Opening network connections. 50 * Network:: Opening network connections.
50 @end menu 51 @end menu
479 @node Deleting Processes 480 @node Deleting Processes
480 @section Deleting Processes 481 @section Deleting Processes
481 @cindex deleting processes 482 @cindex deleting processes
482 483
483 @dfn{Deleting a process} disconnects Emacs immediately from the 484 @dfn{Deleting a process} disconnects Emacs immediately from the
484 subprocess, and removes it from the list of active processes. It sends 485 subprocess. Processes are deleted automatically after they terminate,
485 a signal to the subprocess to make the subprocess terminate, but this is 486 but not necessarily right away. You can delete a process explicitly
486 not guaranteed to happen immediately. The process object itself 487 at any time. If you delete a terminated process explicitly before it
487 continues to exist as long as other Lisp objects point to it. The 488 is deleted automatically, no harm results. Deletion of a running
488 process mark continues to point to the same place as before (usually 489 process sends a signal to terminate it and calls the process sentinel
489 into a buffer where output from the process was being inserted). 490 if it has one.
490 491
491 You can delete a process explicitly at any time. Processes are 492 @code{get-buffer-process} and @code{process-list} do not remember a
492 deleted automatically after they terminate, but not necessarily right 493 deleted process, but the process object itself continues to exist as
493 away. If you delete a terminated process explicitly before it is 494 long as other Lisp objects point to it. All the Lisp primitives that
494 deleted automatically, no harm results. 495 work on process objects accept deleted processes, but those that do
496 I/O or send signals will report an error. The process mark continues
497 to point to the same place as before, usually into a buffer where
498 output from the process was being inserted.
495 499
496 @defopt delete-exited-processes 500 @defopt delete-exited-processes
497 This variable controls automatic deletion of processes that have 501 This variable controls automatic deletion of processes that have
498 terminated (due to calling @code{exit} or to a signal). If it is 502 terminated (due to calling @code{exit} or to a signal). If it is
499 @code{nil}, then they continue to exist until the user runs 503 @code{nil}, then they continue to exist until the user runs
500 @code{list-processes}. Otherwise, they are deleted immediately after 504 @code{list-processes}. Otherwise, they are deleted immediately after
501 they exit. 505 they exit.
502 @end defopt 506 @end defopt
503 507
504 @defun delete-process name 508 @defun delete-process name
505 This function deletes the process associated with @var{name}, killing it 509 This function deletes the process associated with @var{name}, killing
506 with a @code{SIGHUP} signal. The argument @var{name} may be a process, 510 it with a @code{SIGKILL} signal. The argument @var{name} may be a
507 the name of a process, a buffer, or the name of a buffer. 511 process, the name of a process, a buffer, or the name of a buffer.
512 Calling @code{delete-process} on a running process terminates it,
513 updates the process status, and runs the sentinel (if any) immediately.
514 If the process has already terminated, calling @code{delete-process}
515 has no effect on its status, or on the running of its sentinel (which
516 will happen sooner or later).
508 517
509 @smallexample 518 @smallexample
510 @group 519 @group
511 (delete-process "*shell*") 520 (delete-process "*shell*")
512 @result{} nil 521 @result{} nil
513 @end group
514 @end smallexample
515 @end defun
516
517 @defun process-kill-without-query process &optional do-query
518 This function specifies whether Emacs should query the user if
519 @var{process} is still running when Emacs is exited. If @var{do-query}
520 is @code{nil}, the process will be deleted silently.
521 Otherwise, Emacs will query about killing it.
522
523 The value is @code{t} if the process was formerly set up to require
524 query, @code{nil} otherwise. A newly-created process always requires
525 query.
526
527 @smallexample
528 @group
529 (process-kill-without-query (get-process "shell"))
530 @result{} t
531 @end group 522 @end group
532 @end smallexample 523 @end smallexample
533 @end defun 524 @end defun
534 525
535 @node Process Information 526 @node Process Information
862 This function sends a signal to process @var{pid}, which need not be 853 This function sends a signal to process @var{pid}, which need not be
863 a child of Emacs. The argument @var{signal} specifies which signal 854 a child of Emacs. The argument @var{signal} specifies which signal
864 to send; it should be an integer. 855 to send; it should be an integer.
865 @end defun 856 @end defun
866 857
858 @node Query Before Exit
859 @section Querying Before Exit
860
861 When Emacs exits, it terminates all its subprocesses by sending them
862 the @code{SIGHUP} signal. Because some subprocesses are doing
863 valuable work, Emacs normally asks the user to confirm that it is ok
864 to terminate them. Each process has a query flag which, if
865 non-@code{nil}, says that Emacs should ask for confirmation before
866 exiting and thus killing that process. The default for the query flag
867 is @code{t}, meaning @emph{do} query.
868
869 @tindex process-query-on-exit-flag
870 @defun process-query-on-exit-flag process
871 This returns the query flag of @var{process}.
872 @end defun
873
874 @tindex set-process-query-on-exit-flag
875 @defun set-process-query-on-exit-flag process flag
876 This function sets the query flag of @var{process} to @var{flag}. It
877 returns @var{flag}.
878
879 @smallexample
880 @group
881 ;; @r{Don't query about the shell process}
882 (set-process-query-on-exit-flag (get-process "shell") nil)
883 @result{} t
884 @end group
885 @end smallexample
886 @end defun
887
888 @defun process-kill-without-query process &optional do-query
889 This function clears the query flag of @var{process}, so that
890 Emacs will not query the user on account of that process.
891
892 Actually, the function does more than that: it returns the old value of
893 the process's query flag, and sets the query flag to @var{do-query}.
894 Please don't use this function to do those things any more---please
895 use the newer, cleaner functions @code{process-query-on-exit-flag} and
896 @code{set-process-query-on-exit-flag} in all but the simplest cases.
897 The only way you should use @code{process-kill-without-query} nowadays
898 is like this:
899
900 @smallexample
901 @group
902 ;; @r{Don't query about the shell process}
903 (process-kill-without-query (get-process "shell"))
904 @end group
905 @end smallexample
906 @end defun
907
867 @node Output from Processes 908 @node Output from Processes
868 @section Receiving Output from Processes 909 @section Receiving Output from Processes
869 @cindex process output 910 @cindex process output
870 @cindex output from processes 911 @cindex output from processes
871 912
972 @var{buffer}. If @var{buffer} is @code{nil}, the process becomes 1013 @var{buffer}. If @var{buffer} is @code{nil}, the process becomes
973 associated with no buffer. 1014 associated with no buffer.
974 @end defun 1015 @end defun
975 1016
976 @defun get-buffer-process buffer-or-name 1017 @defun get-buffer-process buffer-or-name
977 This function returns the process associated with @var{buffer-or-name}. 1018 This function returns a nondeleted process associated with the buffer
978 If there are several processes associated with it, then one is chosen. 1019 specified by @var{buffer-or-name}. If there are several processes
979 (Currently, the one chosen is the one most recently created.) It is 1020 associated with it, this function chooses one (currently, the one most
980 usually a bad idea to have more than one process associated with the 1021 recently created, but don't count on that). Deletion of a process
981 same buffer. 1022 (see @code{delete-process}) makes it ineligible for this function to
1023 return.
1024
1025 It is usually a bad idea to have more than one process associated with
1026 the same buffer.
982 1027
983 @smallexample 1028 @smallexample
984 @group 1029 @group
985 (get-buffer-process "*shell*") 1030 (get-buffer-process "*shell*")
986 @result{} #<process shell> 1031 @result{} #<process shell>
1195 @cindex sentinel 1240 @cindex sentinel
1196 1241
1197 A @dfn{process sentinel} is a function that is called whenever the 1242 A @dfn{process sentinel} is a function that is called whenever the
1198 associated process changes status for any reason, including signals 1243 associated process changes status for any reason, including signals
1199 (whether sent by Emacs or caused by the process's own actions) that 1244 (whether sent by Emacs or caused by the process's own actions) that
1200 terminate, stop, or continue the process. The process sentinel is also 1245 terminate, stop, or continue the process. The process sentinel is
1201 called if the process exits. The sentinel receives two arguments: the 1246 also called if the process exits. The sentinel receives two
1202 process for which the event occurred, and a string describing the type 1247 arguments: the process for which the event occurred, and a string
1203 of event. 1248 describing the type of event.
1204 1249
1205 The string describing the event looks like one of the following: 1250 The string describing the event looks like one of the following:
1206 1251
1207 @itemize @bullet 1252 @itemize @bullet
1208 @item 1253 @item
1216 1261
1217 @item 1262 @item
1218 @code{"@var{name-of-signal} (core dumped)\n"}. 1263 @code{"@var{name-of-signal} (core dumped)\n"}.
1219 @end itemize 1264 @end itemize
1220 1265
1221 A sentinel runs only while Emacs is waiting (e.g., for terminal input, 1266 A sentinel runs only while Emacs is waiting (e.g., for terminal
1222 or for time to elapse, or for process output). This avoids the timing 1267 input, or for time to elapse, or for process output). This avoids the
1223 errors that could result from running them at random places in the 1268 timing errors that could result from running them at random places in
1224 middle of other Lisp programs. A program can wait, so that sentinels 1269 the middle of other Lisp programs. A program can wait, so that
1225 will run, by calling @code{sit-for} or @code{sleep-for} 1270 sentinels will run, by calling @code{sit-for} or @code{sleep-for}
1226 (@pxref{Waiting}), or @code{accept-process-output} (@pxref{Accepting 1271 (@pxref{Waiting}), or @code{accept-process-output} (@pxref{Accepting
1227 Output}). Emacs also allows sentinels to run when the command loop is 1272 Output}). Emacs also allows sentinels to run when the command loop is
1228 reading input. 1273 reading input. @code{delete-process} calls the sentinel when it
1274 terminates a running process.
1275
1276 Emacs does not keep a queue of multiple reasons to call the sentinel
1277 of one process; it records just the current status and the fact that
1278 there has been a change. Therefore two changes in status, coming in
1279 quick succession, can call the sentinel just once. However, process
1280 termination will always run the sentinel exactly once. This is
1281 because the process status can't change again after termination.
1229 1282
1230 Quitting is normally inhibited within a sentinel---otherwise, the 1283 Quitting is normally inhibited within a sentinel---otherwise, the
1231 effect of typing @kbd{C-g} at command level or to quit a user command 1284 effect of typing @kbd{C-g} at command level or to quit a user command
1232 would be unpredictable. If you want to permit quitting inside a 1285 would be unpredictable. If you want to permit quitting inside a
1233 sentinel, bind @code{inhibit-quit} to @code{nil}. @xref{Quitting}. 1286 sentinel, bind @code{inhibit-quit} to @code{nil}. @xref{Quitting}.
1252 @defun set-process-sentinel process sentinel 1305 @defun set-process-sentinel process sentinel
1253 This function associates @var{sentinel} with @var{process}. If 1306 This function associates @var{sentinel} with @var{process}. If
1254 @var{sentinel} is @code{nil}, then the process will have no sentinel. 1307 @var{sentinel} is @code{nil}, then the process will have no sentinel.
1255 The default behavior when there is no sentinel is to insert a message in 1308 The default behavior when there is no sentinel is to insert a message in
1256 the process's buffer when the process status changes. 1309 the process's buffer when the process status changes.
1310
1311 Changes in process sentinel take effect immediately---if the sentinel
1312 is slated to be run but has not been called yet, and you specify a new
1313 sentinel, the eventual call to the sentinel will use the new one.
1257 1314
1258 @smallexample 1315 @smallexample
1259 @group 1316 @group
1260 (defun msg-me (process event) 1317 (defun msg-me (process event)
1261 (princ 1318 (princ