why group leader cannnot able create the session in Linux

来源:互联网 发布:nginx 静态文件服务器 编辑:程序博客网 时间:2024/06/16 01:52

Forbidding setsid() in a process group leader is required by POSIX:

The setsid() function shall create a new session, if the calling process is not a process group leader.

It is required to ensure that all members of a process group are members of the same session.

Long answer

Process group ID is PID of process group leader. Session ID is PID of session leader. After successful setsid() call, process group ID, session ID and PID should be the same.

However, for the process group leader, process group ID is already equal to PID. If it would be able to call setsid(), it's process group ID remains the same, so that:

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

Thus, in this case we have a process group with members belonging to different sessions. POSIX wants to forbid this situation.

Why?

Process groups and sessions were invented for job control. Process groups are used to determine foreground and background groups, so that foreground group will receive signals from terminal.

To implement this, terminal tracks its current foreground process group and sends signal to that group when some event occurs.

But this assumes that all processes from any given process group share the same controlling terminal, so that signals sent by terminal are meaningful for them.

Controlling terminal is shared by the following rules:

  • all processes from the same session share the same controlling terminal;
  • processes from different sessions can't share the same controlling terminal.

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

Reference

See "The Linux Programming Interface", Chapter 34 (Process Groups, Sessions, and Job Control).

https://stackoverflow.com/questions/27523451/why-group-leader-cannnot-able-create-the-session-in-linux
阅读全文
0 0
原创粉丝点击