Advanced Programming in the UNIX Environment: Second Edition [Electronic resources]

W. Richard Stevens; Stephen A. Rago

نسخه متنی -صفحه : 369/ 158
نمايش فراداده

9.4. Process Groups

Chapter 10.

A process group is a collection of one or more processes, usually associated with the same job (job control is discussed in Section 9.8), that can receive signals from the same terminal. Each process group has a unique process group ID. Process group IDs are similar to process IDs: they are positive integers and can be stored in a pid_t data type. The function getpgrp returns the process group ID of the calling process.

#include <unistd.h> pid_t getpgrp(void);

Returns: process group ID of calling process

In older BSD-derived systems, the getpgrp function took a

pid argument and returned the process group for that process. The Single UNIX Specification defines the getpgid function as an XSI extension that mimics this behavior.

#include <unistd.h> pid_t getpgid(pid_t

pid );

Returns: process group ID if OK, 1 on error

If

pid is 0, the process group ID of the calling process is returned. Thus,

getpgid(0);

is equivalent to

getpgrp();

Each process group can have a process group leader. The leader is identified by its process group ID being equal to its process ID.

It is possible for a process group leader to create a process group, create processes in the group, and then terminate. The process group still exists, as long as at least one process is in the group, regardless of whether the group leader terminates. This is called the process group lifetimethe period of time that begins when the group is created and ends when the last remaining process leaves the group. The last remaining process in the process group can either terminate or enter some other process group.

A process joins an existing process group or creates a new process group by calling setpgid. (In the next section, we'll see that setsid also creates a new process group.)

#include <unistd.h> int setpgid(pid_t

pid , pid_t

pgid );

Returns: 0 if OK, 1 on error

Section 8.6 lets us wait for either a single process or one process from a specified process group.