Threads and Processes


UNIX processes are designed to support a multi-user computing environment. In particular they provide address spaces, open file tables, etc. on a per/process basis. Support, through mechanisms such as pipes, is also provided for cooperating processes that support a single application.

It quickly becomes clear that cooperating processes are not an efficient way to support applications. For example, processes supporting single applications usually share common files. Moreover, communications between processes could be facilitated if processes shared memory.

Threads can be though of a processes that share common file tables and address spaces. A corrollary is that Threads are easier to set up than processes.


A difficulty with shared resources:

Consider the the following version of the update synchronization problem. A banks account update application has two threads.

In both cases the update consists of retreaving the present balance, adding the deposit or subtracting the withdrawn, finally writing the new balance back into the database. Now consider the following sequence of events.

  1. Teller retrieves the present balance.
  2. Teller times out and Electronic begins to run.
  3. Electronic retrieves the present balance.(the same)
  4. Electronic updates the balance, stores it, and exits.
  5. Teller starts up, updates the balance, stores it, and exits.

Problem - Electronic's update is lost.