Critical-Section Problem

Race Condition

Several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place.

To guard against the race condition above, we need to ensure that only one process at a time can be manipulating the variable counter. To make such a guarantee, we require that the processes be synchronized in some way.

Critical Section

The critical-section problem is to design a protocol that the processes can use to cooperate. Each process must request permission to enter its critical section. The section of code implementing this request is the entry section. The critical section may be followed by an exit section. The remaining code is the remainder section.

Solution

A solution to the critical-section problem must satisfy the following three requirements:

  • Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections.

  • Progress - If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in deciding which will enter its critical section next, and this selection cannot be postponed indefinitely.

  • Bounded Waiting - There exists a bound, or limit, on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.

Two general approaches are used to handle critical sections in operating systems: preemptive kernels and nonpreemptive kernels.

A preemptive kernel allows a process to be preempted while it is running in kernel mode. A nonpreemptive kernel does not allow a process running in kernel mode to be preempted; a kernel-mode process will run until it exits kernel mode, blocks, or voluntarily yields control of the CPU.

Producer / Consumer Example

What if the processing you wish to perform with mutal exclusion needs to occur only under certain condidtions?

Condition Variable

  • Used in conjunction with mutex

Wait(mutex, cond)

  • mutex is automatically released & required on wait

Signal(cond)

  • notify only one thread waiting on condition

Broadcast(cond)

  • notify all waiting threads

results matching ""

    No results matching ""