0

I'm reading a textbook which discusses process group and shell and it says:

Unix shells use the abstraction of a job to represent the processes that are created as a result of evaluating a single command line. At any point in time, there is at most one foreground job and zero or more background jobs. For example, typing linux> ls | sort creates a foreground job consisting of two processes connected by a Unix pipe: one running the ls program, the other running the sort program. The shell creates a separate process group for each job.

and below is a picture: enter image description here

You can see that the foreground job has process id(pid) process group id(pgid) and they are both 20

My questions are:

Q1-I was told that a process group can be considered as a job, but how come the foreground job in the picture has a process id? as my understanding is, a process is an instance of program in executation, and a process group consists of its member processes, so process group is pretty much an abstraction concept, then how can a concept be an instance of program in executation?

Q2-I was also told that: by convention, the process group ID of a process group equals the process ID of the first member of the process group. so in the picture the pgid of the foreground job should be 21 since the pid of its first member(child process) is 21?

1
  • 1
    Q2 isn't a question. You don't get a question just by changing a full stop into a question mark. Ironically, what you labelled "Q2" is part of the answer to the actual questions in Q1 ("how come the … ?" "how can a concept be … ?"). To make a question of Q2 you need to reverse the order of subject and verb in the bit that you want to ask. – JdeBP Sep 11 '20 at 6:51
3

In the picture, the process group concept is represented by the dahsed squares, not the circles. The circles all represent processes, and the “jobs” are the first process in each group.

See How is a process group ID set? for details.

4
  • thanks for your answer. So is a job also a process?Can I say the job in the picture is the first process in the process group? – secondimage Sep 11 '20 at 6:15
  • 1
    Yes, the job in the picture is the first process in the process group. Try running (sleep 10 & sleep 10 & sleep 10) & and then pstree -p $$ to see an example; there will be a single job with four processes (a shell and the three sleeps), and the job will be identified by its first process (the shell). – Stephen Kitt Sep 11 '20 at 6:55
  • thanks. Last question, we know that init process is the first process in the beginning , the shell process is a child of it. Does it mean that init process create a process group whose Id is 10, then the shell just inhertits this Id as process group, that's why the pgid of the shell is 10? – secondimage Sep 11 '20 at 8:15
  • init’s in pgid 1, not 10. Shells create their own process groups, see this answer for details; in the picture, the shell’s pgid is 10 because its pid is 10. – Stephen Kitt Sep 11 '20 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.