Process Group ID

Every process belongs to a process group that is identified by an integer process group ID value. When a process generates child processes, the operating system automatically creates a process group. The initial parent process is known as the process leader . The process leader's PID will be the same as its process group ID. [2] Additional process group members generated by the process group leader inherit the same process group ID. The operating system uses process group relationships to distribute signals to groups of processes. For example, should a process group leader receive a kill or hang-up signal causing it to terminate, then all processes in its group will also be passed the same terminating signal. A process can find its process group ID from the system call getpgid . In some versions of Linux you may find the getpgid system call absent. In these versions the system call getpgrp (which requires no PID argument) provides the same functionality as the getpgid system call. The getpgid system call is defined in Table 2.3.

[2] Ah-haother than generating temporary file names , another use for the getpid system call!

Table 2.3. Summary of the getpgid System Call.

Include File(s)


 

Manual Section

2

Summary

pid_t getpgid( pid_t pid );

Return

Success

Failure

Sets errno

The process group ID

1

Yes

If successful, this call will return the process group ID for the pid that is passed. If the value of pid is 0, the call is for the current process (eliminating the need for a separate call to getpid ). If the getpgid system call fails, a 1 is returned and the value in errno is set to one of the values in Table 2.4 to indicate the source of the error.

Table 2.4. getpgid Error Messages.

#

Constant

perror Message

Explanation

1

EPERM

Not owner

Invalid access permissions for the calling process.

3

ESRCH

No such process

No such process ID as pid .

A short program using the getpgid system call is shown in Program 2.1. Before looking over the program, a brief explanation concerning the compilation of the program is in order. As UNIX has evolved, developers have established a number of standards such as ANSI C, POSIX. 1, POSIX. 2, BSD, SVID, X/Open, and others. On occasion, system calls (such as getpgid ) and library functions created under one standard (say, BSD) are modified slightly to meet the requirements for another standard (such as POSIX). When using the g++ compiler, defining the constant _GNU_SOURCE instructs the compiler to use the POSIX definition if there is a conflict.

Program 2.1 Displaying process group IDs.

File : p2.1.cxx
 /*
 Displaying process group ID information
 */
 #define _GNU_SOURCE
 + #include 
 #include 
 #include 
 using namespace std;
 int
 10 main( ){
 cout << "

Initial process 	 PID " << getpid()
 << "	 PPID "<< getppid()
 << "	 GID " << getpgid(0)
 << endl << getpgid(pid_t(getppid())) << endl;
 +
 for (int i = 0; i < 3; ++i)
 if (fork( ) == 0) // Generate some processes
 cout << "New process 	 PID " << getpid()
 << "	 PPID "<< getppid()
 20 << "	 GID " << getpgid(0)
 << endl;
 return 0;
 }

Figure 2.1 displays the output of the program.

Figure 2.1 Program 2.1 output.

Initial process PID 3350 PPID 3260 GID 3350

New process PID 3351 PPID 3350 GID 3350
New process PID 3352 PPID 3351 GID 3350
New process PID 3353 PPID 3350 GID 3350
New process PID 3356 PPID 3353 GID 3350
New process PID 3355 PPID 3351 GID 3350
New process PID 3354 PPID 3352 GID 3350
New process PID 3357 PPID 3350 GID 3350

Note that the actual ID numbers change each time the program is run. The relationship of the processes within the process group is shown in Figure 2.2.

Figure 2.2. Process ID relationships.

graphics/02fig02.gif

All of the processes generated by the program indicate that they belong to the same process group: the process group of the initial process 3350. If the parent of a process dies [3] (terminates) before its child process(es), the process init (which is process ID 1) will inherit the child process and become its foster parent. The process group ID for a process does not change if this inheritance occurs.

[3] There seems to be no end to the anthropomorphic references for parent/child processes, even when they border on the macabre!

A process may change its process group by using the system call setpgid , which sets the process group ID (Table 2.5).

The setpgid system call sets the process group pid to that of pgid . If the value for pid is 0, the call refers to the current process. Otherwise, the call refers to the specified PID. The value for pgid represents the group to which the process will belong. If the value for pgid is 0, the pid referenced process will become the process leader. For this call to be successful, the invoking process must have the correct permissions to institute the requested change. The setpgid system call returns 0 if successful, or returns a 1 and sets errno if it fails. The value errno is assigned when setpgid fails is given in Table 2.6.

Table 2.5. Summary of the setpgid System Call.

Include File(s)


 

Manual Section

2

Summary

int setpgid(pid_t pid, pid_t pgid);

Return

Success

Failure

Sets errno

1

Yes

Table 2.6. setpgid Error Messages.

#

Constant

perror Message

Explanation

1

EPERM

Operation not permitted

  • Process pid already a session leader.
  • Process pid is not in same session as calling process.
  • Invalid process group specified.

3

ESRCH

No such process

No such process ID as pid .

22

EINVAL

Invalid argument

The pgid value is less than 0 or greater than MAX_PID1.

For those of us who talk fast or listen casually, it is easy to confuse the process group ID with the process's group ID. A process's group ID is covered in Section 2.6.

In addition to process groups, UNIX also supports the concept of a session . A session is a collection of related and unrelated processes and process groups. As with process grouping, there are a number of system calls (e.g., setsid , getsid ) that can be used to create and manipulate a session. The process calling setsid becomes the session leader as well as the process group leader. In this arrangement, there is no controlling tty (terminal device). Keep in mind a process inherits its controlling terminal from its parent. Certain input sequences, such as a quit (CTRL+) or an interrupt (CTRL+C), received by a controlling terminal are automatically propagated to other processes in the session.

EXERCISE

Modify Program 2.1 so that each new process becomes its own group leader.

Programs and Processes

Processing Environment

Using Processes

Primitive Communications

Pipes

Message Queues

Semaphores

Shared Memory

Remote Procedure Calls

Sockets

Threads

Appendix A. Using Linux Manual Pages

Appendix B. UNIX Error Messages

Appendix C. RPC Syntax Diagrams

Appendix D. Profiling Programs



Interprocess Communication in Linux
Interprocess Communications in Linux: The Nooks and Crannies
ISBN: 0130460427
EAN: 2147483647
Year: 2001
Pages: 136

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net