Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <unistd.h>
int tcsetpgrp(int fildes, pid_t newid);
Sets the process group ID (PGID) of the foreground process group associated with the terminal referred to by fildes. This terminal must be the controlling terminal of the process calling tcsetpgrp() and must be currently associated with the session of the calling process. newid must match a PGID of a process in the same session as the calling process.
After the PGID associated with the terminal is set, reads by the process group formerly associated with the terminal fail or cause the process group to stop from a SIGTTIN signal. Writes may also cause the process to stop (from a SIGTTOU signal), or they may succeed, depending on how tcsetattr() sets TOSTOP and the signal options for SIGTTOU.
fildes can be any of the descriptors representing the controlling terminal (such as standard input, standard output, and standard error), and the function affects future access from any file descriptor in use for the terminal. Consider using redirection when specifying the file descriptor.
If tcsetpgrp() is called from a background process group against the caller's controlling terminal, a SIGTTOU signal may be generated depending how the process is handling SIGTTOUs:
Processing for SIGTTOU | System Behavior |
---|---|
Default or signal handler | The SIGTTOU signal is generated, and the function is not performed. tcsetpgrp() returns -1 and sets errno to EINTR. |
Ignored or blocked | The SIGTTOU signal is not sent, and the function continues normally. |
If successful, tcsetpgrp() returns 0.
⁄* CELEBT10
This example changes the PGID.
*⁄
#define _POSIX_SOURCE
#include <termios.h>
#include <unistd.h>
#include <sys⁄wait.h>
#include <stdio.h>
#include <signal.h>
main() {
pid_t pid;
int status;
if (fork() == 0)
if ((pid = tcgetpgrp(STDOUT_FILENO)) < 0)
perror("tcgetpgrp() error");
else {
printf("original foreground process group id of stdout was %d\n",
(int) pid);
if (setpgid(getpid(), 0) != 0)
perror("setpgid() error");
else {
printf("now setting to %d\n", (int) getpid());
if (tcsetpgrp(STDOUT_FILENO, getpid()) != 0)
perror("tcsetpgrp() error");
else if ((pid = tcgetpgrp(STDOUT_FILENO)) < 0)
perror("tcgetpgrp() error");
else
printf("new foreground process group id of stdout was %d\n",
(int) pid);
}
}
else wait(&status);
}
original foreground process group id of stdout was 2228230
now setting to 2949128
new foreground process group id of stdout was 2949128