Here are descriptions of the functions for manipulating process groups. Your program should include the header files sys/types.h and unistd.h to use these functions.
The
setsid
function creates a new session. The calling process becomes the session leader, and is put in a new process group whose process group ID is the same as the process ID of that process. There are initially no other processes in the new process group, and no other process groups in the new session.This function also makes the calling process have no controlling terminal.
The
setsid
function returns the new process group ID of the calling process if successful. A return value of-1
indicates an error. The followingerrno
error conditions are defined for this function:
EPERM
- The calling process is already a process group leader, or there is already another process group around that has the same process group ID.
The
getsid
function returns the process group ID of the session leader of the specified process. If a pid is0
, the process group ID of the session leader of the current process is returned.In case of error
-1
is returned anderrno
is set. The followingerrno
error conditions are defined for this function:
ESRCH
- There is no process with the given process ID pid.
EPERM
- The calling process and the process specified by pid are in different sessions, and the implementation doesn't allow to access the process group ID of the session leader of the process with ID pid from the calling process.
The getpgrp
function has two definitions: one derived from BSD
Unix, and one from the POSIX.1 standard. The feature test macros you
have selected (see Feature Test Macros) determine which definition
you get. Specifically, you get the BSD version if you define
_BSD_SOURCE
; otherwise, you get the POSIX version if you define
_POSIX_SOURCE
or _GNU_SOURCE
. Programs written for old
BSD systems will not include unistd.h, which defines
getpgrp
specially under _BSD_SOURCE
. You must link such
programs with the -lbsd-compat
option to get the BSD definition.
The POSIX.1 definition of
getpgrp
returns the process group ID of the calling process.
The BSD definition of
getpgrp
returns the process group ID of the process pid. You can supply a value of0
for the pid argument to get information about the calling process.
getpgid
is the same as the BSD functiongetpgrp
. It returns the process group ID of the process pid. You can supply a value of0
for the pid argument to get information about the calling process.In case of error
-1
is returned anderrno
is set. The followingerrno
error conditions are defined for this function:
ESRCH
- There is no process with the given process ID pid. The calling process and the process specified by pid are in different sessions, and the implementation doesn't allow to access the process group ID of the process with ID pid from the calling process.
The
setpgid
function puts the process pid into the process group pgid. As a special case, either pid or pgid can be zero to indicate the process ID of the calling process.This function fails on a system that does not support job control. See Job Control is Optional, for more information.
If the operation is successful,
setpgid
returns zero. Otherwise it returns-1
. The followingerrno
error conditions are defined for this function:
EACCES
- The child process named by pid has executed an
exec
function since it was forked.EINVAL
- The value of the pgid is not valid.
ENOSYS
- The system doesn't support job control.
EPERM
- The process indicated by the pid argument is a session leader, or is not in the same session as the calling process, or the value of the pgid argument doesn't match a process group ID in the same session as the calling process.
ESRCH
- The process indicated by the pid argument is not the calling process or a child of the calling process.