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

Process Relationships

UNIX processes are not isolated entities. Every process has relationships with other processes through parent-child hierarchies, process groups, and sessions. These relationships enable process management, job control, signal delivery, and terminal control.

Learning Objectives

  • Understand parent and child process relationships.
  • Learn process groups.
  • Understand sessions.
  • Learn session leaders.
  • Understand controlling terminals.

1. What are Process Relationships?

A process relationship defines how one process is associated with another process or with a collection of processes. UNIX organizes processes into parent-child hierarchies, process groups, and sessions to simplify resource management, signal handling, and job control.

2. Parent and Child Processes

When a process creates another process using fork(), the new process is called the child process, and the creating process is called the parent process. The child inherits many attributes from its parent, including open file descriptors and environment variables.

3. Parent-Child Hierarchy

init/systemd ↓ Parent Process ↓ Child Process ↓ Grandchild Process

Together, these relationships form a process tree.

4. Process Groups

A process group is a collection of one or more related processes that can receive signals as a unit. Each process group is identified by a unique Process Group ID (PGID).

5. Why Process Groups?

  • Support job control.
  • Deliver signals to multiple related processes.
  • Manage foreground and background jobs.
  • Organize cooperating processes.

6. Sessions

A session is a collection of one or more process groups. A session is typically created by a login process and represents a user login environment.

7. Process Relationship Hierarchy

Session ↓ Process Group ↓ Parent Process ↓ Child Process

8. Common System Calls

System Call Purpose
fork() Create a child process.
getpid() Get process ID.
getppid() Get parent process ID.
getpgrp() Get process group ID.
setsid() Create a new session.

9. Practical Uses of Process Relationships

  • Shell job control.
  • Terminal management.
  • Daemon creation.
  • Signal broadcasting.
  • Process monitoring tools.

10. Interview Questions

  1. What is a parent process?
  2. How is a child process created?
  3. What is a process group?
  4. What is a Process Group ID (PGID)?
  5. Why are process groups used?
  6. What is a session?
  7. How are sessions and process groups related?
  8. Which system call creates a new session?
  9. What is the purpose of getppid()?
  10. How do process relationships help with job control?

11. Session Leader

A session leader is the process that creates a new session by calling setsid(). The Process ID (PID) of the session leader becomes the Session ID (SID) for the newly created session.

12. Characteristics of a Session Leader

  • Creates a new session using setsid().
  • Becomes the leader of the new process group.
  • Its PID becomes the Session ID (SID).
  • Initially has no controlling terminal.
  • May later acquire a controlling terminal, depending on system behavior.

13. Process Group Leader

A process group leader is a process whose Process ID (PID) is equal to its Process Group ID (PGID). The process group leader represents the process group when signals are delivered to the entire group.

14. Process Group Example

Process Group (PGID = 3200) │ ├── Process 3200 (Leader) ├── Process 3205 ├── Process 3210 └── Process 3212

15. Controlling Terminal

A controlling terminal is the terminal associated with a session. It provides user input and displays program output for the foreground process group.

16. Foreground Process Group

The foreground process group is the process group that currently has access to the controlling terminal for interactive input. Keyboard-generated signals are typically delivered to the foreground process group.

17. Background Process Group

A background process group executes without controlling the terminal for interactive input. Background processes continue running while the foreground process group interacts with the user.

18. Session and Process Group Relationship

Session (SID) │ ├── Process Group 1 (Foreground) │ ├── Process A │ └── Process B │ └── Process Group 2 (Background) ├── Process C └── Process D

19. Foreground vs Background Process Groups

Foreground Background
Receives terminal input. Does not receive terminal input by default.
Receives keyboard-generated signals. May continue execution while another group is in the foreground.
Interacts directly with the user. Typically performs work without direct user interaction.

20. Interview Questions

  1. What is a session leader?
  2. What does setsid() do?
  3. How is the Session ID determined?
  4. What is a process group leader?
  5. When is a process considered the group leader?
  6. What is a controlling terminal?
  7. Which process group receives keyboard input?
  8. How are foreground and background process groups different?
  9. Can a session contain multiple process groups?
  10. Why are sessions required in UNIX?

21. setsid()

The setsid() system call creates a new session, creates a new process group, and makes the calling process both the session leader and the process group leader. The calling process must not already be a process group leader.

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

22. What Happens When setsid() is Called?

  • A new session is created.
  • A new process group is created.
  • The calling process becomes the session leader.
  • The calling process becomes the process group leader.
  • The new session initially has no controlling terminal.

23. getsid()

The getsid() system call returns the Session ID (SID) of a specified process. If the specified process belongs to the same session as the caller, its Session ID is returned.

#include <unistd.h> pid_t getsid(pid_t pid);

24. getpgrp()

The getpgrp() system call returns the Process Group ID (PGID) of the calling process.

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

25. setpgid()

