previous | start | next

shell race condition

Problem:

  1. The shell forks a child process to execute a program.
  2. The child begins execution of the program.
  3. Meanwhile, the shell adds the child process to the job table.
  4. When the child terminates, the shell's SIGCHLD handler removes the child process from the job table.

A problem occurs if the child execution is so short that it finishes before the shell can add it to the job table.

That is step 4 occurs before step 3 and so the shell would try to remove a process from the table before it is there - fails.

The step 3 occurs and the shell tries to add the job that has already terminated.

This is a "race" between the child and the shell. The job table can become corrputed if the child wins the race.

The solution:

Now the child will be guaranteed to be in the table BEFORE the shell tries to remove it.



previous | start | next