3.1 Process Identification

Team-FLY

UNIX identifies processes by a unique integral value called the process ID . Each process also has a parent process ID , which is initially the process ID of the process that created it. If this parent process terminates, the process is adopted by a system process so that the parent process ID always identifies a valid process.

The getpid and getppid functions return the process ID and the parent process ID, respectively. The pid_t is an unsigned integer type that represents a process ID.

  SYNOPSIS  #include <unistd.h>    pid_t getpid(void);    pid_t getppid(void) ;  POSIX  

Neither the getpid nor the getppid functions can return an error.

Example 3.1 outputPID.c

The following program outputs its process ID and its parent process ID. Notice that the return values are cast to long for printing since there is no guarantee that a pid_t will fit in an int .

 #include <stdio.h> #include <unistd.h> int main (void) {    printf("I am process %ld\n", (long)getpid());    printf("My parent is %ld\n", (long)getppid());    return 0; } 

System administrators assign a unique integral user ID and an integral group ID to each user when creating the user's account. The system uses the user and group IDs to retrieve from the system database the privileges allowed for that user. The most privileged user, superuser or root , has a user ID of 0. The root user is usually the system administrator.

A UNIX process has several user and group IDs that convey privileges to the process. These include the real user ID, the real group ID, the effective user ID and the effective group ID. Usually, the real and effective IDs are the same, but under some circumstances the process can change them. The process uses the effective IDs for determining access permissions for files. For example, a program that runs with root privileges may want to create a file on behalf of an ordinary user. By setting the process's effective user ID to be that of this user, the process can create the files "as if" the user created them. For the most part, we assume that the real and effective user and group IDs are the same.

The following functions return group and user IDs for a process. The gid_t and uid_t are integral types representing group and user IDs, respectively. The getgid and getuid functions return the real IDs, and getegid and geteuid return the effective IDs.

  SYNOPSIS  #include <unistd.h>    gid_t getegid(void);    uid_t geteuid(void);    git_t getgid(void);    uid_t getuid(void);  POSIX  

None of these functions can return an error.

Example 3.2 outputIDs.c

The following program prints out various user and group IDs for a process.

 #include <stdio.h> #include <unistd.h> int main(void) {    printf("My real user ID is       %5ld\n", (long)getuid());    printf("My effective user ID is  %5ld\n", (long)geteuid());    printf("My real group ID is      %5ld\n", (long)getgid());    printf("My effective group ID is %5ld\n", (long)getegid());    return 0; } 
Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

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