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
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
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
- What is a parent process?
- How is a child process created?
- What is a process group?
- What is a Process Group ID (PGID)?
- Why are process groups used?
- What is a session?
- How are sessions and process groups related?
- Which system call creates a new session?
- What is the purpose of
getppid()? - 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
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
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
- What is a session leader?
- What does
setsid()do? - How is the Session ID determined?
- What is a process group leader?
- When is a process considered the group leader?
- What is a controlling terminal?
- Which process group receives keyboard input?
- How are foreground and background process groups different?
- Can a session contain multiple process groups?
- 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.
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.
24. getpgrp()
The getpgrp() system call returns the Process Group ID (PGID) of the calling process.
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.
26. Creating a New Session
27. Example: setsid()
28. Example: getpgrp()
29. Process Group Management Workflow
30. Interview Questions
- What is the purpose of
setsid()? - Why must a process not already be a process group leader before calling
setsid()? - What does
getsid()return? - How is
getpgrp()different fromgetpid()? - What is the purpose of
setpgid()? - Which process typically uses
setpgid()? - Why is a new process group useful?
- What happens immediately after a successful
setsid()call? - Can a session contain multiple process groups?
- 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
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
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
40. Interview Questions
- What is a login session?
- Which process normally becomes the session leader after login?
- What is the purpose of a controlling terminal?
- Which process group receives keyboard-generated signals?
- What does
Ctrl + Cgenerate? - What does
Ctrl + Zgenerate? - What is the difference between a foreground job and a background job?
- What is job control?
- How does a shell manage foreground and background jobs?
- 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
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
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
49. Practical Applications
- UNIX shells.
- Terminal emulators.
- Background services.
- Daemon processes.
- System monitoring utilities.
- Remote login services.
50. Interview Questions
- What is an orphaned process group?
- How does an orphaned process group differ from an orphan process?
- When is
SIGHUPgenerated? - Why are
SIGHUPandSIGCONTsent together to certain orphaned process groups? - Why is process hierarchy important?
- Which process is typically at the root of the process hierarchy?
- Why do daemon processes create new sessions?
- How are sessions, process groups, and processes related?
- Can a session contain multiple process groups?
- How does job control depend on process relationships?
51. Example: Parent and Child Process
52. Example: Display Process Group
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
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
- Explain the relationship between PID, PGID, and SID.
- Why can't a process group leader successfully call
setsid()? - How does a shell implement foreground and background jobs?
- What happens when
Ctrl + Cis pressed? - Differentiate a process, a process group, and a session.
- How does
setpgid()help implement job control? - Explain an orphaned process group.
- Why are
SIGHUPandSIGCONTsent together to certain orphaned process groups? - How can the
ps -jcommand help debug process relationships? - 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.
Concept Mastery Assessment — Module 27
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.