Is a session leader the only process in its group?











up vote
3
down vote

favorite
1












The following diagram from APUE leads me to wonder: in a process session, does the process group of the session leader contain only the session leader and no other process?




  • Any process created by fork() will inherit the same process group and session from its parent. So can the session leader fork arbitrary number of processes into its own process group?


  • For example, is a shell (when it is a session leader) the only process in its process group? If yes, is it because when the shell forks a child process, the child is initially in the same process group as the shell, but then immediately starts a new process group by calling setpgid() ?



Thanks.



enter image description here










share|improve this question
























  • Related: unix.stackexchange.com/questions/18166/…
    – Kusalananda
    May 5 at 19:38















up vote
3
down vote

favorite
1












The following diagram from APUE leads me to wonder: in a process session, does the process group of the session leader contain only the session leader and no other process?




  • Any process created by fork() will inherit the same process group and session from its parent. So can the session leader fork arbitrary number of processes into its own process group?


  • For example, is a shell (when it is a session leader) the only process in its process group? If yes, is it because when the shell forks a child process, the child is initially in the same process group as the shell, but then immediately starts a new process group by calling setpgid() ?



Thanks.



enter image description here










share|improve this question
























  • Related: unix.stackexchange.com/questions/18166/…
    – Kusalananda
    May 5 at 19:38













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





The following diagram from APUE leads me to wonder: in a process session, does the process group of the session leader contain only the session leader and no other process?




  • Any process created by fork() will inherit the same process group and session from its parent. So can the session leader fork arbitrary number of processes into its own process group?


  • For example, is a shell (when it is a session leader) the only process in its process group? If yes, is it because when the shell forks a child process, the child is initially in the same process group as the shell, but then immediately starts a new process group by calling setpgid() ?



Thanks.



enter image description here










share|improve this question















The following diagram from APUE leads me to wonder: in a process session, does the process group of the session leader contain only the session leader and no other process?




  • Any process created by fork() will inherit the same process group and session from its parent. So can the session leader fork arbitrary number of processes into its own process group?


  • For example, is a shell (when it is a session leader) the only process in its process group? If yes, is it because when the shell forks a child process, the child is initially in the same process group as the shell, but then immediately starts a new process group by calling setpgid() ?



Thanks.



enter image description here







linux shell process session






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 14:07

























asked May 5 at 19:29









Tim

25.3k72243446




25.3k72243446












  • Related: unix.stackexchange.com/questions/18166/…
    – Kusalananda
    May 5 at 19:38


















  • Related: unix.stackexchange.com/questions/18166/…
    – Kusalananda
    May 5 at 19:38
















Related: unix.stackexchange.com/questions/18166/…
– Kusalananda
May 5 at 19:38




Related: unix.stackexchange.com/questions/18166/…
– Kusalananda
May 5 at 19:38










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










No, there's no such restriction. If it was the case, commands that don't implement job control (in practice, only shells do) wouldn't be able to fork a process (as child processes inherit the process group) when started as xterm -e that-command for instance.



Even when the session leader is an interactive shell with job control enabled, you can have other processes in its group.



Running:



xterm -e 'sleep 1000 & exec zsh'


And in that xterm:



  PID  PGID   SID TTY          TIME CMD
14003 14003 14003 pts/20 00:00:00 zsh
14004 14003 14003 pts/20 00:00:00 sleep
14012 14012 14003 pts/20 00:00:00 ps


Most commands run from an interactive shell are run in separate process groups, but it's not the case for all.



For instance, in bash:



$ exec 3< <(sleep 1000)
$ ps -j
PID PGID SID TTY TIME CMD
13913 13913 13913 pts/19 00:00:00 bash
14136 13913 13913 pts/19 00:00:00 bash
14137 13913 13913 pts/19 00:00:00 sleep
14138 14138 13913 pts/19 00:00:00 ps


Or the processes started as part of prompt expansions:



$ PS1=$'$(ps -j)n$ '
PID PGID SID TTY TIME CMD
14212 14212 14212 pts/18 00:00:00 bash
14292 14212 14212 pts/18 00:00:00 ps
$





share|improve this answer





















  • Thanks. Do you happen to know why bash makes some child processes into new process groups, but not some other child processes?
    – Tim
    Dec 4 at 14:26




















up vote
1
down vote













All external commands are run in a process group different from the one of the shell. The shell is a session leader and thus that is the only process in its process group.



All commands which belong to the same pipeline are in the same process group.






share|improve this answer























  • Thanks. Is it correct that proc1 and proc2 may run external commands, but why are they in the same group in the diagram, contradicting to "All external commands are run in a different process group" ?
    – Tim
    May 6 at 14:48












  • @Tim Probably a misunderstanding. I have improved the wording.
    – Hauke Laging
    May 6 at 15:00










  • Thanks. I have updated my post. Hope that I clarify my problem.
    – Tim
    Dec 4 at 12:56













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441999%2fis-a-session-leader-the-only-process-in-its-group%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










