setpgidのヘルプ・マニュアル
日本語 英語
setpgid --help
man setpgid
SETPGID(2) Linux Programmer’s Manual SETPGID(2)
名前
setpgid, getpgid, setpgrp, getpgrp - プロセス・グループの設定/取得を行
う
書式
#include
int setpgid(pid_t pid, pid_t pgid);
pid_t getpgid(pid_t pid);
pid_t getpgrp(void); /* POSIX.1 version */
pid_t getpgrp(psid_t pid); /* BSD version */
int setpgrp(void); /* System V version */
int setpgrp(pid_t pid, pid_t pgid); /* BSD version */
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
getpgid(): _XOPEN_SOURCE >= 500
setpgrp() (POSIX.1): _SVID_SOURCE || _XOPEN_SOURCE >= 500
setpgrp() (BSD), getpgrp() (BSD): _BSD_SOURCE && ! (_POSIX_SOURCE ||
_POSIX_C_SOURCE || _XOPEN_SOURCE || _XOPEN_SOURCE_EXTENDED ||
_GNU_SOURCE || _SVID_SOURCE)
説明
これらのインタフェースすべてが Linux で利用可能で、これらを使ってプロセ
ス のプロセス・グループ ID (PGID) の取得や設定ができる。推奨の、POSIX.1
で規定された方法では、 getpgrp(void) で呼び出し元プロセスの PGID を取得
し、 setpgid() で設定する。
setpgid() は pid で指定したプロセスの PGID に pgid を設定する。 pid が
ゼロならば、呼び出し元プロセスのプロセス ID が pid として使用され る 。
pgid がゼロならば、 pid で指定されたプロセスの PGID がそのプロセスのプ
ロセス ID と同じに設定される。 setpgid() をプロセスをあるプロセス・グル
ー プから別のグループへ移動するために使用する場合は (一部のシェルはパイ
プラインを生成する時にこれを行う)、両方のプロセス・グループは同じセッシ
ョンの一部でなければならない (setsid(2) と credentials(7) 参照)。この場
合は pgid は参加すべき既存のプロセス・グループを指定し、そのセッショ ン
ID は参加するプロセスのセッション ID に一致しなければならない。
POSIX.1 バージョンの getpgrp() は引き数を一つもとらず、呼び出し元プロセ
スの PGID を返す。
getpgid() は pid で指定されたプロセスの PGID を返す。 pid がゼロなら ば
、呼び出し元プロセスのプロセス ID が pid として使用される。 (呼び出し元
プロセス以外のプロセスの PGID の取得が必要になることはめったになく、 呼
び 出 し元プロセスの PGID を取得するには POSIX.1 バージョンの getpgrp()
を使うのが望ましい。)
System V バージョンの setpgrp() は引き数を一つもとらず、 setpgid(0, 0)
と等価である。
BSD 仕様の setpgrp() は pid と pgid を引き数にとり、 setpgid(pid, pgid)
と等価である。
BSD 仕様の getpgrp() は pid だけを引き数にとり、 getpgid(pid) と等価 で
ある。
返り値
setpgid() と setpgrp() は成功した場合、ゼロを返す。エラーの場合は -1 を
返し、 errno が適切に設定される。
POSIX.1 バージョンの getpgrp() は常に呼び出しプロセスの PGID を返す。
getpgid() と BSD 仕様の getpgrp() は成功した場合プロセス・グループを 返
す。エラーの場合は -1 を返し、 errno が適切に設定される。
エラー
EACCES 呼び出し元プロセスの子プロセスのプロセス・グループ ID を変更しよ
うとしたが、すでにその子プロセスは execve(2) を実行して い た 。
(setpgid(), setpgrp())
EINVAL pgid が 0 より小さい。 (setpgid(), setpgrp())
EPERM プロセスを異なるセッションのプロセス・グループに移動させようとし
た。または呼び出し元プロセスの子プロセスのプロセス・グループ ID
を変更しようとしたが、その子プロセスは別のセッションだった。また
はセッションリーダーのプロセス・グループ ID を変更しようとした。
(setpgid(), setpgrp())
ESRCH getpgid() の場合: pid がどのプロセスにも一致しない。 setpgid()
の場合: pid が呼び出し元のプロセスではなく、呼び出し元のプロセス
の子プロセスでもない。
準拠
setpgid() と、引き数なしバージョンの getpgrp() は POSIX.1-2001 に準拠し
ている。
POSIX.1-2001 は、 getpgid() と、引き数なしバージョンの setpgrp() も規定
している。 POSIX.1-2008 は、この setpgrp() の仕様を廃止予定としている。
引き数 1 個バージョンの getpgrp() と引き数 2 個バージョン の setpgrp()
は 4.2BSD に由来し、 POSIX.1 では規定されていない。
注意
fork(2) で 作 成 さ れ た子プロセスは、親プロセスの PGID を継承する。
execve(2) の前後で PGID は保存される。
各プロセス・グループはセッションのメンバーであり、各プロセスはそのプ ロ
セス・グループが所属しているセッションのメンバーである。
セ ッションは制御端末 (controlling terminal) を持つことができる。いつで
も、セッションに所属するプロセス・グループの一つ (だけ) が端末のフォ ア
グ ランドのプロセス・グループになることができ、残りのプロセス・グループ
はバックグラウンドになる。端末からシグナルが生成された場合 (例えば、 中
断キーを叩いて SIGINT が生成されるなど)、そのシグナルはフォアグラウンド
のプロセス・グループに送られる (シグナルを生 成 す る 文 字 の 説 明 は
termios(3) を参照)。フォアグラウンドのプロセス・グループだけが端末から
の read(2) ができる。バックグラウンドのプロセス・グループが端末か ら の
read(2) を行おうとした場合、そのプロセス・グループにはシグナル SIGTSTP
が送られ、そのプロセス・グループは一時停止 (suspend) する。関数 tcgetp-
grp(3) と tcsetpgrp(3) を使うと、制御端末のフォアグラウンドのプロセス・
グループを取得/設定できる。
setpgid() と getpgrp() は、 bash(1) のようなプログラムで、シェルのジ ョ
ブ制御 (job control) の実装のためのプロセスグループを作成するのに使われ
る。
セッションが制御端末を持っていて、その端末に対して CLOCAL フラグが設 定
さ れ て おらず、端末のハングアップが起きた場合、セッション・リーダーに
SIGHUP が送られる。セッション・リーダーが終了した場合には、その制御端末
の フォアグランドのプロセス・グループに所属する各プロセスにも SIGHUP シ
グナルが送られる。
プロセスの終了によってプロセス・グループが孤児 (orphaned) になった際 に
、 その新たに孤児になったプロセス・グループに停止しているメンバーがいれ
ば、その孤児になったプロセス・グループの全てのプロセスに SIGHUP シグ ナ
ルに続けて SIGCONT シグナルが送られる。
関連項目
getuid(2), setsid(2), tcgetpgrp(3), tcsetpgrp(3), termios(3), creden-
tials(7)
Linux 2008-08-06 SETPGID(2)
SETPGID(2) Linux Programmer’s Manual SETPGID(2)
NAME
setpgid, getpgid, setpgrp, getpgrp - set/get process group
SYNOPSIS
#include
int setpgid(pid_t pid, pid_t pgid);
pid_t getpgid(pid_t pid);
pid_t getpgrp(void); /* POSIX.1 version */
pid_t getpgrp(psid_t pid); /* BSD version */
int setpgrp(void); /* System V version */
int setpgrp(pid_t pid, pid_t pgid); /* BSD version */
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getpgid(): _XOPEN_SOURCE >= 500
setpgrp() (POSIX.1): _SVID_SOURCE || _XOPEN_SOURCE >= 500
setpgrp() (BSD), getpgrp() (BSD): _BSD_SOURCE && ! (_POSIX_SOURCE ||
_POSIX_C_SOURCE || _XOPEN_SOURCE || _XOPEN_SOURCE_EXTENDED ||
_GNU_SOURCE || _SVID_SOURCE)
DESCRIPTION
All of these interfaces are available on Linux, and are used for get-
ting and setting the process group ID (PGID) of a process. The pre-
ferred, POSIX.1-specified ways of doing this are: getpgrp(void), for
retrieving the calling process’s PGID; and setpgid(), for setting a
process’s PGID.
setpgid() sets the PGID of the process specified by pid to pgid. If
pid is zero, then the process ID of the calling process is used. If
pgid is zero, then the PGID of the process specified by pid is made the
same as its process ID. If setpgid() is used to move a process from
one process group to another (as is done by some shells when creating
pipelines), both process groups must be part of the same session (see
setsid(2) and credentials(7)). In this case, the pgid specifies an
existing process group to be joined and the session ID of that group
must match the session ID of the joining process.
The POSIX.1 version of getpgrp(), which takes no arguments, returns the
PGID of the calling process.
getpgid() returns the PGID of the process specified by pid. If pid is
zero, the process ID of the calling process is used. (Retrieving the
PGID of a process other than the caller is rarely necessary, and the
POSIX.1 getpgrp() is preferred for that task.)
The System V-style setpgrp(), which takes no arguments, is equivalent
to setpgid(0, 0).
The BSD-specific setpgrp() call, which takes arguments pid and pgid, is
equivalent to setpgid(pid, pgid).
The BSD-specific getpgrp() call, which takes a single pid argument, is
equivalent to getpgid(pid).
RETURN VALUE
On success, setpgid() and setpgrp() return zero. On error, -1 is
returned, and errno is set appropriately.
The POSIX.1 getpgrp() always returns the PGID of the caller.
getpgid(), and the BSD-specific getpgrp() return a process group on
success. On error, -1 is returned, and errno is set appropriately.
ERRORS
EACCES An attempt was made to change the process group ID of one of the
children of the calling process and the child had already per-
formed an execve(2) (setpgid(), setpgrp()).
EINVAL pgid is less than 0 (setpgid(), setpgrp()).
EPERM An attempt was made to move a process into a process group in a
different session, or to change the process group ID of one of
the children of the calling process and the child was in a dif-
ferent session, or to change the process group ID of a session
leader (setpgid(), setpgrp()).
ESRCH For getpgid(): pid does not match any process. For setpgid():
pid is not the calling process and not a child of the calling
process.
CONFORMING TO
setpgid() and the version of getpgrp() with no arguments conform to
POSIX.1-2001.
POSIX.1-2001 also specifies getpgid() and the version of setpgrp() that
takes no arguments. (POSIX.1-2008 marks this setpgrp() specification
as obsolete.)
The version of getpgrp() with one argument and the version of setpgrp()
that takes two arguments derive from 4.2BSD, and are not specified by
POSIX.1.
NOTES
A child created via fork(2) inherits its parent’s process group ID.
The PGID is preserved across an execve(2).
Each process group is a member of a session and each process is a mem-
ber of the session of which its process group is a member.
A session can have a controlling terminal. At any time, one (and only
one) of the process groups in the session can be the foreground process
group for the terminal; the remaining process groups are in the back-
ground. If a signal is generated from the terminal (e.g., typing the
interrupt key to generate SIGINT), that signal is sent to the fore-
ground process group. (See termios(3) for a description of the charac-
ters that generate signals.) Only the foreground process group may
read(2) from the terminal; if a background process group tries to
read(2) from the terminal, then the group is sent a SIGTSTP signal,
which suspends it. The tcgetpgrp(3) and tcsetpgrp(3) functions are
used to get/set the foreground process group of the controlling termi-
nal.
The setpgid() and getpgrp() calls are used by programs such as bash(1)
to create process groups in order to implement shell job control.
If a session has a controlling terminal, and the CLOCAL flag for that
terminal is not set, and a terminal hangup occurs, then the session
leader is sent a SIGHUP. If the session leader exits, then a SIGHUP
signal will also be sent to each process in the foreground process
group of the controlling terminal.
If the exit of the process causes a process group to become orphaned,
and if any member of the newly orphaned process group is stopped, then
a SIGHUP signal followed by a SIGCONT signal will be sent to each
process in the newly orphaned process group.
SEE ALSO
getuid(2), setsid(2), tcgetpgrp(3), tcsetpgrp(3), termios(3), creden-
tials(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-08-06 SETPGID(2)