106

What are session leaders, as in ps -d which selects all processes except session leaders?

3 Answers 3

111

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.

4
  • 9
    Note: Use ps xao pid,ppid,pgid,sid,comm to view these IDs.
    – Mike R
    Commented Aug 8, 2014 at 13:27
  • 4
    Why don't people give more of such illustrative real world comparison based answers.. +1 Commented Dec 13, 2014 at 16:23
  • Hmm.. if you run the two pipelines say sleep 1 | sleep 2 & sleep 3 | sleep 4 & directly from SID process, they have different process group ids, but if you run them from subshell, they both have same process group id. It is confusing.
    – jarno
    Commented Mar 20, 2020 at 0:12
  • 1
    To clarify: is it true that the process SID has nothing to do with users and user sessions? Commented Dec 5, 2022 at 1:32
28

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, Libre Office, IM client, etc. These are the processes that are not session leaders.

7
  • Please don't mind but I might need a little more clarification - - session leader is one, what are the others called, and what are they like (behavior, what do they do different from Session Leader)?
    – its_me
    Commented Aug 6, 2011 at 3:26
  • They are called member of the session, I believe.
    – Arcege
    Commented Aug 6, 2011 at 18:48
  • I like your explanation but I'm still missing one point: IIUC, at any time every process I started is actually child of the shell I logged in with (unless I disowned it, of course). Why wouldn't OS just walk the process tree and kill all siblings of this process and their siblings? (Actually that's what I always thought it does...until today :D) So what is the rationale behind using sessions instead? Commented May 13, 2012 at 21:23
  • There needs to be a away of determining when a process is no longer part of the original session (login shell or daemon, for example). One thought might be to automatically change the PPID (parent pid) to 1, but that does break the process tree. A new session id creates a group that could be sent a signal together. An example of this is the GUI, fire off firefox as a separate session. Then, when the [X] button is pressed, send a signal the firefox's session and its children, but the window manager is not affected - couldn't do this with straight PPID-PID relationships.
    – Arcege
    Commented May 14, 2012 at 0:18
  • 3
    I think that this answer may be confusing sessions and process groups, as kill -123 is used to send a signal to a process group; I don't believe that kill can send signals to sessions. Following on, if I'm reading this answer correctly, $$ gets the PID of the shell, in which case kill -$$ would send a signal to the shell's process group, not the shell's session.
    – eZanmoto
    Commented Oct 10, 2020 at 9:57
16

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.

3
  • 2
    Being a linux newbie, I couldn't make out what you said. Looks like you are puzzled too? But you are probably knowledgeable enough to understand what Arcege's answer meant. If you do, can you please explain the same in more clarity?
    – its_me
    Commented Aug 6, 2011 at 3:30
  • Interesting... thanks... So, the session id (SID) is the terminal's PID for ./sid and nohup ./sid, and when you run setsid ./sid, the session id (SID) is brand new and is the same as the process PID... I'm not sure why nohup prevented the fork (or seems to), but I think I've got the general idea...
    – Peter.O
    Commented Aug 6, 2011 at 10:02
  • 1
    @Peter.O the nohup ran the forked process. However, the script uses _exit(0) which exits immediately and doesn't make sure to flush file buffers, if it used exit(0) (no underscore), the buffers would be flushed and you would see output from the forked process. printf knows it's outputting to a terminal so it would output every line as it gets them, but in nohup the output is to a file so printf will buffer till it has a lot of text before actually writing out to a file. you might also see duplicate text in the output of the nohup since there are unflushed buffers at the time of the fork.
    – Jake
    Commented Sep 26, 2020 at 21:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .