Why Team Leader Can't Create Linux Session

Why is the team leader unable to create a session. but other than a group leader able to create a session?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

main() {

    int pid;
    int ppid=getppid();

    if ( setsid() < 0)
         perror("ERROR");

    if((pid=fork()) == 0)
    {
        printf("proces1=%d %d\n",getpid(),getpgrp());
        int s=getpgrp();

        //setpgid(pid,pid);
        if (setpgid(pid,0) < 0)
            perror("ERROR");

        printf("group after proces=%d %d\n",getpid(),getpgrp());
        exit(0);
    }

    wait(0);
    printf("group after proces=%d %d\n",getpid(),getpgrp());       
}                   

      

Explain, please.

+3


source to share


1 answer


Short answer

A ban setsid()

in the leader of a process group is required by POSIX:

The function setsid()

should create a new session if the calling process is not the leader of the process group.

Requires all members of a process group to be members of the same session.

Long answer

Process group ID is the ID of the process group. Session ID is the ID of the session manager. After a successful call setsid()

, the process group ID, session ID, and PID must be the same.

However, for the process group leader, the process group ID is already PID. If it can call setsid()

, the process group ID will remain the same, so:

  • the leader of the process group belongs to the new session;
  • other members of the process group belong to the old session.

So in this case we have a process group with members belonging to different sessions. POSIX wants to disallow this situation.



Why?

Process groups and sessions were invented for job management. Process groups are used to define foreground and background groups so that the foreground group will receive signals from the terminal.

To do this, the terminal keeps track of its current foreground process group and sends a signal to this group when an event occurs.

But this assumes that all processes from any given process group have the same control terminal, so that the signals sent by the terminal make sense to them.

The control terminal is divided by the following rules:

  • all processes from one session have the same control terminal;
  • processes from different sessions cannot use the same controlling terminal.

Thus, if we want all members of a process group to have the same controlling terminal, we must also require them to be members of the same session.

Link

See Linux Programming Interface, Chapter 34 (Process Groups, Sessions, and Job Management).

+3


source







All Articles