No, there's no such restriction. If it was the case, commands that don't implement job control (in practice, only shells do) wouldn't be able to fork a process (as child processes inherit the process group) when started as xterm -e that-command for instance.



Even when the session leader is an interactive shell with job control enabled, you can have other processes in its group.



Running:



xterm -e 'sleep 1000 & exec zsh'


And in that xterm:



  PID  PGID   SID TTY          TIME CMD
14003 14003 14003 pts/20 00:00:00 zsh
14004 14003 14003 pts/20 00:00:00 sleep
14012 14012 14003 pts/20 00:00:00 ps


Most commands run from an interactive shell are run in separate process groups, but it's not the case for all.



For instance, in bash:



$ exec 3< <(sleep 1000)
$ ps -j
PID PGID SID TTY TIME CMD
13913 13913 13913 pts/19 00:00:00 bash
14136 13913 13913 pts/19 00:00:00 bash
14137 13913 13913 pts/19 00:00:00 sleep
14138 14138 13913 pts/19 00:00:00 ps


Or the processes started as part of prompt expansions:



$ PS1=$'$(ps -j)n$ '
PID PGID SID TTY TIME CMD
14212 14212 14212 pts/18 00:00:00 bash
14292 14212 14212 pts/18 00:00:00 ps
$





share|improve this answer





















  • Thanks. Do you happen to know why bash makes some child processes into new process groups, but not some other child processes?
    – Tim
    Dec 4 at 14:26

















up vote
1
down vote



accepted










No, there's no such restriction. If it was the case, commands that don't implement job control (in practice, only shells do) wouldn't be able to fork a process (as child processes inherit the process group) when started as xterm -e that-command for instance.



Even when the session leader is an interactive shell with job control enabled, you can have other processes in its group.



Running:



xterm -e 'sleep 1000 & exec zsh'


And in that xterm:



  PID  PGID   SID TTY          TIME CMD
14003 14003 14003 pts/20 00:00:00 zsh
14004 14003 14003 pts/20 00:00:00 sleep
14012 14012 14003 pts/20 00:00:00 ps


Most commands run from an interactive shell are run in separate process groups, but it's not the case for all.



For instance, in bash:



$ exec 3< <(sleep 1000)
$ ps -j
PID PGID SID TTY TIME CMD
13913 13913 13913 pts/19 00:00:00 bash
14136 13913 13913 pts/19 00:00:00 bash
14137 13913 13913 pts/19 00:00:00 sleep
14138 14138 13913 pts/19 00:00:00 ps


Or the processes started as part of prompt expansions:



$ PS1=$'$(ps -j)n$ '
PID PGID SID TTY TIME CMD
14212 14212 14212 pts/18 00:00:00 bash
14292 14212 14212 pts/18 00:00:00 ps
$





share|improve this answer





















  • Thanks. Do you happen to know why bash makes some child processes into new process groups, but not some other child processes?
    – Tim
    Dec 4 at 14:26















up vote
1
down vote



accepted







up vote
1
down vote



accepted






No, there's no such restriction. If it was the case, commands that don't implement job control (in practice, only shells do) wouldn't be able to fork a process (as child processes inherit the process group) when started as xterm -e that-command for instance.



Even when the session leader is an interactive shell with job control enabled, you can have other processes in its group.



Running:



xterm -e 'sleep 1000 & exec zsh'


And in that xterm:



  PID  PGID   SID TTY          TIME CMD
14003 14003 14003 pts/20 00:00:00 zsh
14004 14003 14003 pts/20 00:00:00 sleep
14012 14012 14003 pts/20 00:00:00 ps


Most commands run from an interactive shell are run in separate process groups, but it's not the case for all.



For instance, in bash:



$ exec 3< <(sleep 1000)
$ ps -j
PID PGID SID TTY TIME CMD
13913 13913 13913 pts/19 00:00:00 bash
14136 13913 13913 pts/19 00:00:00 bash
14137 13913 13913 pts/19 00:00:00 sleep
14138 14138 13913 pts/19 00:00:00 ps


Or the processes started as part of prompt expansions:



$ PS1=$'$(ps -j)n$ '
PID PGID SID TTY TIME CMD
14212 14212 14212 pts/18 00:00:00 bash
14292 14212 14212 pts/18 00:00:00 ps
$





share|improve this answer












No, there's no such restriction. If it was the case, commands that don't implement job control (in practice, only shells do) wouldn't be able to fork a process (as child processes inherit the process group) when started as xterm -e that-command for instance.



Even when the session leader is an interactive shell with job control enabled, you can have other processes in its group.



Running:



xterm -e 'sleep 1000 & exec zsh'


And in that xterm:



  PID  PGID   SID TTY          TIME CMD
14003 14003 14003 pts/20 00:00:00 zsh
14004 14003 14003 pts/20 00:00:00 sleep
14012 14012 14003 pts/20 00:00:00 ps


Most commands run from an interactive shell are run in separate process groups, but it's not the case for all.



