2

In this video, it is said that:

In the normal arrangement, if you open a terminal, and inside you have a shell running, that shell represents the start of a new session. That session starts with one job, containing the shell process. When the shell runs a pipeline in the foreground, the processes of that pipeline runs as processes of the existing job.

Whereas in the famous The TTY demystified, it seems that when a shell runs a pipeline in the foreground, it is part of a new job. What's correct?

enter image description here

1
  • As noted in one of the answers the main confusion here seems the definition of a job. Here the pipeline of processes is called a job. So running a pipeline starts a new job. The job can run immediately attached to the main shell's process. So both are correct. Nov 13, 2021 at 16:01

2 Answers 2

1

It depends on what you mean by "program".

First, the excerpt from the video is misleading when it says "That session starts with one job, containing the shell process." As explained in What is the difference between a job and a process? and also as implied by the POSIX definition of job, a job is a shell concept. So a running shell is not itself a job unless it is part of a job from a parent shell process. In the excerpt, the parent process is the terminal which, as explained in the The Bash Guide, is just a graphical interface program used to run text-based interface programs which aren't necessarily shells.

Here are some commands to illustrate that the table from The TTY demystified is accurate:

$ cat
hello
hello
^Z
[1]+  Stopped                 cat
$ cat | sort
^Z
$ jobs
[1]-  Stopped                 cat
[2]+  Stopped                 cat | sort

You can see in the last output that two jobs exist: cat and cat | sort, the later being equivalent to the ls | sort from the table.

As to whether "a program run by bash runs in a new job", POSIX defines:

Job

A set of processes, comprising a shell pipeline, and any processes descended from it, that are all in the same process group.

So if you consider that "a set of processes, comprising a shell pipeline" is a running program (which you can on the basis that a program is a set of instruction to be executed), then yes.

But since "program" is a more general concept than just "shell pipelines", then it is not generally true that programs run by bash run in a new job. For example cat and sort are also programs but they don't each run in a separate job.

Also, shell builtins are "programs" in the sense that they are "set of instructions". But they are embedded components of the shell program itself and so they do not necessarily need the creation of separate child processes to be executed. In circumstances where they are not executed in separate child processes they cannot be part of a job for the shell.

1
  • I'd love a comment on why this scored a down vote. Nov 13, 2021 at 16:01
-1

Yes it does. You can spawn it off your shell by typing bg after your ^Z and it will continue to run under it's own PID.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .