ADVERTISEMENT
Responsive Ad Unit • Systems Architecture & Low-Level Engineering

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.

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

5. Example Using getpid()

#include <stdio.h> #include <unistd.h> int main(void) { printf("PID : %d\n", (int)getpid()); return 0; }

6. getppid()

The getppid() function returns the process identifier of the parent process that created the calling process.

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

7. Example Using getppid()

#include <stdio.h> #include <unistd.h> int main(void) { printf("Parent PID : %d\n", (int)getppid()); return 0; }

8. Parent-Child Relationship

Parent Process ↓ fork() ↓ Child Process ↓ Parent PID Stored ↓ Child Uses getppid()

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

  1. What is a PID?
  2. Which function returns the current process identifier?
  3. What is the purpose of getppid()?
  4. Can two running processes have the same PID?
  5. What does PPID represent?
  6. Which header file declares getpid()?
  7. How is a parent-child relationship established?
  8. Why are process identifiers important?
  9. Which kernel component assigns process identifiers?
  10. 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.

#include <unistd.h> uid_t getuid(void);

13. Example Using getuid()

#include <stdio.h> #include <unistd.h> int main(void) { printf("Real UID : %u\n", (unsigned)getuid()); return 0; }

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.

#include <unistd.h> uid_t geteuid(void);

15. Example Using geteuid()

#include <stdio.h> #include <unistd.h> int main(void) { printf("Effective UID : %u\n", (unsigned)geteuid()); return 0; }

16. Real Group ID (GID)

The Real Group ID identifies the primary group associated with the user who started the process.

#include <unistd.h> gid_t getgid(void);

17. Effective Group ID (EGID)

The Effective Group ID is used by the kernel during permission checks that involve group ownership.

#include <unistd.h> gid_t getegid(void);

18. Example Displaying Process Credentials

#include <stdio.h> #include <unistd.h> int main(void) { printf("PID : %d\n", (int)getpid()); printf("PPID : %d\n", (int)getppid()); printf("UID : %u\n", (unsigned)getuid()); printf("EUID : %u\n", (unsigned)geteuid()); printf("GID : %u\n", (unsigned)getgid()); printf("EGID : %u\n", (unsigned)getegid()); return 0; }

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

  1. What is the purpose of getuid()?
  2. What is the difference between getuid() and geteuid()?
  3. Why does a process have both real and effective user IDs?
  4. Which function returns the effective group identifier?
  5. What is returned by getgid()?
  6. When are effective IDs used?
  7. Can the Real UID and Effective UID be different?
  8. What header file declares these credential functions?
  9. Why are process credentials important?
  10. 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.

#include <unistd.h> int getgroups(int gidsetsize, gid_t grouplist[]);

24. Example Using getgroups()

#include <stdio.h> #include <unistd.h> int main(void) { gid_t groups[32]; int n; n = getgroups(32, groups); if (n == -1) return 1; printf("Supplementary Groups\n"); for (int i = 0; i < n; i++) { printf("%u\n", (unsigned)groups[i]); } return 0; }

25. Process Credential Workflow

User Starts Program ↓ Kernel Creates Process ↓ Assign PID ↓ Assign UID ↓ Assign GID ↓ Assign Supplementary Groups ↓ Assign Effective IDs ↓ Program Executes

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

uid=1000(user) gid=1000(user) groups=1000(user),4(adm),24(cdrom)

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

  1. What is the purpose of the Saved User ID?
  2. What are supplementary groups?
  3. Which function retrieves supplementary group IDs?
  4. What is returned by getgroups()?
  5. Why can a process belong to multiple groups?
  6. Which Linux command displays all process credentials?
  7. What information does the id command display?
  8. Why should applications check the return value of getgroups()?
  9. Can a process have more than one supplementary group?
  10. 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.

#include <unistd.h> int setuid(uid_t uid);

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.

#include <unistd.h> int seteuid(uid_t euid);

34. setgid()

The setgid() function sets the group ID of the calling process according to the permission rules enforced by the operating system.

#include <unistd.h> int setgid(gid_t gid);

35. setegid()

The setegid() function changes the Effective Group ID of the calling process. Subsequent group permission checks use the updated Effective Group ID.

#include <unistd.h> int setegid(gid_t egid);

36. Credential Modification Workflow

Running Process ↓ Calls setuid()/seteuid() ↓ Kernel Validates Request ↓ Permission Check ↓ Allowed? ↓ Yes ─────────► Credentials Updated ↓ No ↓ Operation Fails

37. Example Using seteuid()

