3
0

ctty.htm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  2. <html><head>
  3. <!-- saved from http://www.win.tue.nl/~aeb/linux/lk/lk-10.html -->
  4. <meta name="GENERATOR" content="SGML-Tools 1.0.9"><title>The Linux kernel: Processes</title>
  5. </head>
  6. <body>
  7. <hr>
  8. <h2><a name="s10">10. Processes</a></h2>
  9. <p>Before looking at the Linux implementation, first a general Unix
  10. description of threads, processes, process groups and sessions.
  11. </p><p>
  12. (See also <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap11.html">General Terminal Interface</a>)
  13. </p><p>A session contains a number of process groups, and a process group
  14. contains a number of processes, and a process contains a number
  15. of threads.
  16. </p><p>A session can have a controlling tty.
  17. At most one process group in a session can be a foreground process group.
  18. An interrupt character typed on a tty ("Teletype", i.e., terminal)
  19. causes a signal to be sent to all members of the foreground process group
  20. in the session (if any) that has that tty as controlling tty.
  21. </p><p>All these objects have numbers, and we have thread IDs, process IDs,
  22. process group IDs and session IDs.
  23. </p><p>
  24. </p><h2><a name="ss10.1">10.1 Processes</a>
  25. </h2>
  26. <p>
  27. </p><h3>Creation</h3>
  28. <p>A new process is traditionally started using the <code>fork()</code>
  29. system call:
  30. </p><blockquote>
  31. <pre>pid_t p;
  32. p = fork();
  33. if (p == (pid_t) -1)
  34. /* ERROR */
  35. else if (p == 0)
  36. /* CHILD */
  37. else
  38. /* PARENT */
  39. </pre>
  40. </blockquote>
  41. <p>This creates a child as a duplicate of its parent.
  42. Parent and child are identical in almost all respects.
  43. In the code they are distinguished by the fact that the parent
  44. learns the process ID of its child, while <code>fork()</code>
  45. returns 0 in the child. (It can find the process ID of its
  46. parent using the <code>getppid()</code> system call.)
  47. </p><p>
  48. </p><h3>Termination</h3>
  49. <p>Normal termination is when the process does
  50. </p><blockquote>
  51. <pre>exit(n);
  52. </pre>
  53. </blockquote>
  54. or
  55. <blockquote>
  56. <pre>return n;
  57. </pre>
  58. </blockquote>
  59. from its <code>main()</code> procedure. It returns the single byte <code>n</code>
  60. to its parent.
  61. <p>Abnormal termination is usually caused by a signal.
  62. </p><p>
  63. </p><h3>Collecting the exit code. Zombies</h3>
  64. <p>The parent does
  65. </p><blockquote>
  66. <pre>pid_t p;
  67. int status;
  68. p = wait(&amp;status);
  69. </pre>
  70. </blockquote>
  71. and collects two bytes:
  72. <p>
  73. <figure>
  74. <eps file="absent">
  75. <img src="ctty_files/exit_status.png">
  76. </eps>
  77. </figure></p><p>A process that has terminated but has not yet been waited for
  78. is a <i>zombie</i>. It need only store these two bytes:
  79. exit code and reason for termination.
  80. </p><p>On the other hand, if the parent dies first, <code>init</code> (process 1)
  81. inherits the child and becomes its parent.
  82. </p><p>
  83. </p><h3>Signals</h3>
  84. <p>
  85. </p><h3>Stopping</h3>
  86. <p>Some signals cause a process to stop:
  87. <code>SIGSTOP</code> (stop!),
  88. <code>SIGTSTP</code> (stop from tty: probably ^Z was typed),
  89. <code>SIGTTIN</code> (tty input asked by background process),
  90. <code>SIGTTOU</code> (tty output sent by background process, and this was
  91. disallowed by <code>stty tostop</code>).
  92. </p><p>Apart from ^Z there also is ^Y. The former stops the process
  93. when it is typed, the latter stops it when it is read.
  94. </p><p>Signals generated by typing the corresponding character on some tty
  95. are sent to all processes that are in the foreground process group
  96. of the session that has that tty as controlling tty. (Details below.)
  97. </p><p>If a process is being traced, every signal will stop it.
  98. </p><p>
  99. </p><h3>Continuing</h3>
  100. <p><code>SIGCONT</code>: continue a stopped process.
  101. </p><p>
  102. </p><h3>Terminating</h3>
  103. <p><code>SIGKILL</code> (die! now!),
  104. <code>SIGTERM</code> (please, go away),
  105. <code>SIGHUP</code> (modem hangup),
  106. <code>SIGINT</code> (^C),
  107. <code>SIGQUIT</code> (^\), etc.
  108. Many signals have as default action to kill the target.
  109. (Sometimes with an additional core dump, when such is
  110. allowed by rlimit.)
  111. The signals <code>SIGCHLD</code> and <code>SIGWINCH</code>
  112. are ignored by default.
  113. All except <code>SIGKILL</code> and <code>SIGSTOP</code> can be
  114. caught or ignored or blocked.
  115. For details, see <code>signal(7)</code>.
  116. </p><p>
  117. </p><h2><a name="ss10.2">10.2 Process groups</a>
  118. </h2>
  119. <p>Every process is member of a unique <i>process group</i>,
  120. identified by its <i>process group ID</i>.
  121. (When the process is created, it becomes a member of the process group
  122. of its parent.)
  123. By convention, the process group ID of a process group
  124. equals the process ID of the first member of the process group,
  125. called the <i>process group leader</i>.
  126. A process finds the ID of its process group using the system call
  127. <code>getpgrp()</code>, or, equivalently, <code>getpgid(0)</code>.
  128. One finds the process group ID of process <code>p</code> using
  129. <code>getpgid(p)</code>.
  130. </p><p>One may use the command <code>ps j</code> to see PPID (parent process ID),
  131. PID (process ID), PGID (process group ID) and SID (session ID)
  132. of processes. With a shell that does not know about job control,
  133. like <code>ash</code>, each of its children will be in the same session
  134. and have the same process group as the shell. With a shell that knows
  135. about job control, like <code>bash</code>, the processes of one pipeline, like
  136. </p><blockquote>
  137. <pre>% cat paper | ideal | pic | tbl | eqn | ditroff &gt; out
  138. </pre>
  139. </blockquote>
  140. form a single process group.
  141. <p>
  142. </p><h3>Creation</h3>
  143. <p>A process <code>pid</code> is put into the process group <code>pgid</code> by
  144. </p><blockquote>
  145. <pre>setpgid(pid, pgid);
  146. </pre>
  147. </blockquote>
  148. If <code>pgid == pid</code> or <code>pgid == 0</code> then this creates
  149. a new process group with process group leader <code>pid</code>.
  150. Otherwise, this puts <code>pid</code> into the already existing
  151. process group <code>pgid</code>.
  152. A zero <code>pid</code> refers to the current process.
  153. The call <code>setpgrp()</code> is equivalent to <code>setpgid(0,0)</code>.
  154. <p>
  155. </p><h3>Restrictions on setpgid()</h3>
  156. <p>The calling process must be <code>pid</code> itself, or its parent,
  157. and the parent can only do this before <code>pid</code> has done
  158. <code>exec()</code>, and only when both belong to the same session.
  159. It is an error if process <code>pid</code> is a session leader
  160. (and this call would change its <code>pgid</code>).
  161. </p><p>
  162. </p><h3>Typical sequence</h3>
  163. <p>
  164. </p><blockquote>
  165. <pre>p = fork();
  166. if (p == (pid_t) -1) {
  167. /* ERROR */
  168. } else if (p == 0) { /* CHILD */
  169. setpgid(0, pgid);
  170. ...
  171. } else { /* PARENT */
  172. setpgid(p, pgid);
  173. ...
  174. }
  175. </pre>
  176. </blockquote>
  177. This ensures that regardless of whether parent or child is scheduled
  178. first, the process group setting is as expected by both.
  179. <p>
  180. </p><h3>Signalling and waiting</h3>
  181. <p>One can signal all members of a process group:
  182. </p><blockquote>
  183. <pre>killpg(pgrp, sig);
  184. </pre>
  185. </blockquote>
  186. <p>One can wait for children in ones own process group:
  187. </p><blockquote>
  188. <pre>waitpid(0, &amp;status, ...);
  189. </pre>
  190. </blockquote>
  191. or in a specified process group:
  192. <blockquote>
  193. <pre>waitpid(-pgrp, &amp;status, ...);
  194. </pre>
  195. </blockquote>
  196. <p>
  197. </p><h3>Foreground process group</h3>
  198. <p>Among the process groups in a session at most one can be
  199. the <i>foreground process group</i> of that session.
  200. The tty input and tty signals (signals generated by ^C, ^Z, etc.)
  201. go to processes in this foreground process group.
  202. </p><p>A process can determine the foreground process group in its session
  203. using <code>tcgetpgrp(fd)</code>, where <code>fd</code> refers to its
  204. controlling tty. If there is none, this returns a random value
  205. larger than 1 that is not a process group ID.
  206. </p><p>A process can set the foreground process group in its session
  207. using <code>tcsetpgrp(fd,pgrp)</code>, where <code>fd</code> refers to its
  208. controlling tty, and <code>pgrp</code> is a process group in
  209. its session, and this session still is associated to the controlling
  210. tty of the calling process.
  211. </p><p>How does one get <code>fd</code>? By definition, <code>/dev/tty</code>
  212. refers to the controlling tty, entirely independent of redirects
  213. of standard input and output. (There is also the function
  214. <code>ctermid()</code> to get the name of the controlling terminal.
  215. On a POSIX standard system it will return <code>/dev/tty</code>.)
  216. Opening the name of the
  217. controlling tty gives a file descriptor <code>fd</code>.
  218. </p><p>
  219. </p><h3>Background process groups</h3>
  220. <p>All process groups in a session that are not foreground
  221. process group are <i>background process groups</i>.
  222. Since the user at the keyboard is interacting with foreground
  223. processes, background processes should stay away from it.
  224. When a background process reads from the terminal it gets
  225. a SIGTTIN signal. Normally, that will stop it, the job control shell
  226. notices and tells the user, who can say <code>fg</code> to continue
  227. this background process as a foreground process, and then this
  228. process can read from the terminal. But if the background process
  229. ignores or blocks the SIGTTIN signal, or if its process group
  230. is orphaned (see below), then the read() returns an EIO error,
  231. and no signal is sent. (Indeed, the idea is to tell the process
  232. that reading from the terminal is not allowed right now.
  233. If it wouldn't see the signal, then it will see the error return.)
  234. </p><p>When a background process writes to the terminal, it may get
  235. a SIGTTOU signal. May: namely, when the flag that this must happen
  236. is set (it is off by default). One can set the flag by
  237. </p><blockquote>
  238. <pre>% stty tostop
  239. </pre>
  240. </blockquote>
  241. and clear it again by
  242. <blockquote>
  243. <pre>% stty -tostop
  244. </pre>
  245. </blockquote>
  246. and inspect it by
  247. <blockquote>
  248. <pre>% stty -a
  249. </pre>
  250. </blockquote>
  251. Again, if TOSTOP is set but the background process ignores or blocks
  252. the SIGTTOU signal, or if its process group is orphaned (see below),
  253. then the write() returns an EIO error, and no signal is sent.
  254. [vda: correction. SUS says that if SIGTTOU is blocked/ignored, write succeeds. ]
  255. <p>
  256. </p><h3>Orphaned process groups</h3>
  257. <p>The process group leader is the first member of the process group.
  258. It may terminate before the others, and then the process group is
  259. without leader.
  260. </p><p>A process group is called <i>orphaned</i> when <i>the
  261. parent of every member is either in the process group
  262. or outside the session</i>.
  263. In particular, the process group of the session leader
  264. is always orphaned.
  265. </p><p>If termination of a process causes a process group to become
  266. orphaned, and some member is stopped, then all are sent first SIGHUP
  267. and then SIGCONT.
  268. </p><p>The idea is that perhaps the parent of the process group leader
  269. is a job control shell. (In the same session but a different
  270. process group.) As long as this parent is alive, it can
  271. handle the stopping and starting of members in the process group.
  272. When it dies, there may be nobody to continue stopped processes.
  273. Therefore, these stopped processes are sent SIGHUP, so that they
  274. die unless they catch or ignore it, and then SIGCONT to continue them.
  275. </p><p>Note that the process group of the session leader is already
  276. orphaned, so no signals are sent when the session leader dies.
  277. </p><p>Note also that a process group can become orphaned in two ways
  278. by termination of a process: either it was a parent and not itself
  279. in the process group, or it was the last element of the process group
  280. with a parent outside but in the same session.
  281. Furthermore, that a process group can become orphaned
  282. other than by termination of a process, namely when some
  283. member is moved to a different process group.
  284. </p><p>
  285. </p><h2><a name="ss10.3">10.3 Sessions</a>
  286. </h2>
  287. <p>Every process group is in a unique <i>session</i>.
  288. (When the process is created, it becomes a member of the session
  289. of its parent.)
  290. By convention, the session ID of a session
  291. equals the process ID of the first member of the session,
  292. called the <i>session leader</i>.
  293. A process finds the ID of its session using the system call
  294. <code>getsid()</code>.
  295. </p><p>Every session may have a <i>controlling tty</i>,
  296. that then also is called the controlling tty of each of
  297. its member processes.
  298. A file descriptor for the controlling tty is obtained by
  299. opening <code>/dev/tty</code>. (And when that fails, there was no
  300. controlling tty.) Given a file descriptor for the controlling tty,
  301. one may obtain the SID using <code>tcgetsid(fd)</code>.
  302. </p><p>A session is often set up by a login process. The terminal
  303. on which one is logged in then becomes the controlling tty
  304. of the session. All processes that are descendants of the
  305. login process will in general be members of the session.
  306. </p><p>
  307. </p><h3>Creation</h3>
  308. <p>A new session is created by
  309. </p><blockquote>
  310. <pre>pid = setsid();
  311. </pre>
  312. </blockquote>
  313. This is allowed only when the current process is not a process group leader.
  314. In order to be sure of that we fork first:
  315. <blockquote>
  316. <pre>p = fork();
  317. if (p) exit(0);
  318. pid = setsid();
  319. </pre>
  320. </blockquote>
  321. The result is that the current process (with process ID <code>pid</code>)
  322. becomes session leader of a new session with session ID <code>pid</code>.
  323. Moreover, it becomes process group leader of a new process group.
  324. Both session and process group contain only the single process <code>pid</code>.
  325. Furthermore, this process has no controlling tty.
  326. <p>The restriction that the current process must not be a process group leader
  327. is needed: otherwise its PID serves as PGID of some existing process group
  328. and cannot be used as the PGID of a new process group.
  329. </p><p>
  330. </p><h3>Getting a controlling tty</h3>
  331. <p>How does one get a controlling terminal? Nobody knows,
  332. this is a great mystery.
  333. </p><p>The System V approach is that the first tty opened by the process
  334. becomes its controlling tty.
  335. </p><p>The BSD approach is that one has to explicitly call
  336. </p><blockquote>
  337. <pre>ioctl(fd, TIOCSCTTY, 0/1);
  338. </pre>
  339. </blockquote>
  340. to get a controlling tty.
  341. <p>Linux tries to be compatible with both, as always, and this
  342. results in a very obscure complex of conditions. Roughly:
  343. </p><p>The <code>TIOCSCTTY</code> ioctl will give us a controlling tty,
  344. provided that (i) the current process is a session leader,
  345. and (ii) it does not yet have a controlling tty, and
  346. (iii) maybe the tty should not already control some other session;
  347. if it does it is an error if we aren't root, or we steal the tty
  348. if we are all-powerful.
  349. [vda: correction: third parameter controls this: if 1, we steal tty from
  350. any such session, if 0, we don't steal]
  351. </p><p>Opening some terminal will give us a controlling tty,
  352. provided that (i) the current process is a session leader, and
  353. (ii) it does not yet have a controlling tty, and
  354. (iii) the tty does not already control some other session, and
  355. (iv) the open did not have the <code>O_NOCTTY</code> flag, and
  356. (v) the tty is not the foreground VT, and
  357. (vi) the tty is not the console, and
  358. (vii) maybe the tty should not be master or slave pty.
  359. </p><p>
  360. </p><h3>Getting rid of a controlling tty</h3>
  361. <p>If a process wants to continue as a daemon, it must detach itself
  362. from its controlling tty. Above we saw that <code>setsid()</code>
  363. will remove the controlling tty. Also the ioctl TIOCNOTTY does this.
  364. Moreover, in order not to get a controlling tty again as soon as it
  365. opens a tty, the process has to fork once more, to assure that it
  366. is not a session leader. Typical code fragment:
  367. </p><p>
  368. </p><pre> if ((fork()) != 0)
  369. exit(0);
  370. setsid();
  371. if ((fork()) != 0)
  372. exit(0);
  373. </pre>
  374. <p>See also <code>daemon(3)</code>.
  375. </p><p>
  376. </p><h3>Disconnect</h3>
  377. <p>If the terminal goes away by modem hangup, and the line was not local,
  378. then a SIGHUP is sent to the session leader.
  379. Any further reads from the gone terminal return EOF.
  380. (Or possibly -1 with <code>errno</code> set to EIO.)
  381. </p><p>If the terminal is the slave side of a pseudotty, and the master side
  382. is closed (for the last time), then a SIGHUP is sent to the foreground
  383. process group of the slave side.
  384. </p><p>When the session leader dies, a SIGHUP is sent to all processes
  385. in the foreground process group. Moreover, the terminal stops being
  386. the controlling terminal of this session (so that it can become
  387. the controlling terminal of another session).
  388. </p><p>Thus, if the terminal goes away and the session leader is
  389. a job control shell, then it can handle things for its descendants,
  390. e.g. by sending them again a SIGHUP.
  391. If on the other hand the session leader is an innocent process
  392. that does not catch SIGHUP, it will die, and all foreground processes
  393. get a SIGHUP.
  394. </p><p>
  395. </p><h2><a name="ss10.4">10.4 Threads</a>
  396. </h2>
  397. <p>A process can have several threads. New threads (with the same PID
  398. as the parent thread) are started using the <code>clone</code> system
  399. call using the <code>CLONE_THREAD</code> flag. Threads are distinguished
  400. by a <i>thread ID</i> (TID). An ordinary process has a single thread
  401. with TID equal to PID. The system call <code>gettid()</code> returns the
  402. TID. The system call <code>tkill()</code> sends a signal to a single thread.
  403. </p><p>Example: a process with two threads. Both only print PID and TID and exit.
  404. (Linux 2.4.19 or later.)
  405. </p><pre>% cat &lt;&lt; EOF &gt; gettid-demo.c
  406. #include &lt;unistd.h&gt;
  407. #include &lt;sys/types.h&gt;
  408. #define CLONE_SIGHAND 0x00000800
  409. #define CLONE_THREAD 0x00010000
  410. #include &lt;linux/unistd.h&gt;
  411. #include &lt;errno.h&gt;
  412. _syscall0(pid_t,gettid)
  413. int thread(void *p) {
  414. printf("thread: %d %d\n", gettid(), getpid());
  415. }
  416. main() {
  417. unsigned char stack[4096];
  418. int i;
  419. i = clone(thread, stack+2048, CLONE_THREAD | CLONE_SIGHAND, NULL);
  420. if (i == -1)
  421. perror("clone");
  422. else
  423. printf("clone returns %d\n", i);
  424. printf("parent: %d %d\n", gettid(), getpid());
  425. }
  426. EOF
  427. % cc -o gettid-demo gettid-demo.c
  428. % ./gettid-demo
  429. clone returns 21826
  430. parent: 21825 21825
  431. thread: 21826 21825
  432. %
  433. </pre>
  434. <p>
  435. </p><p>
  436. </p><hr>
  437. </body></html>