user1888243 user1888243 - 9 days ago 6
Bash Question

Linux: What is a job, and What is its Relation to Process

I've been struggling with the concept of a job in Linux, and have read at least 30 sources to get a better understanding of jobs including this one http://unix.stackexchange.com/a/4215. But non of these really clarify the concept. However, I think I finally have formed a good understanding of the concept of a job. I want to verify that my understanding is not flawed. Here's my understand:


"When user executes a shell command, a job is created by the shell.
The job then creates one or more processes which are required to
satisfy the executed command. You can think of the job as a wrapper
that contains all the necessary processes for satisfying the user's
requested command.

The reason that shell creates a job is to enable it to manage groups
of related processes which are necessary to execute a command."

Answer

This is very easy to explain and demonstrate with the output of actual commands. First we run a command that needs to create 2 processes, and then run a command which requires to create only one process:

$(tail -f index.html|grep google )&
[2] 4344

Now, execute the following command:

$ps

  PID TTY          TIME CMD
 4344 pts/18   00:00:00 bash
 4345 pts/18   00:00:00 tail
 4346 pts/18   00:00:00 grep
 4350 pts/18   00:00:00 ps

As you can see, the pid reported for the job is 4344, but the pid for the tail and grep are respectively 4345, 4346. This clearly shows that a parent process with pid of 4344 was created when you run the command, and this parent process has spawned 2 child process 4345, and 4346.

But now, execute the following command:

$tail -f index.html&
[2] 5968

Now, execute the following command:

$ps

  PID TTY          TIME CMD
 5968 pts/18   00:00:00 tail
 5971 pts/18   00:00:00 ps
 8319 pts/18   00:00:00 bash
25281 pts/18   00:00:00 ssh

The above examples show that the job itself is not a process and does not have a pid, but is just a wrapper around the processes which are needed to execute the command. In other words, your understanding is quite correct.