Shell Processes   «Prev  Next»
Lesson 5 Parent and child processes
Objective Relationship between Parent and Child Processes

Relationship between Parent and Child Processes

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):
  1. 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.
  2. 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)
  3. 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.

Running the ps command shows that you are running 1 process
1) Running the ps command shows that you are running 1 process

The diagram reflects the processes you have running
2) The diagram reflects the processes you have running

This is a child process under the Bourne shell in which it was created
3) This is a child process under the Bourne shell in which it was created

The diagram is updated to reflect the new process. It is a child of Bourne
4) The diagram is updated to reflect the new process. It is a child of Bourne

The C shell is now in the foreground and both the Korn and Bourne shells are in the background. In the listing above, the /bin/ksh process (PID 3723) was started from the /bin/sh process (PID 3707), so 3707 is listed as the PPID of 3723.  The  /bin/csh process (PID 3725) was started from the /bin/ksh process (PID 3723), so 3725 is listed as the PPID of 3725.
5) The C shell is now in the foreground and both the Korn and Bourne shells are in the background. In the listing above, the /bin/ksh process (PID 3723) was started from the /bin/sh process (PID 3707), so 3707 is listed as the PPID of 3723. The /bin/csh process (PID 3725) was started from the /bin/ksh process (PID 3723), so 3725 is listed as the PPID of 3725.

Diagram is updated to reflect new process
6) Diagram is updated to reflect new process

Quit the C shell using the exit command
7) Quit the C shell using the exit command


    Unix Shell Programming

    The ps –f command

    The command ps –f lists all of the processes you are currently running. The–f displays a full list of information. This information includes the PID of the parent process, which is referred to as the PPID in the ps command output. Look at the sample ps –f command below. It reflects the processes created in the series of images above. Connecting lines are included in the command below to point out the relationship between the PID and PPID numbers. These lines are not normally part of the output, they are just used here for clarification.
    The next lesson examines three different techniques for running a shell script.

    SEMrush Software 5 SEMrush Banner 5