[previous] [index] [next]

Data Consistency Techniques

Any shared data structures that are not operated on as one piece by the CPU need to be protected by data consistency techniques. Here are the safe data types, from the Intel IA-32 manual [INT3]: So, if you're writing types char, int, and long int on anything from a 486 to a Pentium, you're OK. On Pentiums (not 486s), long long ints are OK also (e.g., the RTIME time count type in RTAI).

Numerous methods exists for ensuring data consistency. To motivate the discussion, consider this simple (and wrong) algorithm for allocating a token that signifies permission to modify shared data:

int token = 0;

A: if (token == 0) { /* no one has it */
B:   token = 1; /* now I have it */
     /* modify shared data here */
     token = 0; /* now I give it up */
   } else {
     /* I didn't get it; I'll try again */
   }
Why is this wrong?

Suppose token is 0 (unclaimed), and Process 1 (P1) executes line A and gets to line B. At this point, P1 can be swapped out, and P2 can run. Now, P2 executes its line A, sees that token is still 0, and gets to its line B. At this point, P1 and P2 will set token to 1, but it's too late: both think they have the token. This technique will not work.

Brief explanations of some techniques that do work are given below. See [TAN] or [DEI] for a textbook treatment.

Two-Process Mutual Exclusion: Dekker's- and Peterson's Algorithms

N-Process Mutual Exclusion using Hardware

N-Reader, 1-Writer Mutual Exclusion using Head/Tail Flags

Other Techniques

There are numerous other techniques for data consistency and mutual exclusion.
Next: Example 7, RTL Semaphores

Back: Example 6, Shared Memory