Jay
Jay

Reputation: 1038

What is the difference between a job pid and a process id in *nix?

A job is a pipeline of processes. After I execute a command line, for example, sleep 42 & , the terminal will give me some information like this

[1] 31562

Is this 31562 the "job pid" of this job? Is it the same as the process of the ls command?

And if I have a command with a pipe, there will be more than one process created, is the job pid same as the process id of the first process of the pipeline?

Upvotes: 1

Views: 4478

Answers (1)

Filipe Gonçalves
Filipe Gonçalves

Reputation: 21213

A job is a pipeline of processes.

Not necessarily. Though most of the times a job is comprised of a processes pipeline, it can be a single command, or it can be a set of commands separated by &&. For example, this would create a job with multiple processes that are not connected by a pipeline:

cat && ps u && ls -l && pwd &

Now, with that out of the way, let's get to the interesting stuff.

Is this 31562 the "job pid" of this job? Is it the same as the process of the ls command?

The job identifier is given inside the square brackets. In this case, it's 1. That's the ID you'll use to bring it to the foreground and perform other administrative tasks. It's what identifies this job in the shell.

The number 31562 is the process group ID of the process group running the job. UNIX/Linux shells make use of process groups: a process group is a set of processes that are somehow related (often by a linear pipeline, but as mentioned before, not necessarily the case). At any moment, you can have 0 or more background process groups, and at most one foreground process group (the shell controls which groups are in the background and which is in the foreground with tcsetpgrp(3)).

A group of processes is identified by a process group ID, which is the ID of the process group leader. The process group leader is the process that first created and joined the group by calling setpgid(2). The exact process that does this depends on how the shell is implemented, but in bash, IIRC, it is the last process in the pipeline.

In any case, what the shell shows is the process group ID of the process group running the job (which, again, is really just the PID of the group leader).

Note that the group leader may have died in the past; the process group ID won't change. This means that the process group ID does not necessarily correspond to a live process.

Upvotes: 1

Related Questions