What are "session leaders" in `ps`?
In Linux, every process has several IDs associated with it, including:
Process ID (PID)
This is an arbitrary number identifying the process. Every process has a unique ID, but after the process exits and the parent process has retrieved the exit status, the process ID is freed to be reused by a new process.Parent Process ID (PPID)
This is just the PID of the process that started the process in question. If the parent process exits before the child does, the child's PPID is changed to another process (usually PID 1).Process Group ID (PGID)
This is just the PID of the process group leader. If PID == PGID, then this process is a process group leader.Session ID (SID)
This is just the PID of the session leader. If PID == SID, then this process is a session leader.
Sessions and process groups are just ways to treat a number of related processes as a unit. All the members of a process group always belong to the same session, but a session may have multiple process groups.
Normally, a shell will be a session leader, and every pipeline executed by that shell will be a process group. This is to make it easy to kill the children of a shell when it exits. (See exit(3) for the gory details.)
I don't think there is a special term for a member of a session or process group that isn't the leader.
A session leader is a process where session id == process id. This sounds contrived, but the session id is inherited by child processes. Some operations within UNIX/Linux operate on process sessions, for example, negating the process id when sending to the kill system call or command. The most common use for this is when logging out of a shell. The OS will send kill -HUP -$$
, which will send a SIGHUP (hangup) signal to all the processes with the same session id as the shell. When you disown a process, the session id of the process is changed from the shell, so it will not respond to the hangup signal. This is one part of the process to become a daemon process.
Most of the processes called from the window manager/graphical environment have the same session id as one of the startup programs. This allows the OS to perform the same kill -HUP -$$
operation on all the programs: such as your browser, music player, libreoffice, IM client, etc. These are the processes that are not session leaders.
I thought I knew the answer to this, but I wrote a C program to figure this out.
#include <stdio.h>
#include <unistd.h>
int
main(int ac, char **av)
{
pid_t sid, mypid, pgid, gid;
mypid = getpid();
sid = getsid(0);
pgid = getpgid(0);
gid = getpgrp();
printf("PID %d\n", mypid);
printf("process group ID of session leader: %d\n", sid);
printf("process group ID: %d\n", pgid);
printf("process group ID: %d\n", gid);
if (!fork())
{
mypid = getpid();
sid = getsid(0);
pgid = getpgid(0);
gid = getpgrp();
printf("child PID %d\n", mypid);
printf("process group ID of session leader: %d\n", sid);
printf("process group ID: %d\n", pgid);
printf("process group ID: %d\n", gid);
_exit(0);
}
return 0;
}
I compiled it with cc -g -o sid sid.c
I ran it a few different ways, to see what happens:
./sid
nohup ./sid > sid.out
setsid ./sid
I was kind of surprised by what Linux (2.6.39) gave back. I also found the section 7 man page, "credentials".
My advice is to do man 7 credentials
(or the equivalent if not on Linux), and read the section about process group and session to see if you can puzzle it out.