Team BBL
Previous Page Next Page

9.7. tcgetpgrp, tcsetpgrp, and tcgetsid Functions

We need a way to tell the kernel which process group is the foreground process group, so that the terminal device driver knows where to send the terminal input and the terminal-generated signals (Figure 9.7).

#include <unistd.h>

pid_t tcgetpgrp(int filedes);

Returns: process group ID of foreground process group if OK, 1 on error

int tcsetpgrp(int filedes, pid_t pgrpid);

Returns: 0 if OK, 1 on error


The function tcgetpgrp returns the process group ID of the foreground process group associated with the terminal open on filedes.

If the process has a controlling terminal, the process can call tcsetpgrp to set the foreground process group ID to pgrpid. The value of pgrpid must be the process group ID of a process group in the same session, and filedes must refer to the controlling terminal of the session.

Most applications don't call these two functions directly. They are normally called by job-control shells.

The Single UNIX Specification defines an XSI extension called tcgetsid to allow an application to obtain the process group ID for the session leader given a file descriptor for the controlling TTY.

#include <termios.h>

pid_t tcgetsid(int filedes);

Returns: session leader's process group ID if OK, 1 on error


Applications that need to manage controlling terminals can use tcgetsid to identify the session ID of the controlling terminal's session leader (which is equivalent to the session leader's process group ID).

    Team BBL
    Previous Page Next Page