Parent Process ID

Every process has an associated parent process ID ( PPID ). The parent process is the process that forked (generated) the child process. The ID of the parent process can be obtained by using the system call getppid (Table 2.2).

Table 2.2. Summary of the getppid System Call.

Include File(s)

 

Manual Section

2

Summary

Pid_t getppid( void );

Return

Success

Failure

Sets errno

The parent process ID

1

Yes

Like the getpid system call, getppid does not require an argument. If it is successful, it will return the PID number of the parent process. The getppid call will fail, returning a value of -1 and setting errno to EPERM (1) if the calling process does not have the proper access permissions.

The following code segment displays the PPID:

cout << "My Parent Process ID is " << getppid( ) << endl;

Unfortunately, there is no system call that allows a parent process to determine the PIDs of all its child processes. If such information is needed, the parent process should save the returned child PID value from the fork system call as each child process is created.

EXERCISE

The manual page entry for the getppid system call does not specifically indicate what is returned by getppid if the parent process is no longer present when the getppid call is made. Write a program that displays the value returned by getppid when such an event occurs (the parent predeceases the child). How did you assure that the parent process was not present when the child process made its getppid call?

Категории