home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / mod.std.unix.v7 / text0053.txt < prev    next >
Encoding:
Internet Message Format  |  1987-06-30  |  54.1 KB

  1. From: hplabs!hpda!hpisoa1!davel (Dave Lennert)
  2. Date: Thu, 09 Oct 86 22:33:31 -0800
  3.  
  4. As an addendum to the P1003 job control proposal text I submitted,
  5. here's a paper describing the 4.2 job control implementation, the
  6. System V incompatibilities, and how they were resolved.  This was
  7. the background investigation that went into the P1003 proposal.
  8.  
  9. This is the same paper that appears in the Atlanta USENIX proceedings.
  10. [ June 1986, pp. 459-474.  -mod ]
  11. It uses the -ms macro package.
  12.  
  13.     Dave Lennert                ucbvax!hpda!davel               [UUCP]
  14.     Hewlett-Packard - 47UX      ihnp4!hplabs!hpda!davel         [UUCP]
  15.     19447 Pruneridge Ave.       hpda!davel@ucb-vax.ARPA         [ARPA]
  16.     Cupertino, CA  95014        (408) 447-6325                  [AT&T]
  17.  
  18.  
  19. # This is a shell archive.  Remove anything before this line,
  20. # then unpack it by saving it in a file and typing "sh file".
  21. #
  22. # Wrapped by davel on Thu Oct  9 22:29:34 PDT 1986
  23. # Contents:  jobpaper.ms
  24.  
  25. echo x - jobpaper.ms
  26. sed 's/^@//' > "jobpaper.ms" <<'@//E*O*F jobpaper.ms//'
  27. @.de PB
  28. @.sp 1
  29. @.RS
  30. @.nf
  31. @..
  32. @.de PE
  33. @.fi
  34. @.RE
  35. @.sp 1
  36. @..
  37. @.DA
  38. @.    \" Put page numbers at bottom center rather than top center
  39. @.rm CH
  40. @.ds CF - \\n(PN -
  41. @.    \" UX - UNIX macro
  42. @.de UX
  43. @.ie \\n(UX \s-1UNIX\s0\\$1
  44. @.el \{\
  45. \s-1UNIX\s0\\$1\(dg
  46. @.FS
  47. \(dg \s-1UNIX\s0 is a trademark of AT&T.
  48. @.FE
  49. @.nr UX 1
  50. @.\}
  51. @..
  52. @.TL
  53. A System V Compatible Implementation of 4.2BSD Job Control
  54. @.AU
  55. David C. Lennert
  56. @.AI
  57. Hewlett-Packard Company
  58. Information Technology Group
  59. hplabs\^!\^hpda\^!\^davel
  60. @.AB
  61. This paper gives an overview of how process groups and controlling
  62. terminals are handled in System V and 4.2BSD and then
  63. discusses the effect 4.2BSD job control has on these things.
  64. A modified 4.2BSD interface is discussed which supports 4.2BSD job control
  65. functionality but in a way which allows AT&T System V compatibility.
  66. This interface has been implemented in Hewlett-Packard's 
  67. @.UX
  68. system, HP-UX.
  69. @.AE
  70. @.sp 1
  71. @.NH
  72. INTRODUCTION
  73. @.PP
  74. The job control functionality first introduced into
  75. @.UX
  76. by Jim Kulp of IIASA and later provided by 4.2BSD
  77. @.UX
  78. has become a \fIde facto\fP industry standard.
  79. However, this job control facility, as implemented in 4.2BSD, is incompatible
  80. in several respects with System V.
  81. @.PP
  82. Recently a proposal was submitted to the IEEE P1003 Portable Operating
  83. System standard committee by Sun Microsystems [Harris86] which attempts
  84. to define
  85. 4.2BSD job control functionality in a way compatible with System V.
  86. Hewlett-Packard Company has been independently developing a similar
  87. proposal.  HP's proposal is almost identical to Sun's but goes
  88. beyond it to address many "corner case" areas which strongly affect
  89. System V compatibility.
  90. @.PP
  91. This paper gives an overview of the relevant areas of System V 
  92. functionality which are affected.  It then overviews how job control
  93. is implemented in 4.2BSD and how this impacts the System V interface.
  94. Finally, the HP-UX interface is presented and a similar overview of its
  95. implementation is given.
  96. @.PP
  97. The various overviews cover how job control signals are generated, passed,
  98. and acknowledged by the tty driver and user processes.  They also explain
  99. how process groups are established and changed.
  100. @.sp 1
  101. @.NH
  102. FUNDAMENTALS
  103. @.PP
  104. In the following discussion the reader is assumed to have an understanding
  105. of several fundamental concepts found in the
  106. @.UX
  107. operating system.  For convenience these concepts are briefly reviewed here.
  108. @.NH 2
  109. Process Groups and Controlling Terminals
  110. @.PP
  111. Every process has a unique numeric value associated with it called
  112. its \fIprocess ID\fP.  Every process also has a non-unique numeric value
  113. associated with it called its \fIprocess group ID\fP.
  114. A \fIprocess group\fP is a collection of processes having identical numeric
  115. process group ID's.  Typically, one process in the process group will
  116. be the \fIprocess group leader\fP.  The process group leader has a process ID
  117. which is numerically equal to the process group ID associated with all processes
  118. in the process group.  Typically, the process group leader is the ancestor
  119. of all other processes in the process group.
  120. @.PP
  121. A process can have a \fIcontrolling terminal\fP which is usually the login
  122. terminal of the user who created the process.  A process can obtain access
  123. to its controlling terminal by opening the file \fI/dev/tty\fP.
  124. All processes in the same process group typically share the same controlling
  125. terminal.  A terminal usually has a process group ID associated with it,
  126. called the \fItty group ID\fP.  When a user generates a keyboard signal
  127. (e.g., by typing the interrupt character), the tty driver sends the appropriate
  128. signal to all processes which are members of the process group indicated by
  129. the tty group ID.
  130. In summary,
  131. usually, but not necessarily, all processes in the same process group share
  132. the same controlling terminal, and the tty group ID for that terminal is
  133. equal to the process group ID of the process group.
  134. @.PP
  135. For further explanation see [Roch85] and intro(2) in your favorite
  136. @.UX
  137. Programmer's Manual.
  138. @.NH 2
  139. 4.2BSD Job Control
  140. @.PP
  141. 4.2BSD job control allows users to selectively stop (suspend) the
  142. execution of processes and continue (resume) their execution at any
  143. later point.  This only easily works for processes which are stopped
  144. and continued during the same login session.
  145. @.PP
  146. The user almost always employs this facility via the interactive interface
  147. jointly supplied by the system tty driver and a job control shell such as
  148. csh(1) or ksh(1).
  149. The tty driver recognizes a user-defined
  150. \fIsuspend character\fP which causes all current foreground processes to
  151. stop and the user's job control shell to resume.
  152. The job control shell provides commands which continue stopped processes
  153. in either the foreground or background.
  154. The tty driver will also stop a background process when it attempts to
  155. read from or write to the users terminal.  This allows the user to finish
  156. or suspend their foreground task without interruption and continue the
  157. stopped background process at a more convenient time.
  158. @.PP
  159. To enable the system to support this,
  160. 4.2BSD job control introduces five new signals: SIGSTOP, SIGTSTP, SIGTTIN,
  161. SIGTTOU, and SIGCONT.  The first four signals cause a process to stop unless
  162. the signals are being caught or ignored.  SIGCONT always causes a stopped
  163. process to continue.  (SIGCONT has no effect on processes which are not
  164. stopped.)  SIGSTOP cannot be caught or ignored.
  165. @.PP
  166. The tty driver sends some of these signals to all processes in the tty
  167. process group under the following conditions:  The driver sends SIGTSTP when
  168. the user types the suspend or delayed suspend character.  The driver sends
  169. SIGTTIN (SIGTTOU) when a background process attempts to read from (write to)
  170. its controlling terminal.
  171. SIGCONT is usually only sent by a job control shell when
  172. the user requests that a stopped process be continued.
  173. Of course, any signal can be sent by a user via the kill(1) command or
  174. by a program via the kill(2) system call.
  175. @.PP
  176. It should be noted that these signals can be added to a
  177. @.UX
  178. implementation in a manner which preserves source and object code
  179. compatibility.  A process is not required to be aware of them.  By
  180. default the signals do "the right thing."
  181. @.PP
  182. For further information see [Joy80] and [UCB83].
  183. @.sp 1
  184. @.NH
  185. AT&T SYSTEM V
  186. @.NH 2
  187. Introduction
  188. @.PP
  189. System V process groups closely resemble the concept of a login session.
  190. That is, all processes spawned during the same login session tend to
  191. belong to the same process group, and keyboard signals are typically
  192. sent to all processes spawned from the login session.
  193. @.NH 2
  194. System V Process Group Handling
  195. @.PP
  196. In System V, the only way to alter the process group associated
  197. with a process (p_pgrp) is via setpgrp(2).  And this can only set the process
  198. group to equal the process ID (pid) of the process.  When this happens
  199. the resulting process with pid = p_pgrp is called a process group leader.
  200. Since a process's pid can never change,
  201. once a process issues a setpgrp(2) call it irrevocably becomes
  202. a process group leader.
  203. @.PP
  204. The init(1M) process spawns all other processes on the system either
  205. directly or indirectly.  Before directly spawning a process
  206. (after the fork(2) but before the exec(2)),
  207. init calls setpgrp(2).
  208. Thus all original children (not orphans) of init are forced to (irrevocably)
  209. be process group leaders.
  210. @.PP
  211. When a new process is created, it is assigned a new pid but it inherits
  212. the process group number of its parent.  Thus child processes are, by
  213. default, not process group leaders (although they can become a process
  214. group leader via setpgrp(2)).
  215. @.PP
  216. When a process group leader which has a controlling terminal (see below)
  217. terminates, SIGHUP is sent to all processes in the same
  218. process group.
  219. @.PP
  220. Further, when a process group leader terminates, all processes that
  221. belong to this process group are altered to belong to no process group
  222. (their p_pgrp is set to zero).
  223. More precisely, when any process exits, all processes whose process
  224. group (p_pgrp) equals the pid of the terminating process will have their
  225. p_pgrp set to zero; this check succeeds only in the case of a
  226. terminating process group leader.
  227. @.NH 2
  228. System V Controlling Terminals
  229. @.PP
  230. A terminal that is currently open by a process may also be a "controlling
  231. terminal" for a process group.  When certain
  232. control characters are typed on a controlling terminal, signals are 
  233. sent by the terminal driver to all processes that belong to the process
  234. group associated with the terminal.
  235. @.PP
  236. When a process becomes a process group leader (via setpgrp(2)) it
  237. automatically loses its controlling terminal.  After this,
  238. the first terminal (that is not already a controlling terminal)
  239. opened by the process
  240. is assigned to be the controlling terminal for that process.
  241. Also, the process group associated with that terminal (t_pgrp, also known
  242. as the tty group ID) is set
  243. equal to the process group associated with the process group leader
  244. (p_pgrp).
  245. All child processes inherit the controlling terminal and
  246. process group of their parent.
  247. @.PP
  248. More precisely, in System V, the process group associated with a 
  249. terminal (t_pgrp), can be changed in the following ways:
  250. @.IP (1)
  251. When a terminal is opened by a process group leader (pid == p_pgrp)
  252. that does not already have a controlling terminal,
  253. it becomes the controlling terminal for that process group 
  254. (t_pgrp is set equal to p_pgrp) if it is not already a controlling
  255. terminal.
  256. @.IP (2)
  257. When a process group leader (pid == p_pgrp) dies, if it has a 
  258. controlling terminal that is associated with the same process
  259. group (t_pgrp == p_pgrp), then that terminal is disassociated from that
  260. process group (t_pgrp is set to zero).
  261. @.IP (3)
  262. When the last process to have a terminal open closes that terminal,
  263. the terminal is disassociated from its process group (t_pgrp
  264. is set to zero).
  265. @.NH 2
  266. System V Typical Scenario
  267. @.PP
  268. This is a typical scenario for the birth and death of a process group and
  269. its controlling terminal.
  270. @.PP
  271. The init(1M) process wants to enable a terminal for login.  It calls fork(2)
  272. to create a new process and then calls
  273. setpgrp(2) to make the process a process
  274. group leader which also removes the process's controlling terminal.  It
  275. then runs the getty(1M) program as the process via exec(2).
  276. Getty opens the terminal causing it to become getty's controlling terminal
  277. and be associated with getty's process group (t_pgrp is set to p_pgrp).
  278. Getty replaces itself with login(1) which replaces itself with a login
  279. shell, e.g., sh(1).
  280. Usually no program calls setpgrp(2) and thus all descendent processes
  281. of the login shell are in the same process group and have the same
  282. controlling terminal; keyboard signals are sent to all processes launched
  283. during this session.
  284. @.PP
  285. When a logout occurs, the login shell (which is the process group leader) 
  286. dies and the controlling terminal is freed up (t_pgrp is set to zero) so
  287. that it can be claimed as a controlling terminal by a subsequent getty 
  288. respawned by init.
  289. SIGHUP is sent to all processes in the same process group.
  290. The process group (p_pgrp) of all descendent processes is then set to zero.
  291. @.PP
  292. Note that there may continue to be background processes (previously
  293. started by the
  294. now defunct login shell) which continue to execute but
  295. keyboard signals will no longer be sent to these processes (since
  296. both t_pgrp and p_pgrp equal zero).
  297. @.sp 1
  298. @.NH
  299. 4.2BSD
  300. @.NH 2
  301. Introduction
  302. @.PP
  303. 4.2BSD process groups closely resemble the concept of a task within
  304. a login session, where a task represents a set of processes which
  305. are affected as a group by job control operations.
  306. Every time a job control shell (e.g., csh) spawns either a foreground
  307. or background command, all processes in the pipeline (and their
  308. descendents) are placed in their own unique process group with the
  309. first command in the pipeline being the process group leader.
  310. @.PP
  311. A task is in the foreground when the process group associated with the
  312. controlling terminal for the task (t_pgrp) is equal to the process
  313. group associated with the processes in the task (p_pgrp).  
  314. Otherwise the task is in the background.
  315. A job control shell moves a job between the foreground and background
  316. by adjusting the terminal process group (t_pgrp) of the controlling
  317. terminal.
  318. @.PP
  319. Note that 4.2BSD forms new process groups with process group leaders
  320. much more often than System V usually does (every command versus
  321. every login).
  322. @.NH 2
  323. 4.2BSD Process Group Handling
  324. @.PP
  325. In 4.2BSD, the process group associated with a process (p_pgrp)
  326. can be altered in two ways.  The first is via setpgrp(2).  4.2BSD's setpgrp(2)
  327. is analogous to System V's setpgrp(2) except that the former can affect
  328. processes other than the current process and can cause the affected
  329. process to adopt a process group other than that process's process ID (pid).
  330. Thus, unlike System V, a process can cease to be a process group leader.
  331. @.PP
  332. In addition to setpgrp(2), a process that is not a member of any
  333. process group (p_pgrp == 0) will "inherit" or join the process
  334. group associated with its controlling terminal at the time the
  335. process is assigned a controlling terminal during open(2).
  336. If the terminal being opened is not presently the controlling
  337. terminal for any process group, then the process opening the
  338. terminal will first be made a process group leader (p_pgrp will
  339. be set to p_pid) and then the terminal will become the controlling
  340. terminal for this new process group.  All this is done by the
  341. tty open code.
  342. @.PP
  343. When a new process is created it inherits the process group of its parent.
  344. @.PP
  345. Unlike System V init(1M), 4.2BSD init(8) does not call setpgrp(2) when spawning
  346. other processes.  All processes spawned by init inherit init's process
  347. group which happens to be zero ("not a member of any process group").
  348. This is actually crucial for assigning controlling terminals; see below.
  349. @.NH 2
  350. 4.2BSD Controlling Terminals
  351. @.PP
  352. Unlike System V, a 4.2BSD process does not lose its controlling terminal
  353. when altering its process group (via setpgrp(2)).
  354. Also unlike System V, a 4.2BSD process that is a process group leader
  355. (pid == p_pgrp) but which has no controlling terminal does not receive
  356. a controlling terminal when opening a new terminal.
  357. @.PP
  358. A process can obtain a controlling terminal under 4.2BSD in only the
  359. following ways:
  360. @.IP (1)
  361. A process can inherit a controlling terminal from its parent.
  362. @.IP (2)
  363. A process that is not a member of any process group (p_pgrp == 0)
  364. can open any terminal and that terminal will become its controlling
  365. terminal (whether or not it is already the controlling terminal for another
  366. process).
  367. However, this can happen in one of two ways:
  368. @.IP
  369. If the terminal is not already a controlling terminal (t_pgrp == 0)
  370. then the opening process becomes a process group leader (its p_pgrp is set
  371. equal to its pid) and the terminal becomes its controlling terminal
  372. (t_pgrp is set to the new p_pgrp value).
  373. @.IP
  374. If the terminal is already a controlling terminal for another process
  375. (t_pgrp is not zero) then the opening process joins the process group
  376. already associated with the controlling terminal.  That is, p_pgrp is
  377. set equal to the current t_pgrp.  Note that the opening process does
  378. not become a process group leader, i.e., p_pgrp is not equal to its pid.
  379. @.IP
  380. Note that this procedure only happens during the first terminal open
  381. for a process that was either originally spawned by init or whose
  382. ancestor processes (all the way back to init) never altered their
  383. process group (p_pgrp) either by opening a terminal or calling setpgrp(2).
  384. @.PP
  385. A terminal ceases to be a controlling terminal (t_pgrp is set to zero) under
  386. 4.2BSD in the following way:
  387. @.IP (1)
  388. When the last process to have a terminal open closes that terminal
  389. then the terminal is disassociated from its process group (t_pgrp
  390. is set to zero).
  391. @.PP
  392. There are two other facilities unique to 4.2BSD which affect access to
  393. control terminals: the TIOCSPGRP ioctl(2) and vhangup(2).
  394. @.PP
  395. The TIOCSPGRP ioctl(2) function changes a terminal's process group (t_pgrp)
  396. to any desired value.
  397. It is typically used by csh(1) to control which set of processes (process
  398. group) is in the foreground.
  399. @.PP
  400. The vhangup(2) function is invoked by init after
  401. forking but before exec'ing getty.  This function removes read and
  402. write permission for all processes (including the caller)
  403. that have the controlling terminal
  404. open (whether or not it is their controlling terminal).  It then sends
  405. SIGHUP to the process group associated with the terminal (t_pgrp).
  406. The latter action is similar to the System V functionality that sends
  407. SIGHUP to a process group on death of the process group leader; 4.2BSD
  408. does not do this on the death of a process group leader.
  409. @.NH 2
  410. 4.2BSD Typical Scenario
  411. @.PP
  412. This is a typical scenario for the birth and death of a login, its
  413. controlling terminal, and process groups associated with a job.
  414. @.PP
  415. The init(8) process wants to enable a terminal for login.
  416. First it creates a new process via fork(2).
  417. Then it opens the terminal which
  418. (because the p_pgrp inherited from init is zero)
  419. causes it to become the controlling
  420. terminal for this process and either alters the process group (p_pgrp) of
  421. the process to match the terminal process group (t_pgrp) if non-zero, or
  422. alters both p_pgrp and t_pgrp to equal the process ID (pid) if t_pgrp is zero.
  423. At this point the new process has a controlling terminal whose process group
  424. (t_pgrp) is equal to the process's process group (p_pgrp).  However, the 
  425. process may not be a process group leader (i.e., p_pgrp may not equal pid).
  426. @.PP
  427. Now the new process calls vhangup(2) to remove
  428. access permissions for the controlling terminal from all processes (as well
  429. as sending SIGHUP to any processes in the process group previously
  430. associated with the terminal).
  431. The new process then reopens the terminal to get a file descriptor with
  432. read and 
  433. write permissions since the vhangup(2) removed these permission from the
  434. file descriptor returned by the previous open.
  435. The previous file descriptor is not closed until now to prevent losing
  436. the controlling terminal; (remember that p_pgrp for the new process is
  437. no longer zero.)
  438. @.PP
  439. The new process now replaces itself with getty(8) which replaces itself
  440. with login(1) which replaces itself with a login shell, e.g., csh(1).
  441. Csh now begins to manipulate the process group associated with the
  442. terminal (t_pgrp) via the TIOCSPGRP and TIOCGPGRP ioctl(2) calls and
  443. the process group associated with its child processes (p_pgrp) via
  444. setpgrp(2) in order to allow job control.  This happens (briefly) in the
  445. following way:
  446. @.PP
  447. Csh launches a pipeline by making all programs in the pipeline be
  448. immediate descendents of csh.  (This is different from sh which makes
  449. all programs in the pipeline except the last be descendents of the
  450. last program in the pipeline.)
  451. All programs in the pipeline belong to the same process group (not the
  452. same as csh's process group) and the first program in the pipeline is
  453. the process group leader (its pid is equal to the process group for the
  454. pipeline).
  455. If the pipeline is being launched in the foreground (or moved to the
  456. foreground) then the process group associated with the terminal (t_pgrp)
  457. is set to the process group of the pipeline.
  458. @.PP
  459. When a logout occurs, the login shell dies.
  460. Any pending SIGTTIN, SIGTTOU, and SIGTSTP signals are cleared for all 
  461. descendent processes.
  462. All immediate child processes are inherited as orphans by init; if any are
  463. currently stopped then they are killed (SIGKILL).
  464. If the exiting process is the last process that has the controlling 
  465. terminal open then the terminal's process group (t_pgrp) is set to zero,
  466. otherwise it is left alone.
  467. Nothing special is done for process group leaders; in fact, login shells
  468. are frequently not process group leaders.  (SIGHUP is not sent and the
  469. controlling terminal is not necessarily cleared.)
  470. @.PP
  471. Note that there may continue to be processes (previously
  472. started by the now defunct login shell) which continue to execute.
  473. And that keyboard signals can still be sent to these processes under some
  474. circumstances (specifically when the processes were in the foreground
  475. (p_pgrp == t_pgrp) when the login shell died; this usually only happens
  476. when the login shell is killed from another terminal via kill(1).)
  477. Note also that this continues to be true even after a new session logs in
  478. on the same terminal since the new login shell joins the process group
  479. which is already associated with the terminal from the prior login.
  480. @.NH 2
  481. Job Control Signal Handling
  482. @.PP
  483. The following discussions concerning signals and kernel process
  484. synchronization are similar to ones found in [Thom78], [Ritch79],
  485. and [Bach79].
  486. @.NH 3
  487. Basic Overview
  488. @.PP
  489. Usually a process is either running or sleeping waiting for an event to
  490. occur (e.g., I/O completion).  When a signal is sent to a process (either
  491. by another process or an I/O driver) what actually occurs is that a flag
  492. is set for the receiving (or target) process indicating that the signal
  493. has been sent and the target process performs the actual signal operation
  494. to itself the next time it runs.  Thus sending a signal amounts to
  495. requesting the target process to itself perform a particular action.
  496. If the target process is already running it is interrupted to process
  497. the signal.  If it is runnable but not currently running then the
  498. system merely waits for it to become the currently running process at
  499. which point the signal is acknowledged.  If the target process
  500. is sleeping then either
  501. it is moved into a runnable state (if it is sleeping at an "interruptable"
  502. priority) or it is left sleeping (at an "uninterruptable" priority) and
  503. the signal is not acknowledged until the slept on event occurs.
  504. @.PP
  505. The kernel procedure which sends a signal is psignal() and is executed
  506. by the sending process or driver.
  507. Psignal() updates a list of pending signals for the receiving process.
  508. If the receiving process is the currently running process and it is
  509. executing in kernel mode then the pending signal is acknowledged
  510. when the current system call completes.
  511. (This is the case where the sending process and the receiving process
  512. are the same.)
  513. If the receiving process is the currently running process and it is
  514. executing in user mode then a special event is generated
  515. which causes the process to
  516. enter the kernel and acknowledge the pending signal.
  517. (This is the case where the sending "process" is really an interrupt
  518. handler which, for example, is servicing an interrupt character
  519. typed on a user's terminal.)
  520. If the receiving process is
  521. sleeping but not holding off signals then it is set running via wakeup();
  522. the pending signal is acknowledged as soon as the receiving process executes.
  523. If the receiving process is suspended in a sleep state that holds off
  524. signals ("sleeping uninterruptably") then it is left sleeping;
  525. the pending signal will be acknowledged after the waited for event occurs.
  526. @.PP
  527. The procedure which tests for a pending signal is issig() and is executed
  528. by the receiving process.
  529. Issig() is executed whenever the receiving process changes from kernel
  530. mode to user mode execution; for example, at the completion of a system call.
  531. It is also executed whenever the receiving process is awakened from
  532. being suspended in a sleep state that does not hold off signals ("sleeping
  533. interruptably").
  534. @.PP
  535. The procedure which performs the requested signal operation (e.g.,
  536. invoking a signal handler or killing the process) is psig() and is
  537. executed by the receiving process if issig() returns true.
  538. @.PP
  539. This basic structure is essentially the same in System V, 4.2BSD, and
  540. HP-UX.  However, under 4.2BSD-style job control, these general principles
  541. can work slightly differently:
  542. @.PP
  543. When processing stop signals,
  544. the psignal() function, called by the sending process, actually stops 
  545. the target process sometimes.  In these cases, the target process
  546. never realizes that it received the signal or that it stopped.  
  547. However, in other cases, psignal() performs the usual process of 
  548. setting the flag (p_sig) requesting
  549. that the target process stop itself the next time it runs.
  550. @.PP
  551. The issig() function, called by the target process, can actually stop the
  552. target process.
  553. @.PP
  554. The psig() function is only called in the case where a user handler
  555. has been provided for the job control signal.
  556. @.PP
  557. A more complete description of job control signal handling is contained
  558. in the pseudocode below.
  559. @.NH 3
  560. psignal()
  561. @.PP
  562. To send SIGCONT to a target process:
  563. @.PB
  564. sending SIGCONT clears any pending stop signals;
  565. if the target process is STOPPED but is also SLEEPING (p_wchan != 0)
  566.     merely continue the process's SLEEP;
  567.  
  568. else if the target process is STOPPED and is NOT SLEEPING (p_wchan == 0)
  569.     set the process RUNNING;
  570. @.PE
  571. @.PP
  572. To send a stop signal (SIGTSTP, SIGTTIN, SIGTTOU, SIGSTOP) to a target process:
  573. @.PB
  574. sending a stop signal clears any pending SIGCONT;
  575.  
  576. if the target process is RUNNABLE or RUNNING
  577.     note the pending signal in p_sig;
  578.  
  579. else if the target process is SLEEPING NON-interruptably
  580.     note the pending signal in p_sig;
  581.  
  582. else if the target process is SLEEPING interruptably
  583.      and IS catching the signal
  584.     note the pending signal in p_sig and
  585.     wakeup the process from its sleep;
  586.  
  587. else if the target process is SLEEPING interruptably
  588.      and is NOT catching the signal
  589.     stop the process by setting its state to SSTOP
  590.     but leave it sleeping on its p_wchan;
  591.     send SIGCLD to parent (if it expects BSD-style)
  592. @.PE
  593. @.PP
  594. General note: sending a stop signal (other than SIGSTOP) to a child
  595. of init causes the target process to be killed.
  596. @.NH 3
  597. issig()
  598. @.PP
  599. Issig() is called in all cases except where the process was
  600. sleeping interruptably and was not catching the signal. 
  601. @.PP
  602. To acknowledge a pending SIGCONT or stop signal:
  603. @.PB
  604. if in the middle of a VFORK
  605.     hold off all stop signals (pretend they don't exist yet)
  606.  
  607. else if catching the signal
  608.     return a request to invoke user signal handler via psig()
  609.  
  610. else if SIGCONT
  611.     do nothing  /* pretend it doesn't exist */
  612.  
  613. else /* stop signals */
  614.     stop the process by setting its state to SSTOP
  615.     send SIGCLD to parent (if it expects BSD-style)
  616.     call swtch() to dispatch another process
  617. @.PE
  618. @.PP
  619. General note: sending a stop signal (other than SIGSTOP) to a child
  620. of init causes the target process to be killed.
  621. @.NH 3
  622. psig()
  623. @.PP
  624. Psig() is called whenever issig() returns an indication
  625. that a user handler is defined for a job control signal.
  626. Psig() merely invokes the user signal handler.
  627. @.NH 3
  628. wakeup()
  629. @.PP
  630. The fact that a process is sleeping (waiting for an event to occur)
  631. is indicated by two process state values:  p_wchan is non-zero,
  632. indicating the event being waited for, and the process state is SSLEEP.
  633. @.PP
  634. Wakeup() usually causes all processes waiting (sleeping) on a specified 
  635. event to be awakened.  When a process is awakened two things
  636. happen:  The process is removed from the sleep queue (p_wchan is cleared)
  637. and it is added to the run queue.
  638. @.PP
  639. If, however, wakeup() discovers a process whose p_wchan matches the 
  640. specified event but whose process state is SSTOP (stopped) then
  641. the process is removed from the sleep queue (indicating that the
  642. waited for even has happened) but it is not placed on the run queue.
  643. A subsequent SIGCONT will cause it to be placed on the run queue.
  644. @.PP
  645. Thus it is possible to have a process which is both sleeping (p_wchan
  646. non-zero) and stopped (process state is SSTOP rather than SSLEEP).
  647. @.NH 3
  648. Signal Setup via init
  649. @.PP
  650. When init(8) launches any process it causes the process to ignore all
  651. the job control stop signals (SIGTSTP, SIGTTIN, & SIGTTOU).  This allows
  652. login shells which are not job control shells to automatically ignore
  653. the signals.  Further, all descendent processes of such a login shell
  654. will also ignore these signals unless they explicitly enable them.
  655. @.NH 2
  656. Foreground/Background Processes
  657. @.NH 3
  658. Basic Overview
  659. @.PP
  660. 4.2BSD job control supports the notion of a process being in the
  661. @.I foreground
  662. or
  663. @.I background.
  664. The distinction is a background process is usually forced to stop when
  665. it attempts to perform I/O (including most control operations) on its
  666. controlling terminal, while a foreground process is not hindered.
  667. @.PP
  668. Specifically, when a background process attempts to read from its controlling
  669. terminal it is sent the SIGTTIN signal which, by default, causes it to stop.
  670. When it attempts to write to its controlling terminal and LTOSTOP has been
  671. enabled for the terminal, then the process is sent the SIGTTOU signal which,
  672. by default, causes it to stop.
  673. If, however, a background process has chosen to catch the signal, the 
  674. specified user handler is invoked.
  675. If the process is ignoring or masking the stop signal(s), then
  676. the terminal I/O request returns an I/O error, EIO.
  677. @.PP
  678. A background process is one whose process group (p_pgrp) is not equal
  679. to the process group of its controlling terminal (t_pgrp) (and t_pgrp
  680. is not zero).  All other processes (including ones doing I/O to terminals
  681. that are not their controlling terminals) are considered to be in the
  682. foreground.
  683. @.NH 3
  684. Tty Driver Provisions
  685. @.PP
  686. To distinguish between foreground and background
  687. programs the tty driver must perform checks on attempted I/O operations to
  688. a process's controlling terminal.
  689. This is done in several places.
  690. @.PP
  691. At the beginning of a read/write system call the tty driver checks
  692. to see if the calling process is in the background.
  693. If it is, then all processes in the
  694. process group of the calling process are sent the appropriate
  695. signal (SIGTTIN or SIGTTOU) unless the signal is masked or ignored
  696. by the calling process.  In this case the driver returns the EIO error.
  697. After the tty driver sends the signal, the calling process is put to
  698. sleep waiting for the \fIlightning bolt event\fP\(dg.
  699. @.FS
  700. \(dg The lightning bolt event is a standard
  701. @.UX
  702. event which occurs frequently, for example, every second.
  703. @.FE
  704. This allows the calling process to receive the signal (and usually stop).
  705. When the process returns from the sleep (usually by being continued) the
  706. tty driver repeats the foreground/background check before proceeding with
  707. the operation.
  708. @.PP
  709. When the process is in the foreground, the I/O operation proceeds.
  710. In the case of a terminal read this usually results in the process being
  711. put to sleep to wait for input characters to arrive.
  712. At this point the user could type their suspend character (e.g., ^Z).
  713. This causes the interrupt portion of the tty driver to send SIGTSTP
  714. to the controlling terminal's process group (i.e., all processes which
  715. are in the foreground).  In our scenario this
  716. would include the process sleeping on terminal input, and this would
  717. typically cause it to stop.
  718. @.PP
  719. When a sleeping process is stopped it is also left sleeping as well.
  720. If, in this case, tty input characters subsequently arrived then the 
  721. process would
  722. be awakened.  However, because it is also stopped, it would not be
  723. set running; it would merely be "unslept".
  724. @.PP
  725. At some later time the process would be continued (e.g., via a csh
  726. "fg" or "bg" command which sends SIGCONT).
  727. If the process had not been previously unslept it would merely continue
  728. its sleeping; it would receive no indication that it had stopped and 
  729. continued.
  730. If the process had been previously unslept it would now be set running.
  731. @.PP
  732. When the process is set running it resumes execution in the tty driver.
  733. Because a (potentially substantial) amount of time has elapsed and
  734. because the process may have been stopped and restarted,
  735. the tty driver is no longer sure whether this process is still in
  736. the foreground.
  737. So before checking if input characters are available, the tty driver
  738. rechecks whether the process is in the foreground or background.
  739. This is necessary because, in our
  740. scenario, the stopped process could have been continued in the background
  741. (via csh "bg").  To check this the tty driver merely repeats the
  742. foreground/background check it made at the beginning of the system call.
  743. @.sp 1
  744. @.NH
  745. SYSTEM V INCOMPATIBILITIES AND THEIR RESOLUTIONS
  746. @.PP
  747. Job control as implemented in 4.2BSD is incompatible with System V
  748. semantics in some significant respects.  This section discusses
  749. each of these incompatibilities and the resolution implemented in HP-UX
  750. to maintain System V compatibility.
  751. @.PP
  752. The system interface needed to support 4.2BSD-style job control,
  753. tailored for System V compatibility as discussed in this section,
  754. is presented in the form of manual page excerpts in [Len86].
  755. @.NH 2
  756. Setpgrp(2) Changes
  757. @.PP
  758. Because the needed semantics of 4.2BSD setpgrp(2) conflict with the
  759. semantics of System V setpgrp(2), the 4.2BSD setpgrp(2) function was renamed
  760. to be setpgrp2(2).  (The choice of new name is arbitrary; setpgrp2 was chosen
  761. in the same spirit as 4.2BSD's wait3(2).)
  762. @.NH 2
  763. SIGHUP Changes
  764. @.PP
  765. System V semantics state that when a process group leader dies, all processes
  766. in the same process group are sent the SIGHUP signal which, by default, kills
  767. all the processes.
  768. @.PP
  769. Job control shells execute a command by making all processes in the pipeline
  770. belong to the same (brand new) process group and by making the first program
  771. in the pipeline be the process group leader.  Typically, the first program in a
  772. pipeline terminates before the other programs.  Under System V semantics,
  773. this would cause the premature death of the remaining pipeline.  Because
  774. of this, 4.2BSD does not generate SIGHUP on process group leader death.
  775. @.PP
  776. In order to support System V semantics and still allow job control to
  777. function properly, HP-UX makes a distinction between a "System V process
  778. group leader" and a "job control process group leader".  A System V 
  779. process group leader is given System V semantics (SIGHUP is generated)
  780. and a job control process group leader is given 4.2BSD semantics (SIGHUP
  781. is not generated).
  782. A process which becomes a process group leader via setpgrp(2) is considered
  783. to be a System V process group leader.
  784. A process which becomes a process group leader via setpgrp2(2) is considered
  785. to be a job control process group leader.
  786. Since the HP-UX (and System V) init(1M) program calls setpgrp(2) on behalf
  787. of all processes it spawns, all login shells start out as System V process
  788. group leaders.  A process must explicitly call setpgrp2(2) to deviate from
  789. the System V semantics.
  790. @.NH 2
  791. SIGCLD Changes
  792. @.PP
  793. Under System V, SIGCLD is sent to a process whenever one of its immediate
  794. child processes dies.
  795. Under 4.2BSD, SIGCLD (or its variant, SIGCHLD) is also generated
  796. when a process changes state from running to stopped.
  797. Since a System V application would not expect to receive SIGCLD
  798. under these new circumstances and since a job control shell would
  799. not be able to function properly without such notification, a compatible
  800. compromise was developed.
  801. @.PP
  802. The (parent) process wishing to trap SIGCLD may set a flag when calling
  803. the HP-UX sigvector(2)\(dg
  804. @.FS
  805. \(dg Sigvector(2) is an HP-UX extension proposed to the IEEE P1003 [Head85]
  806. which supports both the reliable
  807. signal operations of 4.2BSD sigvec(2) and the conventional signal 
  808. operations of System V signal(2).  In HP-UX, signal(2) is implemented
  809. as a library using sigvector(2).
  810. Note that the changes proposed here to sigvector(2) can be identically
  811. made to 4.2BSD sigvec(2).
  812. @.FE
  813. \|routine to establish a signal handler.  This flag
  814. will cause SIGCLD to be sent for stopped children, in addition to terminated
  815. children.
  816. A System V application using signal(2) will see the System V compatible
  817. SIGCLD semantics.
  818. @.NH 2
  819. Controlling Terminal Changes
  820. @.PP
  821. Under System V, whenever a process group leader dies, the controlling terminal
  822. associated with that process group (if any) is deallocated (disassociated from
  823. that process group).
  824. 4.2BSD does not deallocate controlling terminals on process group leader death
  825. for the following reason:
  826. Job control shells make the lead process in every pipeline a process
  827. group leader.  If the controlling terminal for each pipeline were deallocated
  828. whenever the lead process terminated, then the remaining processes would
  829. effectively become background processes (assuming they were currently in
  830. the foreground) and would stop when any of them attempted subsequent I/O to the
  831. terminal.
  832. @.PP
  833. To allow both semantics, controlling terminals are only deallocated when
  834. a "System V process group leader" dies and not when a "job control process
  835. group leader" dies.  (See the discussion of SIGHUP changes above.)
  836. @.PP
  837. However, this change leads to the following problem:
  838. In order for a terminal to be allocated as a controlling terminal for a 
  839. new login, it must be deallocated when the previous login terminates.
  840. System V relies on process group leader death to deallocate controlling
  841. terminals (since all login shells are forced to be process group leaders
  842. by init(1M)).
  843. This is no longer reliable since login shells could become "job control
  844. process group leaders".  Further, not all logins are spawned directly
  845. by init(1M); the 4.2BSD rlogin facility is a prime example.
  846. 4.2BSD solves this problem by allowing a new login to join the process
  847. group of the controlling terminal which is still allocated from the 
  848. previous login.  However this violates System V compatibility.
  849. @.PP
  850. The solution chosen was to mark a process that causes a controlling
  851. terminal to be allocated and to deallocate the controlling terminal
  852. whenever that process terminates.  This reliably catches logins which
  853. are spawned either directly or indirectly from init(1M), whether they are
  854. "System V process group leaders" or not.  Controlling terminals continue
  855. to be deallocated on death of System V process group leaders using the
  856. System V semantics.
  857. @.NH 2
  858. Security
  859. @.PP
  860. Several security holes exist in the 4.2BSD process group altering
  861. mechanisms.  To plug these holes the following changes were made.
  862. @.PP
  863. 4.2BSD setpgrp(2) allows a process to alter the process group associated
  864. with another process to any value.
  865. 4.2BSD restricts this operation so that the
  866. affected process must pass the same security restrictions enforced when
  867. sending signals, or must be a descendent of the calling process.
  868. However, this still allows a process to join a process group
  869. already associated with another user.
  870. To tighten this security, setpgrp2(2) was further restricted such that if the
  871. specified new process group value is equal to the process ID (pid) or
  872. process group ID of any existing processes, then all such processes must
  873. pass the above security restrictions.
  874. @.PP
  875. Similarly, the 4.2BSD TIOCSPGRP ioctl(2) allows a terminal's process group
  876. to be altered to any value.  This allows a user's terminal to easily become
  877. an additional "controlling terminal" for another user's process group;
  878. keyboard signals can be sent to the other user's processes, thus bypassing
  879. the security enforced by kill(2).
  880. Because of this, the TIOCSPGRP ioctl(2) was altered to enforce similar
  881. security restrictions as setpgrp2(2).
  882. @.PP
  883. In System V and 4.2BSD, a process can obtain access to its controlling
  884. terminal by opening the file \fI/dev/tty\fP.  Under System V, processes
  885. left executing after a user's logout are allowed further access to
  886. \fI/dev/tty\fP until the terminal it represents is reallocated as a
  887. controlling terminal for a new login.  More specifically, \fI/dev/tty\fP
  888. access is allowed whenever the process group ID of the leftover process
  889. matches the process group ID of the terminal.  These IDs continue to match
  890. immediately after logout (since both have been zeroed) until the terminal
  891. is re-enabled for login by getty(1M).  (Note that when the new login
  892. terminates, \fI/dev/tty\fP access is restored again to these prior processes
  893. because the controlling terminal's process group ID is re-zeroed.)  Further,
  894. if a process has its controlling terminal opened directly (not
  895. via the \fI/dev/tty\fP synonym) then access is not restricted at all after
  896. logout.
  897. These System V semantics can constitute security problems.  However, they
  898. are not explicitly required by the System V Interface Definition [ATT86].
  899. @.PP
  900. 4.2BSD does nothing to hamper \fI/dev/tty\fP access for processes remaining
  901. after logout.  The process group ID for the controlling terminal is not
  902. altered, and, in fact, it is preserved even into the next login (since
  903. subsequent logins join the already existing process group associated with the
  904. terminal, if any).  These semantics also represent security problems.
  905. However, 4.2BSD does prohibit access to the controlling terminal if it
  906. is opened directly; this is accomplished when init(8) issues the vhangup(2)
  907. system call.
  908. @.PP
  909. Although preserving the System V semantics for controlling terminal access
  910. after logout is not deemed necessary or even recommended, it is easy to do
  911. in the following way.
  912. Whenever a process that allocated a controlling terminal dies, all processes
  913. which share this controlling terminal have their process group ID zeroed.
  914. This is analogous to, and occurs in addition to, the System V behavior of
  915. zeroing the process group ID for all related processes when their process
  916. group leader dies.  \fI/dev/tty\fP checks similar to System V can then
  917. be employed.
  918. @.NH 2
  919. TTY Driver Considerations
  920. @.PP
  921. For System V compatibility, the suspend and delayed suspend characters
  922. are defaulted to a disabled value (0377).  This means that job control
  923. is "inactive" by default when a user logs on.  The user must explicitly
  924. activate job control by defining either or both of these characters via
  925. stty(1) or some similar interface.
  926. @.PP
  927. There should be no problem allowing 4.2BSD-style job control, as modified
  928. here, to co-exist with System V's shell layers job control system.
  929. (See shl(1) and sxt(7) in the System V Release 2 reference manuals.)
  930. @.sp 1
  931. @.NH
  932. HP-UX
  933. @.NH 2
  934. Introduction
  935. @.PP
  936. HP-UX process groups are used in two major ways.
  937. @.PP
  938. System V process groups closely resemble the concept of a login session.
  939. That is, all processes spawned during the same login session tend to
  940. belong to the same process group, and keyboard signals are typically
  941. sent to all processes spawned from the login session.
  942. @.PP
  943. Job control process groups closely resemble the concept of a task within
  944. a login session, where a task represents a set of processes which
  945. are affected as a group by job control operations.
  946. Every time a job control shell (e.g., csh) spawns either a foreground
  947. or background command, all processes in the pipeline (and their
  948. descendents) are placed in their own unique process group with the
  949. first command in the pipeline being the process group leader.
  950. @.PP
  951. A task is in the foreground when the process group associated with the
  952. controlling terminal for the task (t_pgrp) is equal to the process
  953. group associated with the processes in the task (p_pgrp).  
  954. Otherwise the task is in the background.
  955. A job control shell moves a job between the foreground and background
  956. by adjusting the terminal process group (t_pgrp) of the controlling
  957. terminal.
  958. @.PP
  959. Note that a job control shell forms new process groups with process 
  960. group leaders
  961. much more often than a non-job control (System V) shell usually does
  962. (every command versus every login).
  963. @.NH 2
  964. HP-UX Process Group Handling
  965. @.PP
  966. In HP-UX, the process group associated with a process (p_pgrp)
  967. can be altered via setpgrp(2) or setpgrp2(2).
  968. @.PP
  969. As in System V, setpgrp(2) can only set the process
  970. group to equal the process ID (pid) of the process.  When this happens,
  971. the resulting process with pid = p_pgrp is called a System V process group
  972. leader.
  973. @.PP
  974. Setpgrp2(2) is analogous to setpgrp(2) except that it can affect
  975. processes other than the current process and can cause the affected
  976. process to adopt a process group other than that process's process ID (pid).
  977. Setpgrp2(2) also forms job control process groups rather than System V
  978. process groups.
  979. Using setpgrp2(2), the calling process, or certain other processes, can either
  980. become a job control process group leader or can cease to be a process
  981. group leader.
  982. @.PP
  983. Because job control process groups are handled slightly differently
  984. by HP-UX than System V process groups, HP-UX marks processes
  985. that are job control process group leaders (i.e., that have
  986. called setpgrp2(2) without subsequently calling setpgrp(2)).
  987. @.PP
  988. The init(1M) process spawns all other processes on the system either
  989. directly or indirectly.  Before directly spawning a process
  990. (after the fork(2) but before the exec(2)),
  991. init calls setpgrp(2).
  992. Thus all original children (not orphans) of init are forced to
  993. start out as System V process group leaders.
  994. @.PP
  995. When a new process is created, it is assigned a new pid but it inherits
  996. the process group number of its parent.  Thus child processes are, by
  997. default, not process group leaders (although they can become a process
  998. group leader via either setpgrp(2) or setpgrp2(2)).
  999. @.PP
  1000. When a System V process group leader that has a controlling terminal
  1001. (see below) terminates, SIGHUP is sent to all processes in the same
  1002. process group.
  1003. Further, when a System V process group leader terminates, all processes
  1004. which belong to this process group are altered to belong to no process group
  1005. (their p_pgrp is set to zero).
  1006. @.PP
  1007. Also, whenever any process that allocated a controlling terminal terminates,
  1008. all processes that share this controlling terminal are altered to belong
  1009. to no process group (their p_pgrp is set to zero).
  1010. @.PP
  1011. When any process exits, any pending SIGTTIN, SIGTTOU, and SIGTSTP signals
  1012. are cleared from all descendent processes (not just immediate children).
  1013. @.NH 2
  1014. HP-UX Controlling Terminals
  1015. @.PP
  1016. A terminal that is currently open by a process may also be a "controlling
  1017. terminal" for a process group (collection of processes).  When certain
  1018. control characters are typed on a controlling terminal, signals are 
  1019. sent by the terminal driver to all processes which belong to the process
  1020. group associated with the terminal.  These include the job control suspend
  1021. and delayed suspend characters.
  1022. @.PP
  1023. Controlling terminals also play a role in determining whether a process
  1024. is in the foreground or background.  See FOREGROUND/BACKGROUND PROCESSES
  1025. above.
  1026. @.PP
  1027. When a process becomes a System V process group leader (via setpgrp(2)) it
  1028. automatically loses its controlling terminal.
  1029. (This does not happen for a job control process group leader, i.e. when
  1030. calling setpgrp2(2).)
  1031. After this,
  1032. the first terminal (that is not already a controlling terminal)
  1033. opened by a process that is a (System V or job control) process group leader
  1034. is assigned to be the controlling terminal for that process;
  1035. also the process group associated with that terminal (t_pgrp) is set
  1036. equal to the process group associated with the process group leader
  1037. process (p_pgrp).
  1038. All child processes inherit the controlling terminal and
  1039. process group of their parent.
  1040. @.PP
  1041. More precisely, in HP-UX, the process group associated with a 
  1042. terminal (t_pgrp), can be changed in the following ways:
  1043. @.IP (1)
  1044. When a terminal is opened by a System V or job control
  1045. process group leader (pid == p_pgrp)
  1046. that does not already have a controlling terminal,
  1047. it becomes the controlling terminal for that process group 
  1048. (t_pgrp is set equal to p_pgrp) if it is not already a controlling
  1049. terminal.
  1050. @.IP (2)
  1051. When a System V process group leader (pid == p_pgrp) dies, if it has a 
  1052. controlling terminal that is associated with the same process
  1053. group (t_pgrp == p_pgrp), that terminal is disassociated from that
  1054. process group (t_pgrp is set to zero).
  1055. @.IP (3)
  1056. When any process dies which originally caused a controlling terminal
  1057. to be created (see (1) above),
  1058. if it still has a controlling terminal,
  1059. that terminal is disassociated from its
  1060. process group (t_pgrp is set to zero).
  1061. @.IP (4)
  1062. When the last process to have a terminal open closes that terminal,
  1063. the terminal is disassociated from its process group (t_pgrp
  1064. is set to zero).
  1065. @.IP (5)
  1066. The TIOCSPGRP ioctl(2) call can explicitly change a terminal's
  1067. process group (t_pgrp) to any value within certain security restrictions.
  1068. This is used by a job control shell to change which set of processes
  1069. (process group) is in the foreground.
  1070. @.NH 2
  1071. HP-UX Typical Scenario
  1072. @.PP
  1073. This is a typical scenario for the birth and death of a login, its 
  1074. controlling terminal, and the process groups associated with a job.
  1075. @.PP
  1076. The init(1M) process wants to enable a terminal for login.  It creates a
  1077. new process via fork(2) and calls setpgrp(2) to make it a System V process
  1078. group leader which also removes its controlling terminal.  Init then runs
  1079. the getty(1M) program as the process via exec(2).
  1080. Getty opens the terminal causing the terminal to become getty's controlling
  1081. terminal
  1082. and be associated with getty's process group (t_pgrp is set to p_pgrp).
  1083. As a side effect, this process is now marked as having created a controlling
  1084. terminal; when it dies the controlling terminal will be freed for re-use.
  1085. Getty replaces itself with login(1) which replaces itself with a login
  1086. shell.
  1087. @.PP
  1088. At this point one of two scenarios typically takes place.  The login shell
  1089. is either a job control shell (e.g., csh(1)) or it is not (e.g., sh(1)).
  1090. @.PP
  1091. If the login shell is not a job control shell then things proceed much
  1092. as they do on System V.
  1093. Usually no program calls setpgrp(2) or setpgrp2(2) and thus all 
  1094. descendent processes
  1095. of the login shell are in the same process group and have the same
  1096. controlling terminal; keyboard signals are sent to all processes launched
  1097. during this session.
  1098. @.PP
  1099. If the login shell is a job control shell, then job control operations are
  1100. performed.
  1101. Csh begins to manipulate the process group associated with the
  1102. terminal (t_pgrp) via the TIOCSPGRP and TIOCGPGRP ioctl(2) calls and
  1103. the process group associated with its child processes (p_pgrp) via
  1104. setpgrp2(2) in order to allow job control.  This happens (briefly) in the
  1105. following way:
  1106. @.PP
  1107. Csh launches a pipeline by making all programs in the pipeline be
  1108. immediate descendents of csh.  (This is different from sh which makes
  1109. all programs in the pipeline except the last be descendents of the
  1110. last program in the pipeline.)
  1111. All programs in the pipeline belong to the same process group (not the
  1112. same as csh's process group) and the first program in the pipeline is
  1113. the process group leader (its pid is equal to the process group for the
  1114. pipeline).  This process is specially marked as a job control process
  1115. group leader since it was established via setpgrp2(2); this basically
  1116. prevents SIGHUP from being sent to the pipeline when the lead process dies.
  1117. If the pipeline is being launched in the foreground (or moved to the
  1118. foreground) then the process group associated with the terminal (t_pgrp)
  1119. is set to the process group of the pipeline via the TIOCSPGRP ioctl(2).
  1120. @.PP
  1121. When a logout occurs, the login shell dies.
  1122. Any pending SIGTTIN, SIGTTOU, and SIGTSTP signals are cleared for all 
  1123. descendent processes.
  1124. All immediate child processes are inherited as orphans by init; if any are
  1125. currently stopped then they are killed (SIGKILL).
  1126. Since the login shell (actually the getty before it was overlaid) created
  1127. a controlling terminal, the controlling terminal is now freed (t_pgrp
  1128. is set to zero) so that it can be claimed as a controlling terminal by
  1129. a subsequent getty respawned by init; also, all processes which share
  1130. this controlling terminal have their process group (p_pgrp) set to zero.
  1131. @.PP
  1132. When a logout occurs and
  1133. the login shell is a System V process group leader, SIGHUP is sent
  1134. to all processes in the same process group, and the process group (p_pgrp)
  1135. of all descendent processes is set to zero.
  1136. @.PP
  1137. Note that there may continue to be background processes (previously
  1138. started by the
  1139. now defunct login shell) which continue to execute but
  1140. keyboard signals will no longer be sent to these processes (since
  1141. both t_pgrp and p_pgrp equal zero).
  1142. @.sp 1
  1143. @.NH
  1144. ACKNOWLEDGEMENTS
  1145. @.PP
  1146. The following people from Hewlett-Packard contributed to the interface
  1147. design and implementation of job control for HP-UX:
  1148. Jim Barton,
  1149. Dave Decot,
  1150. Larry Dwyer,
  1151. Jeff Glasson,
  1152. Rita Hanson,
  1153. Stephen Hares,
  1154. Steve Head,
  1155. Bob Lenk,
  1156. John Marvin,
  1157. Dave Mears,
  1158. Peter Notess,
  1159. Arn Schaeffer,
  1160. Eviatar Shafrir.
  1161. @.PP
  1162. Guy Harris from Sun Microsystems made many substantive comments and suggestions
  1163. which contributed to the interface design and to this paper.
  1164. @.sp 3
  1165. @.NH
  1166. REFERENCES
  1167. @.IP [ATT86] 15
  1168. \fISystem V Interface Definition\fP, Issue 2, AT&T, 1986.
  1169. @.IP [Bach84]
  1170. M. J. Bach and S. J. Buroff, "Multiprocessor 
  1171. @.UX
  1172. Operating Systems",
  1173. \fIAT&T Bell Lab. Tech. J.\fP, \fB63,\fP No. 8 (October 1984),
  1174. pp. 1733-1749.
  1175. @.IP [Head85]
  1176. Stephen Head and Donn Terry, "Reliable Signals Proposal",
  1177. IEEE P1003 Proposal #P.042, Hewlett-Packard Co., September 11, 1985.
  1178. @.IP [Joy80]
  1179. William Joy, "An Introduction to the C Shell",
  1180. Computer Science Division, University of California
  1181. at Berkeley, November 1980.
  1182. @.IP [Len86]
  1183. David Lennert, Guy Harris, et. al.,
  1184. "System V Compatible BSD-style Job Control Facilities",
  1185. IEEE P1003 Proposal #P.047, Hewlett-Packard Co. & Sun Microsystems,
  1186. April 9, 1986.
  1187. @.IP [Ritch79]
  1188. Dennis M. Ritchie, "The
  1189. @.UX
  1190. I/O System",
  1191. \fIUNIX Programmer's Manual\fP, Seventh Edition, Volume 2b,
  1192. Bell Telephone Laboratories, Murray Hill, NJ, January 1979.
  1193. @.IP [Roch85]
  1194. Marc J. Rochkind,
  1195. \fIAdvanced
  1196. @.UX
  1197. Programming\fP,
  1198. Englewood Cliffs, N.J.: Prentice-Hall, 1985.
  1199. @.IP [Harris86]
  1200. Guy Harris, "Notes on Signal, Terminal Interface, and User/Group ID
  1201. Handling Proposals", IEEE P1003 Proposal #P.045, Sun Microsystems,
  1202. January 11, 1986.
  1203. @.IP [Thom78]
  1204. K. Thompson, "UNIX Implementation",
  1205. \fIBell System Tech. J.\fP, \fB57,\fP No. 6 (July - August 1978),
  1206. pp. 1931-1946.
  1207. @.IP [UCB83]
  1208. \fIUNIX Programmer's Manual\fP, 4.2 Berkeley Software Distribution, Virtual 
  1209. VAX-11 Version, Computer Science Division, University of California
  1210. at Berkeley, August 1983.
  1211. @.PP
  1212. @.bp
  1213. @.DS C
  1214. APPENDIX
  1215.  
  1216. JOB CONTROL MANUAL PAGE EXCERPTS
  1217. @.DE
  1218. @.PP
  1219. The following pages contain the
  1220. @.UX
  1221. manual pages which are effected by adding a System V compatible
  1222. implementation of 4.2BSD job control.  Note that each manual page
  1223. generally contains only that portion of text which differs from the
  1224. System V manual page of the same name.  Sometimes, unchanged text
  1225. is provided for locality reference.  Changed text lines are flagged
  1226. with change bars.
  1227. @//E*O*F jobpaper.ms//
  1228. chmod u=rw,g=rw,o=r jobpaper.ms
  1229.  
  1230. echo Inspecting for damage in transit...
  1231. temp=/tmp/shar$$; dtemp=/tmp/.shar$$
  1232. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  1233. cat > $temp <<\!!!
  1234.    1200   8792  53685 jobpaper.ms
  1235. !!!
  1236. wc  jobpaper.ms | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  1237. if [ -s $dtemp ]
  1238. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  1239. else echo "No problems found."
  1240. fi
  1241. exit 0
  1242.  
  1243.  
  1244. Volume-Number: Volume 7, Number 54
  1245.  
  1246.