ctty.htm 17 KB

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