This section contains detailed descriptions of the functions relating to job control.
You can use the ctermid function to get a file name that you can use to open the controlling terminal. In the GNU library, it returns the same string all the time: "/dev/tty". That is a special "magic" file name that refers to the controlling terminal of the current process (if it has one). To find the name of the specific terminal device, use ttyname; the section called “Identifying Terminals”.
The function ctermid is declared in the header file stdio.h. char * function>ctermid/function> (char *string) The ctermid function returns a string containing the file name of the controlling terminal for the current process. If string is not a null pointer, it should be an array that can hold at least L_ctermid characters; the string is returned in this array. Otherwise, a pointer to a string in a static area is returned, which might get overwritten on subsequent calls to this function.
An empty string is returned if the file name cannot be determined for any reason. Even if a file name is returned, access to the file it represents is not guaranteed.
int function>L_ctermid/function> The value of this macro is an integer constant expression that represents the size of a string large enough to hold the file name returned by ctermid.
See also the isatty and ttyname functions, in the section called “Identifying Terminals”.
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. pid_t function>setsid/function> (void) 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 following errno error conditions are defined for this function:
The calling process is already a process group leader, or there is already another process group around that has the same process group ID.
pid_t function>getsid/function> (pid_t pid) The getsid function returns the process group ID of the session leader of the specified process. If a pid is 0, the process group ID of the session leader of the current process is returned.
In case of error -1 is returned and errno is set. The following errno error conditions are defined for this function:
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 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 (the section called “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. pid_t function>getpgrp/function> (void) The POSIX.1 definition of getpgrp returns the process group ID of the calling process.
pid_t function>getpgrp/function> (pid_t pid) The BSD definition of getpgrp returns the process group ID of the process pid. You can supply a value of 0 for the pid argument to get information about the calling process.
int function>getpgid/function> (pid_t pid) getpgid is the same as the BSD function getpgrp. It returns the process group ID of the process pid. You can supply a value of 0 for the pid argument to get information about the calling process.
In case of error -1 is returned and errno is set. The following errno error conditions are defined for this function:
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.
int function>setpgid/function> (pid_t pid, pid_t pgid) 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. the section called “Job Control is Optional”, for more information.
If the operation is successful, setpgid returns zero. Otherwise it returns -1. The following errno error conditions are defined for this function:
The child process named by pid has executed an exec function since it was forked.
The value of the pgid is not valid.
The system doesn't support job control.
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.
The process indicated by the pid argument is not the calling process or a child of the calling process.
int function>setpgrp/function> (pid_t pid, pid_t pgid) This is the BSD Unix name for setpgid. Both functions do exactly the same thing.
These are the functions for reading or setting the foreground process group of a terminal. You should include the header files sys/types.h and unistd.h in your application to use these functions. Although these functions take a file descriptor argument to specify the terminal device, the foreground job is associated with the terminal file itself and not a particular open file descriptor.
pid_t function>tcgetpgrp/function> (int filedes) This function returns the process group ID of the foreground process group associated with the terminal open on descriptor filedes.
If there is no foreground process group, the return value is a number greater than 1 that does not match the process group ID of any existing process group. This can happen if all of the processes in the job that was formerly the foreground job have terminated, and no other job has yet been moved into the foreground.
In case of an error, a value of -1 is returned. The following errno error conditions are defined for this function:
The filedes argument is not a valid file descriptor.
The system doesn't support job control.
The terminal file associated with the filedes argument isn't the controlling terminal of the calling process.
int function>tcsetpgrp/function> (int filedes, pid_t pgid) This function is used to set a terminal's foreground process group ID. The argument filedes is a descriptor which specifies the terminal; pgid specifies the process group. The calling process must be a member of the same session as pgid and must have the same controlling terminal.
For terminal access purposes, this function is treated as output. If it is called from a background process on its controlling terminal, normally all processes in the process group are sent a SIGTTOU signal. The exception is if the calling process itself is ignoring or blocking SIGTTOU signals, in which case the operation is performed and no signal is sent.
If successful, tcsetpgrp returns 0. A return value of -1 indicates an error. The following errno error conditions are defined for this function:
The filedes argument is not a valid file descriptor.
The pgid argument is not valid.
The system doesn't support job control.
The filedes isn't the controlling terminal of the calling process.
The pgid isn't a process group in the same session as the calling process.
pid_t function>tcgetsid/function> (int fildes) This function is used to obtain the process group ID of the session for which the terminal specified by fildes is the controlling terminal. If the call is successful the group ID is returned. Otherwise the return value is (pid_t) -1 and the global variable errno is set to the following value:
The filedes argument is not a valid file descriptor.
The calling process does not have a controlling terminal, or the file is not the controlling terminal.