Next: Terminal Access Functions, Previous: Identifying the Terminal, Up: Functions for Job Control
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
setsidfunction 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
setsidfunction returns the new process group ID of the calling process if successful. A return value of-1indicates an error. The followingerrnoerror 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
getsidfunction 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
-1is returned anderrnois set. The followingerrnoerror 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
getpgrpreturns the process group ID of the calling process.
The BSD definition of
getpgrpreturns the process group ID of the process pid. You can supply a value of0for the pid argument to get information about the calling process.
getpgidis the same as the BSD functiongetpgrp. It returns the process group ID of the process pid. You can supply a value of0for the pid argument to get information about the calling process.In case of error
-1is returned anderrnois set. The followingerrnoerror 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
setpgidfunction 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,
setpgidreturns zero. Otherwise it returns-1. The followingerrnoerror conditions are defined for this function:
EACCES- The child process named by pid has executed an
execfunction 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.