The setpgid() system call assigns a process to an existing process group or creates a new process group if appropriate. It is commonly used by shells to organize jobs into separate process groups.

#include <unistd.h> int setpgid( pid_t pid, pid_t pgid );

26. Creating a New Session

Existing Session ↓ fork() ↓ Child Process ↓ setsid() ↓ New Session ↓ New Process Group ↓ Session Leader

27. Example: setsid()

#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t sid; sid = setsid(); if (sid == -1) { perror("setsid"); return 1; } printf("Session ID : %d\n", (int)sid); return 0; }

28. Example: getpgrp()

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

29. Process Group Management Workflow

Parent Process ↓ fork() ↓ Child Process ↓ setpgid() ↓ Assigned to Process Group ↓ Job Control

30. Interview Questions

  1. What is the purpose of setsid()?
  2. Why must a process not already be a process group leader before calling setsid()?
  3. What does getsid() return?
  4. How is getpgrp() different from getpid()?
  5. What is the purpose of setpgid()?
  6. Which process typically uses setpgid()?
  7. Why is a new process group useful?
  8. What happens immediately after a successful setsid() call?
  9. Can a session contain multiple process groups?
  10. Why are sessions important for job control?

31. Login Session

A login session begins when a user successfully logs into the operating system. The login program creates a new session, and the user's shell becomes the session leader. All commands started from that shell normally belong to the same session unless another session is explicitly created.

32. Login Session Structure

Login ↓ Shell (Session Leader) ↓ Foreground Process Group ↓ Command 1 ↓ Command 2 ↓ Command 3

33. Controlling Terminal (Detailed)

A controlling terminal is the terminal associated with a session. It provides interactive communication between the user and the foreground process group. The terminal driver manages keyboard input, terminal output, and the delivery of terminal-generated signals.

34. Terminal-Generated Signals

Signal Typical Keyboard Shortcut Purpose
SIGINT Ctrl + C Interrupt the foreground process.
SIGQUIT Ctrl + \ Terminate the foreground process and generate a core dump if applicable.
SIGTSTP Ctrl + Z Suspend the foreground process.

35. Signal Delivery to Process Groups

Keyboard ↓ Terminal Driver ↓ Foreground Process Group ↓ Process A ↓ Process B ↓ Process C

Signals generated by the terminal are delivered to the foreground process group rather than to a single process.

36. Foreground Jobs

  • Receive keyboard input.
  • Display output directly on the terminal.
  • Receive terminal-generated signals.
  • Interact directly with the user.

37. Background Jobs

  • Execute without occupying the foreground.
  • Continue running while other commands execute.
  • Do not normally receive interactive keyboard input.
  • Can later be moved to the foreground by the shell.

38. Job Control

Job control allows a shell to manage multiple jobs by placing them in foreground or background process groups. The shell uses process groups and sessions to start, suspend, resume, and terminate jobs.

39. Job Control Workflow

User Starts Command ↓ Shell ↓ New Process Group ↓ Foreground Execution ↓ Ctrl + Z ↓ Job Suspended ↓ bg ↓ Background Execution ↓ fg ↓ Foreground Execution

40. Interview Questions

  1. What is a login session?
  2. Which process normally becomes the session leader after login?
  3. What is the purpose of a controlling terminal?
  4. Which process group receives keyboard-generated signals?
  5. What does Ctrl + C generate?
  6. What does Ctrl + Z generate?
  7. What is the difference between a foreground job and a background job?
  8. What is job control?
  9. How does a shell manage foreground and background jobs?
  10. Why are process groups essential for job control?

41. Orphaned Process Group

An orphaned process group is a process group in which every member's parent process is either a member of the same process group or belongs to a different session. As a result, no process outside the group within the same session can manage or monitor the group's execution.

42. Creation of an Orphaned Process Group

Parent Process ↓ fork() ↓ Child Process ↓ New Process Group ↓ Parent Terminates ↓ Process Group Becomes Orphaned

43. Why are Orphaned Process Groups Important?

  • Prevent suspended jobs from remaining permanently stopped.
  • Allow the operating system to cleanly manage abandoned jobs.
  • Support reliable job control.
  • Ensure predictable signal handling.
  • Maintain process management consistency.

44. SIGHUP (Hangup Signal)

SIGHUP is traditionally generated when the controlling terminal is disconnected. It is also commonly used to notify processes that they should reload configuration files or perform cleanup before continuing or terminating, depending on the application's design.

45. Orphaned Process Groups and SIGHUP

If an orphaned process group contains one or more stopped processes, the operating system sends SIGHUP followed by SIGCONT to each process in the group. This allows the stopped processes to resume execution and respond to the hangup notification appropriately.

46. Process Hierarchy

systemd / init ↓ Login Process ↓ Shell ↓ Editor ↓ Compiler ↓ Executable Program

Every process in the system belongs to a process hierarchy that begins with the system's initial process.

47. Relationship with Daemon Processes

