The relationship between parent and child processes in Unix is hierarchical, with the parent process acting like the originator and the child process being its offspring. Here's a breakdown of this relationship:
Process Creation (Fork):
- The parent process creates a new child process using the `fork` system call. This essentially creates a copy of the parent process at that specific point in execution.
- The child process inherits most of the parent's attributes, including:
- Open file descriptors (allowing access to the same open files)
- Environment variables
- Working directory
- Signal handlers (functions to handle specific events)
- However, the child process has its own:
- Process ID (PID) - Unique identifier to distinguish it from the parent and other processes.
- Memory space - Separate memory allocation for the child process to execute its instructions.
- Program counter - Tracks the current execution point within the program code (important for continuing execution after the fork).
Process Execution:
- After the `fork` call, both the parent and child processes exist concurrently.
- They typically diverge in their execution:
- The parent process might continue executing the code after the `fork`.
- The child process often starts executing the same code from the point of the fork, but this can be altered.
- Shell scripts, for example, might use conditional statements to make the child process execute different code paths compared to the parent.
Process Termination:
- Both parent and child processes can terminate independently. They exit with a status code indicating success or failure (usually 0 for success).
- A child process can exit before its parent. In such cases, the kernel sends a notification (SIGCHLD signal) to the parent process to inform it about the child's termination. The parent process can then handle this signal appropriately, such as waiting for the child to exit and retrieving its exit status.
- Orphaned processes: If the parent process terminates before the child process, the init process (PID 1) becomes the adoptive parent of the orphaned child process, ensuring it has a parent to handle its termination.
Key Points:
- The parent process has more control over its child process (like sending signals), but they both run concurrently.
- They share some resources (open files) but have separate memory spaces and execution states.
- This parent-child relationship forms the foundation for multitasking in Unix-like systems, allowing multiple processes to run seemingly simultaneously.
Real-world Analogy:
Imagine a restaurant kitchen (parent process). The head chef (parent) creates a new recipe (child process) by copying an existing one (fork). The sous chef (child process) gets a copy of the recipe (inherits attributes) but can make slight modifications (different execution paths). Both chefs can cook their recipes concurrently (concurrent execution). When a dish is complete (process termination), the head chef might be notified (SIGCHLD signal) and check if everything went well (exit status).
When you run a program in your shell, a process is created. This new process is called a child process of the shell.
The originating process (the shell from which you ran the command) is called the parent process of the child.
When you run a new shell, you are creating a child process under the originating shell. Examine the SlideShow below to see an example of new processes being created as shells are run from the command line.