<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:DengXian;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@DengXian";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:428703451;
        mso-list-type:hybrid;
        mso-list-template-ids:-1087455980 -1 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1
        {mso-list-id:737825582;
        mso-list-type:hybrid;
        mso-list-template-ids:-1307827172 -1 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l1:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l2
        {mso-list-id:1051616600;
        mso-list-type:hybrid;
        mso-list-template-ids:1142557354 -1 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l2:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l2:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l2:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l2:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l2:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l2:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l2:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l2:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l2:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l3
        {mso-list-id:2067604730;
        mso-list-type:hybrid;
        mso-list-template-ids:1822867258 -1 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l3:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l3:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l3:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l3:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l3:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l3:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l3:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l3:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l3:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Thank you, professor</p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">To clarify, </p>
<ol style="margin-top:0in" start="1" type="1">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l3 level1 lfo4">The “sh” in my processes is the root shell, because I run su(8). I tried not to run su(8), “sh” disappeared. This is a fork-exec.</li></ol>
<p class="MsoListParagraph">apue$ ps</p>
<p class="MsoListParagraph">PID TTY&nbsp;&nbsp; STAT&nbsp;&nbsp;&nbsp; TIME COMMAND</p>
<p class="MsoListParagraph">534 pts/0 O&#43;&nbsp;&nbsp; 0:00.00 ps</p>
<p class="MsoListParagraph">973 pts/0 Ss&nbsp;&nbsp; 0:00.00 -sh</p>
<p class="MsoListParagraph"><o:p>&nbsp;</o:p></p>
<ol style="margin-top:0in" start="2" type="1">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l3 level1 lfo4">For the long pipeline example, I tried to only run the first command separately, and I got a result like this:</li></ol>
<p class="MsoListParagraph">apue$ ( ps -o pid,ppid,pgid,sid,comm; sleep 1; )</p>
<p class="MsoListParagraph">PID PPID PGID SID COMMAND</p>
<p class="MsoListParagraph">973&nbsp; 724&nbsp; 973 973 -sh</p>
<p class="MsoListParagraph">509&nbsp; 973&nbsp; 509 973 -sh</p>
<p class="MsoListParagraph">922&nbsp; 509&nbsp; 509 973 ps</p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">My question is in the point 2 above why the result is not like the long pipe example which has 3 -sh?</p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Yuxi Wang</p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<div style="mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:jschauma@stevens.edu">Jan Schaumann</a><br>
<b>Sent: </b>Friday, December 13, 2019 11:03 AM<br>
<b>To: </b><a href="mailto:cs631apue@lists.stevens.edu">cs631apue@lists.stevens.edu</a><br>
<b>Subject: </b>Re: [cs631apue] About login sh process</p>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Yuxi Wang &lt;ywang286@stevens.edu&gt; wrote:<br>
<br>
&gt; 1.It seems the login shell will fork a process named ?sh?, and then this ?sh? forks to run the command.<br>
<br>
That depends on the system / shell.&nbsp; The shell can<br>
implement the processing of commands and pipelines<br>
differently: it may choose to fork-exec the process<br>
directly or fork-fork-exec.<br>
<br>
Slide 31 of Lecture 07 illustrates the sequence of<br>
direct fork-exec for each process; what you're seeing<br>
is the fork-fork-exec where each command is run in a<br>
separate fork.<br>
<br>
Try to run the same command with increasingly longer<br>
pipelines to see whether your shell creates a new<br>
shell for each command or only for the pipeline.&nbsp; For<br>
example, NetBSD's /bin/sh and /bin/ksh both only<br>
create one extra shell for the entire pipeline:<br>
<br>
$ ( ps -o pid,ppid,pgid,sid,comm; sleep 1; ) | ./cat1 | ./cat2 | more | less | cat<br>
&nbsp;PID PPID PGID SID COMMAND<br>
&nbsp; 80&nbsp;&nbsp; 81&nbsp;&nbsp; 80&nbsp; 80 -sh <br>
&nbsp;386&nbsp;&nbsp; 80&nbsp; 386&nbsp; 80 -sh <br>
&nbsp;468&nbsp;&nbsp; 80&nbsp; 386&nbsp; 80 ./cat1 <br>
&nbsp;680&nbsp;&nbsp; 80&nbsp; 386&nbsp; 80 more <br>
&nbsp;683&nbsp; 386&nbsp; 386&nbsp; 80 -sh <br>
&nbsp;774&nbsp;&nbsp; 80&nbsp; 386&nbsp; 80 less <br>
1388&nbsp;&nbsp; 80&nbsp; 386&nbsp; 80 ./cat2 <br>
1404&nbsp;&nbsp; 80&nbsp; 386&nbsp; 80 cat <br>
1470&nbsp; 683&nbsp; 386&nbsp; 80 ps <br>
<br>
The current shell we're initiating the pipeline above<br>
from is PID 80.&nbsp; That process reads the entire line<br>
entered and:<br>
<br>
- forks a new process (PID 386) for the first command of the<br>
&nbsp; pipeline<br>
&nbsp; - the first command is a subshell &quot;( ps ...)&quot;, so<br>
&nbsp;&nbsp;&nbsp; PID 386 forks a new shell (PID 683)<br>
&nbsp; - the subshell (PID 683) forks a process (PID 1470)<br>
&nbsp;&nbsp;&nbsp; for execution of the ps(1) command<br>
&nbsp; - the 'sleep' command is a built-in and executes<br>
&nbsp;&nbsp;&nbsp; within PID 683 after 1470 completes<br>
- forks a new process (PID 468) for the second command (cat1)<br>
- forks a new process (PID 1388) for the third command (cat2)<br>
- forks a new process (PID 680) for the fourth command (more)<br>
- forks a new process (PID 774) for the fifth command (less)<br>
- forks a new process (PID 1404) for the sixth command (cat)<br>
<br>
<br>
(We use the subshell to add the sleep so that the<br>
ps(1) command will see the actual command names; if<br>
you leave out the subshell and the sleep you will only<br>
see '-sh' processes.)<br>
<br>
<br>
&gt; 2.The shell process we used to type in the command should be the ?sh? not ?-sh?.<br>
<br>
In the example above, all shells are '-sh', because<br>
our initial shell is a login shell.&nbsp; (A login shell<br>
differs from a regular shell in what startup files it<br>
reads and how it's initial environment is set up;<br>
this is usually done by the login mechanism for you,<br>
but you also generally invoke any shell as a login<br>
shell via a command-line option, commonly '-l'.)<br>
<br>
Any fork of a login shell is also a login shell, since<br>
by the semantics of fork(2) the parent and child are<br>
(mostly) identical.<br>
<br>
In your example, you appear to be logged in via ssh as<br>
'g_cream'; that user appears to have run e.g. su(8),<br>
but invoked without '-l', leading to a root shell<br>
(PID 811) in which root ran the command 'ps -ajx'.<br>
<br>
User 'g_cream' is also logged in on the console (PID<br>
40 via 755), so also gets a login shell there.<br>
<br>
-Jan<br>
_______________________________________________<br>
cs631apue mailing list<br>
cs631apue@lists.stevens.edu<br>
<a href="https://lists.stevens.edu/mailman/listinfo/cs631apue">https://lists.stevens.edu/mailman/listinfo/cs631apue</a><o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</body>
</html>