For instance, in bash:



$ exec 3< <(sleep 1000)
$ ps -j
PID PGID SID TTY TIME CMD
13913 13913 13913 pts/19 00:00:00 bash
14136 13913 13913 pts/19 00:00:00 bash
14137 13913 13913 pts/19 00:00:00 sleep
14138 14138 13913 pts/19 00:00:00 ps


Or the processes started as part of prompt expansions:



$ PS1=$'$(ps -j)n$ '
PID PGID SID TTY TIME CMD
14212 14212 14212 pts/18 00:00:00 bash
14292 14212 14212 pts/18 00:00:00 ps
$






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 4 at 14:01









Stéphane Chazelas

296k54559904




296k54559904












  • Thanks. Do you happen to know why bash makes some child processes into new process groups, but not some other child processes?
    – Tim
    Dec 4 at 14:26




















  • Thanks. Do you happen to know why bash makes some child processes into new process groups, but not some other child processes?
    – Tim
    Dec 4 at 14:26


















Thanks. Do you happen to know why bash makes some child processes into new process groups, but not some other child processes?
– Tim
Dec 4 at 14:26






Thanks. Do you happen to know why bash makes some child processes into new process groups, but not some other child processes?
– Tim
Dec 4 at 14:26














up vote
1
down vote













All external commands are run in a process group different from the one of the shell. The shell is a session leader and thus that is the only process in its process group.



All commands which belong to the same pipeline are in the same process group.






share|improve this answer























  • Thanks. Is it correct that proc1 and proc2 may run external commands, but why are they in the same group in the diagram, contradicting to "All external commands are run in a different process group" ?
    – Tim
    May 6 at 14:48












  • @Tim Probably a misunderstanding. I have improved the wording.
    – Hauke Laging
    May 6 at 15:00










  • Thanks. I have updated my post. Hope that I clarify my problem.
    – Tim
    Dec 4 at 12:56

















up vote
1
down vote













All external commands are run in a process group different from the one of the shell. The shell is a session leader and thus that is the only process in its process group.



All commands which belong to the same pipeline are in the same process group.






share|improve this answer























  • Thanks. Is it correct that proc1 and proc2 may run external commands, but why are they in the same group in the diagram, contradicting to "All external commands are run in a different process group" ?
    – Tim
    May 6 at 14:48












  • @Tim Probably a misunderstanding. I have improved the wording.
    – Hauke Laging
    May 6 at 15:00










  • Thanks. I have updated my post. Hope that I clarify my problem.
    – Tim
    Dec 4 at 12:56















up vote
1
down vote










up vote
1
down vote









All external commands are run in a process group different from the one of the shell. The shell is a session leader and thus that is the only process in its process group.



All commands which belong to the same pipeline are in the same process group.






share|improve this answer














All external commands are run in a process group different from the one of the shell. The shell is a session leader and thus that is the only process in its process group.



All commands which belong to the same pipeline are in the same process group.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 6 at 15:00

























answered May 6 at 14:04









Hauke Laging

55.4k1284132




55.4k1284132












  • Thanks. Is it correct that proc1 and proc2 may run external commands, but why are they in the same group in the diagram, contradicting to "All external commands are run in a different process group" ?
    – Tim
    May 6 at 14:48












  • @Tim Probably a misunderstanding. I have improved the wording.
    – Hauke Laging
    May 6 at 15:00










  • Thanks. I have updated my post. Hope that I clarify my problem.
    – Tim
    Dec 4 at 12:56




















  • Thanks. Is it correct that proc1 and proc2 may run external commands, but why are they in the same group in the diagram, contradicting to "All external commands are run in a different process group" ?
    – Tim
    May 6 at 14:48












  • @Tim Probably a misunderstanding. I have improved the wording.
    – Hauke Laging
    May 6 at 15:00










  • Thanks. I have updated my post. Hope that I clarify my problem.
    – Tim
    Dec 4 at 12:56


















Thanks. Is it correct that proc1 and proc2 may run external commands, but why are they in the same group in the diagram, contradicting to "All external commands are run in a different process group" ?
– Tim
May 6 at 14:48






Thanks. Is it correct that proc1 and proc2 may run external commands, but why are they in the same group in the diagram, contradicting to "All external commands are run in a different process group" ?
– Tim
May 6 at 14:48














@Tim Probably a misunderstanding. I have improved the wording.
– Hauke Laging
May 6 at 15:00




@Tim Probably a misunderstanding. I have improved the wording.
– Hauke Laging
May 6 at 15:00












Thanks. I have updated my post. Hope that I clarify my problem.
– Tim
Dec 4 at 12:56






Thanks. I have updated my post. Hope that I clarify my problem.
– Tim
Dec 4 at 12:56




















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441999%2fis-a-session-leader-the-only-process-in-its-group%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Accessing regular linux commands in Huawei's Dopra Linux

Can't connect RFCOMM socket: Host is down

Kernel panic - not syncing: Fatal Exception in Interrupt