Which process group is the terminal foreground process can be determined and/or set by these system calls:
#include <unistd.h> pid_t tcgetpgrp(int fd); int tcsetpgrp(int fd, pid_t pgrp);
where
-
fd is the file descriptor of the controlling terminal of the session; the file descriptor can be any one of the descriptors representing the controlling terminal (e.g. 0 for standard input [stdin], 1 for standard output [stdout], or 2 for standard error [stderr], and
-
pgrp is the group id of a process group in the same session as the calling process that will be set as the foreground process.
Example 1: A process makes itself the foreground process:
tcsetpgrp(0, getpid());
Example 2: Make some other process with pid 1403 (in the same session) be the foreground process:
tcsetpgrp(0, 1403);
Example 3: Get the group pid of the foreground process for the controlling terminal of the calling process:
pid_t gid; gid = tcgetpgrp(0);
Each of these functions returns -1 if there is an error. Errors occur if there is no controlling terminal associated with the calling process or if the file descriptor (0 in the examples) is not a valid file descriptor for the calling process or if the process group (e.g.1403) is not in the same session as the calling process. (See the man pages for details.)