#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;while (true) { /* produce an item in next produced */
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}Mutual Exclusion - If process P i 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 there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely
Bounded Waiting - A bound must exist 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
Assume that the load and store machine-language instructions are atomic; that is, cannot be interrupted
The two processes share two variables:
flag[i] = true implies that process P_i is ready! When P_i is preempted, we denote P_j for other process, j=1-i.Peterson’s solution is not guaranteed to work on modern computer architectures for the primary reason that, to improve system performance, processors and/or compilers may reorder read and write operations that have no dependencies.
As an example, consider the following data that are shared between two threads:
increment(&sequence);acquire() { while (!available)
; /* busy wait */
available = false;;
}
+ release() {
available = true;
}wait() and signal(), originally called P() and V()
Definition of the wait() operation
Mutex locks suffers from busy waiting and wait() and signal() semaphore operations has the same problem.
To overcome this problem, we can modify the definition of the wait() and signal() operations as follows:
Adapted from “Operating System Concepts”, 10th Edition; Abraham Silberschatz, Peter B. Galvin,Greg Gagne; 2018