Process Relationships

Besides parent-child relatonship, there are also process group, sessions relation between process.

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.

getpgid

#include <unistd.h>


pid_t getpgrp(void);  //Returns: process group ID of calling process
pid_t getpgid(pid_t pid);  //Returns: process group ID if OK, −1 on error

// getpgid(0) ==  getpgrp();

setpgid

This function sets the process group ID to pgid in the process whose process ID equals pid .

#include <unistd.h>


int setpgid(pid_t pid, pid_t pgid);   //Returns: 0 if OK, −1 on error
  • If the two arguments are equal, the process specified by pid becomes a process group leader

  • If pid is 0, the process id of the caller is used.

  • If the gpid is 0, the process ID specified by pid is used as the process group ID.

  • A process can set the process group ID of only itself or any of its children. Furthermore, it can’t change the process group ID of one of its children after that child has called one of the exec functions.

Even the process group leader has been the exit, the group still be valid as long as one of processes alive.

Sessions

A session is a collection of one or more process groups.

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 could have been generated by shell commands of the form

proc1 | proc2 & proc3 | proc4 | proc5

setsid

#include <unistd.h>


pid_t setsid(void);      // Returns: process group ID if OK, −1 on error

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

  • 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.

  • 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.

  • The process has no controlling terminal. If the has a controlling terminal before calling setsid, the association is broken.

If the caller is already a process group leader, this function returns an error.

To ensure this is not the case, the usual practice is to call fork and have the parent terminate and the child continue. We are guaranteed that the child is not a process group leader, because the process group ID of the parent is inherited by the child, but the child gets a new process ID. Hence, it is impossible for the child’s process ID to equal its inherited process group ID. (In fact, we can see this implementation in most daemon() function.)

We next use two snippets to illustrate these characters.

Call setsid() in child process.

#include "apue.h"

int
main() {
    pid_t pid;

    pid = fork();

    if (pid < 0) {
        err_sys("fork failed");
        exit(1);
    }

    if (pid == 0) {
        printf("child process running\n");
        pid = setsid();
        if (pid < 0) {
        } else {
            printf("setsid success\n");

        }
        sleep(300);
        exit(0);
    }



    printf("parent process running\n");
    for (; ;) {
        sleep(5);
    }

    exit(0);

}

output:

parent process running

child process running

setsid success

ps xao pid,ppid,pgid,sid,tty,comm

22677 7023 22677 7023 pts/1 process //parent process

22678 22677 22678 22678 ? process // child process

From the ps output, we can see

  1. we create a new process group and a new session
  2. The child process both are the group leader and session leader.
  3. The child process does not have associate terminal.

Call setsid() in parent process.

#include "apue.h"

int
main() {
    pid_t pid;

    pid = fork();

    if (pid < 0) {
        err_sys("fork failed");
        exit(1);
    }

    if (pid == 0) {
        printf("child process running\n");
        sleep(300);
        exit(0);
    }

    printf("parent process running\n");

    pid = setsid();
    if (pid < 0) {
        printf("setsid error\n");
    } else {
        printf("setsid success\n");

    }

    for (; ;) {
        sleep(5);
    }

    exit(0)

}

output:

parent process running

setsid error

child process running

ps xao pid,ppid,pgid,sid,tty,comm

24360 7023 24360 7023 pts/1 process keqiang keqiang

24361 24360 24360 7023 pts/1 process keqiang keqiang

getsid

#include <unistd.h>


pid_t getsid(pid_t pid);      //
Returns: session leader’s process group ID if OK, −1 on error

The Single UNIX Specification talks only about a ‘‘session leader’’; there is no ‘‘session ID’’ similar to a process ID or a process group ID. Obviously, a session leader is a single process that has a unique process ID, so we could talk about a session ID that is the process ID of the session leader. This concept of a session ID was introduced in SVR4. Historically, BSD-based systems didn’t support this notion, but have since been updated to include it. The getsid function returns the process group ID of a process’s session leader.

Controlling Terminal

Sessions and process groups have a few other characteristics.

  1. 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 log in.

  2. The session leader that establishes the connection to the controlling terminal is called the controlling process .

  3. The process groups within a session can be divided into a single foreground process group and one or more background process groups .

  4. Whenever we press the terminal’s interrupt key (often DELETE or Control-C), the interrupt signal is sent to all processes in the foreground process group(all the process in this process groups will receive this signal).

  5. Whenever we press the terminal’s quit key (often Control-backslash), the quit signal is sent to all processes in the foreground process group.

  6. 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).

The relation between process, process group, session, session leader, controlling terminal.

results matching ""

    No results matching ""