Daemon processes typically detach from the controlling terminal by creating a new session. This separation allows them to continue running independently of any user login session.

48. Overall Process Relationship Structure

Session │ ├── Process Group │ ├── Parent Process │ ├── Child Process │ └── Grandchild Process │ └── Process Group ├── Background Process └── Worker Process

49. Practical Applications

  • UNIX shells.
  • Terminal emulators.
  • Background services.
  • Daemon processes.
  • System monitoring utilities.
  • Remote login services.

50. Interview Questions

  1. What is an orphaned process group?
  2. How does an orphaned process group differ from an orphan process?
  3. When is SIGHUP generated?
  4. Why are SIGHUP and SIGCONT sent together to certain orphaned process groups?
  5. Why is process hierarchy important?
  6. Which process is typically at the root of the process hierarchy?
  7. Why do daemon processes create new sessions?
  8. How are sessions, process groups, and processes related?
  9. Can a session contain multiple process groups?
  10. How does job control depend on process relationships?

51. Example: Parent and Child Process

#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; pid = fork(); if (pid == 0) { printf("Child Process\n"); printf("PID : %d\n", getpid()); printf("PPID : %d\n", getppid()); } else { printf("Parent Process\n"); printf("PID : %d\n", getpid()); printf("Child PID : %d\n", pid); } return 0; }

52. Example: Display Process Group

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

53. Useful Linux Commands

Command Purpose
ps -ef Display running processes.
ps -j Display process groups and sessions.
pstree Display the process hierarchy.
jobs Show jobs managed by the current shell.
fg Move a background job to the foreground.
bg Resume a stopped job in the background.
setsid command Start a command in a new session.

54. Example Output of ps -j

PID PGID SID TTY CMD 2201 2201 2201 pts/0 bash 2240 2240 2201 pts/0 vim 2305 2305 2201 pts/0 gcc 2318 2305 2201 pts/0 a.out

The output shows the relationship between the Process ID (PID), Process Group ID (PGID), and Session ID (SID).

55. Real-World Applications

  • Linux shell job control.
  • Terminal emulators.
  • SSH login sessions.
  • Background services.
  • Daemon processes.
  • Process monitoring tools.
  • Interactive command-line applications.

56. Best Practices

  • Always check the return values of process-management system calls.
  • Understand process groups before implementing job control.
  • Use setsid() only when creating a new session is required.
  • Keep foreground and background jobs logically separated.
  • Handle terminal-generated signals appropriately.
  • Use standard utilities to inspect process relationships during debugging.

58. Advanced Interview Questions

  1. Explain the relationship between PID, PGID, and SID.
  2. Why can't a process group leader successfully call setsid()?
  3. How does a shell implement foreground and background jobs?
  4. What happens when Ctrl + C is pressed?
  5. Differentiate a process, a process group, and a session.
  6. How does setpgid() help implement job control?
  7. Explain an orphaned process group.
  8. Why are SIGHUP and SIGCONT sent together to certain orphaned process groups?
  9. How can the ps -j command help debug process relationships?
  10. Why are sessions essential for daemon processes?

59. Key Takeaways

  • Every process belongs to a process hierarchy.
  • Related processes are organized into process groups.
  • One or more process groups form a session.
  • Sessions enable job control and terminal management.
  • Foreground process groups receive terminal-generated signals.
  • setsid() creates a new session.
  • setpgid() organizes processes into process groups.
  • Job control depends on sessions, process groups, and controlling terminals.
Knowledge Check

Concept Mastery Assessment — Module 27

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 Relationships, Process Groups & Sessions in operating systems?
Explanation
Process Relationships, Process Groups & Sessions is designed to manage terminal logins, process groups (pgid), controlling terminals, sessions (sid), setsid(), and job control signal delivery. within the operating system architecture.
Question 2 of 12
Which system interface or data structure is centrally associated with Process Relationships, Process Groups & Sessions?
Explanation
Under POSIX and Linux, Process Relationships, Process Groups & Sessions operates through standardized system calls and kernel control structures.
Question 3 of 12
What safety or concurrency constraint must developers consider when implementing Process Relationships, Process Groups & Sessions?
Explanation
Proper synchronization, avoidance of race conditions, and handling reentrant execution are critical when working with Process Relationships, Process Groups & Sessions.
Question 4 of 12
When a system call related to Process Relationships, Process Groups & Sessions 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 Relationships, Process Groups & Sessions?
Explanation
Operations involving Process Relationships, Process Groups & Sessions balance performance against kernel transition, memory bandwidth, and cache locality tradeoffs.
Question 6 of 12
What security principle governs access to resources in Process Relationships, Process Groups & Sessions?
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 Relationships, Process Groups & Sessions 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 Relationships, Process Groups & Sessions 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 Relationships, Process Groups & Sessions 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 Relationships, Process Groups & Sessions 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 Relationships, Process Groups & Sessions 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 Relationships, Process Groups & Sessions?
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