Process Control
Process control consists of the system interfaces that allow programs to identify themselves, obtain credential information, create new processes, terminate execution, synchronize with child processes, and manage process execution. These interfaces form one of the most important parts of UNIX system programming.
Learning Objectives
- Understand process identifiers.
- Learn process credential APIs.
- Study parent-child relationships.
- Understand process ownership.
- Prepare for process creation and termination.
1. What is Process Control?
Process control refers to the set of operating system services that allow applications to obtain information about processes, manage their execution, communicate with parent and child processes, and control the process lifecycle.
2. Process Identifiers
Each process executing in a UNIX system is assigned one or more identifiers by the kernel. These identifiers uniquely distinguish the process and define its relationship with other processes.
3. Common Process Identifiers
| Identifier | Description |
|---|---|
| PID | Unique process identifier. |
| PPID | Parent process identifier. |
| UID | Real user identifier. |
| EUID | Effective user identifier. |
| GID | Real group identifier. |
| EGID | Effective group identifier. |
4. getpid()
The getpid() function returns the process identifier of
the calling process.
Every executing process has a unique PID while it exists.
5. Example Using getpid()
6. getppid()
The getppid() function returns the process identifier of
the parent process that created the calling process.
7. Example Using getppid()
8. Parent-Child Relationship
9. Practical Applications
| Application | Purpose |
|---|---|
| Shell | Track child processes. |
| Daemon | Verify parent process. |
| Logging | Record process identifiers. |
| Debuggers | Display process hierarchy. |
| Monitoring Tools | Identify running processes. |
10. Interview Questions
- What is a PID?
- Which function returns the current process identifier?
- What is the purpose of
getppid()? - Can two running processes have the same PID?
- What does PPID represent?
- Which header file declares
getpid()? - How is a parent-child relationship established?
- Why are process identifiers important?
- Which kernel component assigns process identifiers?
- What data type is returned by
getpid()?
11. Process Credentials
Every process executes with a set of credentials maintained by the kernel. These credentials determine the identity of the process and the permissions it has while accessing system resources.
12. Real User ID (UID)
The Real User ID identifies the user who started the process. It normally remains unchanged throughout the lifetime of the process unless explicitly modified by privileged operations.
13. Example Using getuid()
14. Effective User ID (EUID)
The Effective User ID determines the permissions used by the kernel when performing access control checks. It may differ from the Real User ID, particularly when executing programs with special permission attributes.
15. Example Using geteuid()
16. Real Group ID (GID)
The Real Group ID identifies the primary group associated with the user who started the process.
17. Effective Group ID (EGID)
The Effective Group ID is used by the kernel during permission checks that involve group ownership.
18. Example Displaying Process Credentials
19. Real IDs vs Effective IDs
| Real ID | Effective ID |
|---|---|
| Represents the user or group that started the process. | Represents the identity used for permission checking. |
| Normally remains unchanged. | May differ depending on program execution attributes. |
| Used to identify the process owner. | Used by the kernel during access control decisions. |
20. Interview Questions
- What is the purpose of
getuid()? - What is the difference between
getuid()andgeteuid()? - Why does a process have both real and effective user IDs?
- Which function returns the effective group identifier?
- What is returned by
getgid()? - When are effective IDs used?
- Can the Real UID and Effective UID be different?
- What header file declares these credential functions?
- Why are process credentials important?
- Which identifiers are used by the kernel during access control?
21. Saved User ID
In addition to the Real User ID and Effective User ID, a process may also have a Saved User ID. The Saved User ID allows privileged programs to temporarily change their effective identity and later restore it.
22. Supplementary Groups
A process may belong to one primary group and multiple supplementary groups. These additional group memberships allow the process to access files and other resources that are owned by those groups.
23. getgroups()
The getgroups() function retrieves the supplementary
group IDs associated with the calling process.
24. Example Using getgroups()
25. Process Credential Workflow
26. Linux Commands
| Command | Purpose |
|---|---|
id |
Display user IDs, group IDs and supplementary groups. |
whoami |
Display the effective user name. |
groups |
Display group memberships. |
id username |
Display credential information for a specified user. |
27. Example Output of id
28. Best Practices
- Always use standard POSIX functions to obtain process credentials.
- Do not assume a fixed number of supplementary groups.
- Check the return value of
getgroups(). - Use credential information instead of hardcoded user identifiers.
- Display credential information in diagnostic utilities when troubleshooting permission issues.
29. Common Programming Mistakes
- Assuming the Real User ID and Effective User ID are always identical.
- Ignoring supplementary group memberships.
- Using fixed-size assumptions without checking the returned group count.
- Ignoring error returns from
getgroups(). - Using numeric identifiers without understanding their meaning.
30. Interview Questions
- What is the purpose of the Saved User ID?
- What are supplementary groups?
- Which function retrieves supplementary group IDs?
- What is returned by
getgroups()? - Why can a process belong to multiple groups?
- Which Linux command displays all process credentials?
- What information does the
idcommand display? - Why should applications check the return value of
getgroups()? - Can a process have more than one supplementary group?
- Why are supplementary groups important for file permissions?
31. Changing Process Credentials
UNIX provides functions that allow a process to change its user and group credentials. The kernel validates every credential change to ensure that only authorized processes can perform these operations.
32. setuid()
The setuid() function sets the user ID of the calling
process, subject to the permission rules enforced by the operating
system.
33. seteuid()
The seteuid() function changes the Effective User ID of
the calling process.
Changing the Effective User ID affects subsequent permission checks
performed by the kernel.
34. setgid()
The setgid() function sets the group ID of the calling
process according to the permission rules enforced by the operating
system.
35. setegid()
The setegid() function changes the Effective Group ID of
the calling process.
Subsequent group permission checks use the updated Effective Group ID.
36. Credential Modification Workflow
37. Example Using seteuid()
38. Privileged Processes
Some credential-changing operations require elevated privileges. The kernel determines whether the calling process has sufficient permissions before allowing the requested credential change.
| Operation | Kernel Validation |
|---|---|
| Change User ID | Permission check required. |
| Change Group ID | Permission check required. |
| Change Effective IDs | Validated according to process credentials. |
39. Best Practices
- Always check the return value of credential-changing functions.
- Handle permission failures gracefully.
- Avoid unnecessary credential changes.
- Use standard POSIX interfaces instead of platform-specific methods.
- Understand the distinction between real and effective identifiers before modifying them.
40. Interview Questions
- What is the purpose of
setuid()? - How does
seteuid()differ fromsetuid()? - Which function changes the Effective Group ID?
- Why does the kernel validate credential changes?
- When can a credential modification fail?
- What should an application do if
setuid()returns-1? - Why are effective IDs important during access control?
- Can every process change its user ID?
- Which header file declares credential modification functions?
- Why should applications avoid unnecessary credential changes?
41. Process Control API Comparison
| Function | Purpose |
|---|---|
getpid() |
Return the process identifier. |
getppid() |
Return the parent process identifier. |
getuid() |
Return the Real User ID. |
geteuid() |
Return the Effective User ID. |
getgid() |
Return the Real Group ID. |
getegid() |
Return the Effective Group ID. |
getgroups() |
Return supplementary group IDs. |
setuid() |
Modify the process user ID. |
seteuid() |
Modify the Effective User ID. |
setgid() |
Modify the process group ID. |
setegid() |
Modify the Effective Group ID. |
42. Process Credential Summary
| Credential | Purpose |
|---|---|
| PID | Unique identifier of a running process. |
| PPID | Identifier of the parent process. |
| UID | Identifies the user that started the process. |
| EUID | Determines permissions used during access control. |
| GID | Primary group of the process owner. |
| EGID | Group used during permission checking. |
| Supplementary Groups | Additional groups associated with the process. |
| Saved User ID | Allows restoration of a previous effective identity. |
43. Linux Process Commands
| Command | Purpose |
|---|---|
ps -ef |
Display all running processes. |
ps -u username |
Display processes owned by a specific user. |
pstree |
Display the parent-child process hierarchy. |
id |
Display user and group credentials. |
whoami |
Display the effective user name. |
groups |
Display group memberships. |
44. Complete Process Credential Flow
45. Best Practices
- Use POSIX credential APIs instead of platform-specific interfaces.
- Always check the return values of credential-related functions.
- Avoid hardcoding user or group identifiers.
- Understand the difference between identity and permissions.
- Log process identifiers during debugging and diagnostics.
- Use the least privilege necessary for process execution.
46. Common Programming Mistakes
- Confusing the Real User ID with the Effective User ID.
- Ignoring supplementary group memberships.
- Assuming every process can modify its credentials.
- Ignoring failures from credential-changing functions.
- Using hardcoded numeric user IDs in applications.
- Failing to understand parent-child process relationships.
47. Advanced Interview Questions
- Explain the difference between PID and PPID.
- Why does UNIX maintain both Real and Effective User IDs?
- When are Effective IDs used instead of Real IDs?
- What is the purpose of supplementary groups?
- Why does the kernel validate credential modification requests?
- How does
getgroups()differ fromgetgid()? - When would
seteuid()be preferred oversetuid()? - Why is the Saved User ID important?
- How does the kernel determine whether a file access should be allowed?
- Why are process identifiers fundamental to process management?
48. Module Summary
This module introduced the UNIX process control interfaces used to identify processes and manage process credentials. It covered process identifiers, parent-child relationships, real and effective user/group IDs, supplementary groups, credential modification functions, Linux process commands, practical examples, and recommended programming practices. These concepts form the foundation for the next topics involving process creation, execution, synchronization, and process accounting.
Concept Mastery Assessment — Module 34
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.