#include <stdio.h> #include <unistd.h> int main(void) { uid_t current; current = geteuid(); printf("Current EUID : %u\n", (unsigned)current); /* Attempt to change EUID */ if (seteuid(current) == -1) { perror("seteuid"); return 1; } printf("Effective UID updated.\n"); return 0; }

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

  1. What is the purpose of setuid()?
  2. How does seteuid() differ from setuid()?
  3. Which function changes the Effective Group ID?
  4. Why does the kernel validate credential changes?
  5. When can a credential modification fail?
  6. What should an application do if setuid() returns -1?
  7. Why are effective IDs important during access control?
  8. Can every process change its user ID?
  9. Which header file declares credential modification functions?
  10. 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

User Executes Program ↓ Kernel Creates Process ↓ Assign PID ↓ Assign User IDs ↓ Assign Group IDs ↓ Assign Supplementary Groups ↓ Program Executes ↓ Kernel Uses Effective IDs ↓ Access Granted or Denied

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

  1. Explain the difference between PID and PPID.
  2. Why does UNIX maintain both Real and Effective User IDs?
  3. When are Effective IDs used instead of Real IDs?
  4. What is the purpose of supplementary groups?
  5. Why does the kernel validate credential modification requests?
  6. How does getgroups() differ from getgid()?
  7. When would seteuid() be preferred over setuid()?
  8. Why is the Saved User ID important?
  9. How does the kernel determine whether a file access should be allowed?
  10. 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.

Knowledge Check

Concept Mastery Assessment — Module 34

Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.

Question 1 of 12
What is the primary architectural purpose of Process Control, wait, waitpid & exec Family in operating systems?
Explanation
Process Control, wait, waitpid & exec Family is designed to manage termination macros (wifexited, wexitstatus, wtermsig), zombie and orphan process dynamics, wait3/wait4, and execve arguments vector. within the operating system architecture.
Question 2 of 12
Which system interface or data structure is centrally associated with Process Control, wait, waitpid & exec Family?
Explanation
Under POSIX and Linux, Process Control, wait, waitpid & exec Family operates through standardized system calls and kernel control structures.
Question 3 of 12
What safety or concurrency constraint must developers consider when implementing Process Control, wait, waitpid & exec Family?
Explanation
Proper synchronization, avoidance of race conditions, and handling reentrant execution are critical when working with Process Control, wait, waitpid & exec Family.
Question 4 of 12
When a system call related to Process Control, wait, waitpid & exec Family fails in C, how does the POSIX runtime conventionally report the failure?
Explanation
Standard POSIX system calls return -1 or NULL on error, assigning an error integer (e.g. EINVAL, EPERM, EAGAIN) to the thread-local errno variable.
Question 5 of 12
What is the primary performance tradeoff associated with Process Control, wait, waitpid & exec Family?
Explanation
Operations involving Process Control, wait, waitpid & exec Family balance performance against kernel transition, memory bandwidth, and cache locality tradeoffs.
Question 6 of 12
What security principle governs access to resources in Process Control, wait, waitpid & exec Family?
Explanation
The kernel validates credentials, effective permissions, and Linux capabilities before granting access to protected operations.
Question 7 of 12
How does execution of Process Control, wait, waitpid & exec Family transition between User Space and Kernel Space?
Explanation
User space transitions to kernel mode securely via software traps or SYSCALL instructions that switch processor rings to Ring 0.
Question 8 of 12
What happens to resources allocated by Process Control, wait, waitpid & exec Family when the owning process terminates?
Explanation
Process resources (open fds, memory maps) are automatically reclaimed by the kernel on termination, while kernel-persistent objects survive.
Question 9 of 12
In production Linux server environments, how is Process Control, wait, waitpid & exec Family optimized for high throughput?
Explanation
Linux optimizes throughput via zero-copy data paths, asynchronous kernel events, and scalable concurrency primitives.
Question 10 of 12
What standard governs the portability of Process Control, wait, waitpid & exec Family across Unix, Linux, and macOS platforms?
Explanation
IEEE POSIX standards define consistent APIs across Unix-like platforms ensuring portable source code execution.
Question 11 of 12
Which command-line diagnostic tool is most effective for inspecting and troubleshooting Process Control, wait, waitpid & exec Family at runtime?
Explanation
Utilities like strace, lsof, and the /proc filesystem provide direct visibility into kernel interactions and resource status.
Question 12 of 12
What is a critical production best practice when developing systems software involving Process Control, wait, waitpid & exec Family?
Explanation
Defensive programming requires explicit error checking on all system operations, proper resource cleanup, and avoiding memory corruption.
0 / 12 (0%)
Great job! Review the concepts above and proceed when ready.
ADVERTISEMENT
Responsive Ad Unit • Technical Courses & Certifications