tcgetpgrp() — Get the foreground process group ID

Standards

Standards / Extensions C or C++ Dependencies

POSIX.1
XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _POSIX_SOURCE
#include <unistd.h>

pid_t tcgetpgrp(int fildes);

General description

Gets the process group ID (PGID) of the foreground process group associated with the terminal referred to by fildes. tcgetpgrp() can run from a background process, but the information may subsequently be changed by a process in the foreground process group.

Returned value

If successful, tcgetpgrp() returns of the foreground process group's PGID.

If unsuccessful, tcgetpgrp() returns -1 and sets errno to one of the following values:
Error Code
Description
EBADF
fildes is not a valid open file descriptor.
ENOTTY
The calling process does not have a controlling terminal, or the file is not the controlling terminal.

Example

CELEBT07
⁄* CELEBT07

   This example gets the foreground PGID.

 *⁄
#define _POSIX_SOURCE
#include <termios.h>
#include <unistd.h>
#include <sys⁄wait.h>       ⁄*FIX: was #include <sys⁄wait.h> *⁄
#include <stdio.h>

main() {
  pid_t pid;

  if ((pid = tcgetpgrp(STDOUT_FILENO)) < 0)
    perror("tcgetpgrp() error");
  else
    printf("the foreground process group id of stdout is %d\n",
           (int) pid);
}
Output
the foreground process group id of stdout is 4063240

Related information