In this chapter, we’ll look at process groups in more detail and the concept of sessions that was introduced by POSIX.1. We’ll also look at the relationship between the login shell that is invoked for us when we login and all the processes that we start from our login shell.

1 Terminal Logins

Let’s start by looking at the programs that are executed when we login to a UNIX system.

BSD Terminal Logins
The system administrator creates a file, usually /etc/ttys, that has one line per terminal device.Each line specifies the name of the device and other parameters that are passed to the getty program. When the system is bootstrapped, the kernel creates process ID 1, the init process, and it is init that brings the system up in multiuser mode.The init process reads the file /etc/ttys and, for every terminal device that allows a login, does a fork followed by an exec of the program getty. This gives us the processes shown in Figure 9.1.

It is getty that calls open for the terminal device. The terminal is opened for reading and writing.Once the device is open, file descriptors 0, 1, and 2 are set to the device. Then getty outputs something like login: and waits for us to enter our user name.
When we enter our user name, getty’s job is complete, and it then invokes the login program, similar to:

1
execle("/bin/login", "login", "-p", username, (char *)0, envp);

Figure 9.2 shows the state of these processes right after login has been invoked.

2 Network Logins

The main (physical) difference between logging in to a system through a serial terminal and logging in to a system through a network is that the connection between the terminal and the computer isn’t point-to-point.

3 Process Groups

In addition to having a process ID, each process belongs to a process group.

A process group is a collection of one or more processes, usually associated with the same job, that can receive signals from the same terminal. Each process group has a unique process group ID. Process group IDs are similar to process IDs: they are positive integers and can be stored in a pid_t data type.

Each process group can have a process group leader. The leader is identified by its process group ID being equal to its process ID.

It is possible for a process group leader to create a process group, create processes in the group, and then terminate.

A process joins an existing process group or creates a new process group by calling setpgid.

4 Sessions

A session is a collection of one or more process groups. For example, we could have the arrangement shown in Figure 9.6. Here we have three process groups in a single session.

The processes in a process group are usually placed there by a shell pipeline. For example, the arrangement shown in Figure 9.6 could have been generated by shell commands of the form
proc1 | proc2 & proc3 | proc4 | proc5

A process establishes a new session by calling the setsid function.
If the calling process is not a process group leader, this function creates a new session. Three things happen.

  1. The process becomes the session leader of this new session. (A session leader is the process that creates a session.) The process is the only process in this new session.
  2. The process becomes the process group leader of a new process group. The new process group ID is the process ID of the calling process.
  3. The process has no controlling terminal. If the process had a controlling terminal before calling setsid, that association is broken.

5 Controlling Terminal

Sessions and process groups have a few other characteristics.

  • A session can have a single controlling terminal. This is usually the terminal device (in the case of a terminal login) or pseudo terminal device (in the case of a network login) on which we login.
  • The session leader that establishes the connection to the controlling terminal is called the controlling process.
  • The process groups within a session can be divided into a single foreground process group and one or more background process groups.
  • If a session has a controlling terminal, it has a single foreground process group and all other process groups in the session are background process groups.
  • Whenever we press the terminal’s interrupt key (often Control-C), the interrupt signal is sent to all processes in the foreground process group.
  • Whenever we press the terminal’s quit key (often Control-backslash), the quit signal is sent to all processes in the foreground process group.
  • If a modem (or network) disconnect is detected by the terminal interface, the hang-up signal is sent to the controlling process (the session leader).

These characteristics are shown in Figure 9.7.

6 tcgetpgrp, tcsetpgrp, and tcgetsid Functions

We need a way to tell the kernel which process group is the foreground process group, so that the terminal device driver knows where to send the terminal input and the terminal-generated signals (Figure 9.7).

7 Job Control

Job control allows us to start multiple jobs (groups of processes) from a single terminal and to control which jobs can access the terminal and which jobs are run in the background.

8 Shell Execution of Programs

Let’s examine how the shells execute programs and how this relates to the concepts of process groups, controlling terminals, and sessions.

9 Orphaned Process Groups

We’ve mentioned that a process whose parent terminates is called an orphan and is inherited by the init process. We now look at entire process groups that can be orphaned and see how POSIX.1 handles this situation.