Concurrency Problems
Published: 2023-04-13 15:46:21

Competition for resources and two common problems that arise due to competition in computer processes can be a bit challenging to comprehend. When two or more processes compete for the same resources, such as I/O equipment, memory, clock, and others, without any information exchange mechanism between the processes, it can cause problems. As a result, the rejected process will slow down, and sometimes, a blocked process may occur, never accessing the resource.

The problems that arise from competition in computer processes can be categorized into two types: the Producer-Consumer Problem and the Reader-Writer Problem. The Producer-Consumer Problem occurs when there are two or more processes interacting using the same buffer, with one process acting as a Producer (adding value to the buffer), and another process acting as a Consumer (taking/reducing the buffer's contents).

One type of issue that arises from competition among computer processes is the Producer-Consumer Problem, which occurs when two or more processes interact using the same buffer, with one process acting as a producer (adding values to the buffer) and another acting as a consumer (removing/reducing the buffer's contents). In order to overcome this problem, requirements such as regulating the critical section, limiting access to the same resource, and halting production or consumption when the buffer is full or empty, must be met.

Another type of issue that can arise from competition among computer processes is the Reader-Writer Problem, which occurs when multiple processes read (reader) the same data at the same time. To overcome this problem, requirements such as allowing two or more readers to read data simultaneously, preventing any reader from reading data while the writer is writing data, and ensuring that the writer waits for all readers to finish reading data, must be met. Techniques such as mutual exclusion, semaphore, monitor, and lock can be used to address competition among computer processes and overcome these issues.

Meanwhile, the Reader-Writer Problem occurs when several processes read (reader) the same data simultaneously. The requirements to overcome this problem are that two or more readers can read data together, no reader can read data when data is being written by the writer, and the writing process must wait for all readers who are reading data to finish reading.

To solve the problem of competition in computer processes, there are several techniques that can be used, such as mutual exclusion, semaphore, monitor, and lock. The mutual exclusion technique is used to limit access to the same resources. The semaphore is used for synchronization between processes, where there is a flag or token setting indicating whether the resource is being used or not. Monitors and locks are also used to ensure that accessing the same resources does not occur simultaneously.

## For solution
### 1. Mutual Exclusion 
condition where only one process or thread can access a particular resource at a given time. This is necessary to prevent errors when multiple processes or threads attempt to access the same resource simultaneously. For example, when two processes try to change the same data at the same time, it can cause damage to the data. To prevent this, only one process or thread can access the resource at a given time.

**Mutual Exclusion** can be handled using locking techniques. Locking allows processes or threads to hold a key or token to access a particular resource. If the resource is already locked by a certain process or thread, other processes or threads cannot access it until the key or token is released. This technique ensures that only one process or thread can access the resource at a given time, thereby preventing errors and data corruption.

### 2. Deadlock 
condition where two or more processes are waiting for each other to release the required resources. As a result, no process can complete its task, and the program gets stuck in a dead state. This can happen if each process holds the resource required by another process, so no one can release the resource. To avoid deadlock, the operating system can use algorithms to detect and prevent such conditions.

**Deadlock** can be handled by using algorithms to detect and prevent the condition. One commonly used algorithm is the Banker's Algorithm. This algorithm ensures that a process can only start if all the required resources are available and will not cause deadlock. Additionally, if a deadlock is detected, the operating system can force processes to release the resources they have taken or perform a system restart to avoid deadlock.


### 3. Starvation
condition where a process or thread cannot access the required resources to complete its task because the resources are always being used by other processes or threads. This can happen if the operating system does not prioritize certain processes or threads, allowing some to keep using the same resources while others do not have access to those resources. To avoid starvation, the operating system can use algorithms to ensure that each process or thread gets access to the resources it needs to complete its task.

**Starvation** can be handled using priority techniques. In an operating system, processes or threads can be prioritized based on their urgency or importance in completing a task. This technique ensures that the most important processes or threads get access to the resources they need to complete their tasks first, thereby preventing processes or threads from being constantly blocked and experiencing starvation.

### 4. Data coherence 
condition where the same data stored in multiple locations maintains the same integrity and consistency. In a multi-process or multi-thread environment, where several processes or threads can access the same data, data coherence is crucial to prevent conflicts and errors in data. The operating system uses data coherence techniques such as cache consistency protocols and time-based access control to ensure that all processes or threads always see the same version of the data they are using.

**Data coherency** can be handled using cache consistency protocols. These protocols ensure that every process or thread always sees the same version of the data they are using, even if the data is stored in multiple locations. Additionally, time-based access control can be used to ensure that every process or thread that needs access to the data gets access in the correct order, thereby preventing errors and conflicts on the data.