Critical section

In computer science, a critical section (English, critical section ' ) to identify a collection of program instructions for the purpose of flow control used. In it may be present at a time only one process / thread, similar to a railroad crossing, which can only travel along the rail vehicle or road vehicle only, but not both types of vehicles simultaneously.

Critical sections consist of several individual statements whose intermediate results represent inconsistent states, on the other threads may not access. The result of a critical section may be visible only as an indivisible unit to the outside.

Requires this concept is to ensure the consistency of the states of resources, eg, data structures, connections, devices, etc., as well as database contents. In the latter case, go to the concepts in the transaction processing.

Examples

In the very simple example, A is to be counted in the shared variable count the frequency of entry into the program section. It is assumed that access to count is only possible by means of a read or a write operation, not by a house of indivisible increment operation ( increment: increase by one ).

The program section is:

Now this section of the program will run two threads temporally entangled. The temporal interleaving is effected by the operating system. The threads have by default no influence. Both threads are independent of each to the variable count to, process and change it:

1:

2:

3:

4:

5:

6:

Prior to the step 5 of the same original value of the variable is read count of two threads. The threads have this value as a private copy. In steps 3 and 4 both threads add each one to its private copy. In steps 5 and 6 to save the threads then count the new value from their private copies back into the variable. In the described scenario, it is thread Y whose write operation is performed as a last resort. It produces the wrong result 1, does not correspond to the task.

Example B Example A was extended by a second critical section, the same variable count is now decremented to and in relation to the first critical section. For consistency, must then be a maximum of staying at a time a thread in two critical sections.

Notation

In programming and modeling languages ​​can be found occasionally the directives:

  • Begin_CriticalSection
  • End_CriticalSection

Or

  • EnterCriticalSection ()
  • LeaveCriticalSection ()

Feature and treatment of critical sections

Be running processes or threads interlocked in parallel or in time and use them data ( storage areas ) or general resources (eg connections, devices) together, so there are resources that may be used by their nature only exclusive: during the execution of a program section of a thread in which the equipment is used changing, the resources for other threads must not be accessible. A program section in a thread in which you access to a shared, but exclusive -to-use program resources, is a critical section.

It is essential to prevent a time- crossed design of critical sections of parallel threads, as this will give unpredictable results or inconsistent states of the resources leads. However, it is not necessary to define a strict order of use of the equipment. It is sufficient to ensure mutual exclusion of accesses. If a thread in a critical section is in relation to a resource, no other thread may enter with respect to the same resource in a critical section. This is achieved by any sequencing the execution of critical sections of the accessing threads. This requirement is weaker than the request for the execution of a critical section Ununterbrechbarkeit which is often mentioned in the context of critical sections. If a critical section with respect to a resource performed, this is not only in favor of execution of a critical section ( of the same shared resource with respect to ) a different process to be interrupted.

To a solution to the problem of the critical section, the following requirements:

  • Mutual exclusion: For a piece of equipment may be located at any time at most one thread in a critical section.
  • Progress: A termination or a continuation of a process outside of a critical section may have no influence on the progress of the remaining processes.
  • Limited Waiting time: No thread may be excluded as long as desired from entering a critical section.
  • The solution must not make any assumption about the execution speed of threads.

By fulfilling these requirements, the consistency of the resources can be ensured. In addition, each thread comes in finite time in its critical section and is not generally prevented from entering its critical section.

207515
de