Process Synchronization Concepts and Communication Models
Basic Concepts of Mutual Exclusion and Synchronization
- Mutual Exclusion: Mutual exclusion refers to the condition where only one process is allowed to access a resource at any given time. This type of access possesses uniqueness and exclusivity.
- Synchronization: Process synchronization refers to logical interdependencies between certain processes. In other words, several processes cooperate with each other to complete a common task. Since every cooperating process advances independently and unpredictably in speed, it is necessary for the cooperating processes to coordinate their work at specific coordination points.
Critical Resources and Critical Sections
- Critical Resource: A resource that can be used by only one process during a certain period of time is called a critical resource.
- Critical Section: The segment of code within a process that accesses a critical resource is referred to as the critical section.
Semaphore Mechanism / PV Primitives
The semaphore mechanism is a method used for synchronization and mutual exclusion in concurrent programming. It was introduced by Dutch computer scientist Edsger Dijkstra in 1965 and serves as a means of controlling access when multiple processes or threads share resources. The semaphore mechanism typically employs two basic operations: P (also known as Wait or Down) and V (also known as Signal or Up).
The PV primitives are the two atomic operations used to manipulate semaphores, where the P operation is used to acquire (decrement) the value of a semaphore, and the V operation is used to release (increment) the value of a semaphore.
P Operation (Wait Operation):
- If the value of the semaphore is greater than 0, decrement the value by 1, and continue program execution.
- If the value of the semaphore equals 0, the process (or thread) becomes blocked and waits for the value to become greater than 0.
V Operation (Signal Operation):
- If there is a process (or thread) that has been blocked waiting for the semaphore, wake up one of the blocked processes (or threads).
- If no process (or thread) is blocked waiting for the semaphore, increment the value by 1.
The primary purpose of the semaphore mechanism is to achieve mutual exclusive access and synchronized operations on shared resources. By using P and V operations to lock and unlock semaphores, it ensures that only one process (or thread) can access a shared resource at any given time, thereby preventing race conditions. The semaphore mechanism can also be used to solve classic concurrent programming problems such as the producer-consumer problem, readers-writers problem, and dining philosophers problem. By reasonably using semaphores, coordination between threads and reasonable allocation of resources can be achieved, ensuring system correctness and stability.
It should be noted that the semaphore mechanism provides only a low-level synchronization primitive; improper use may lead to issues such as deadlock or livelock. Therefore, in practical applications, higher-level synchronization mechanisms (such as mutex locks or condition variables) are often used to build more reliable and safe concurrent programs.
Monitors
A monitor is an advanced synchronization mechanism designed to solve problems related to synchronization and mutual exclusion in concurrent programming. It was proposed by Dutch computer scientist C.A.R. Hoare in 1974 and represents an abstract data type that encapsulates shared variables and operations.
Monitors provide a structured way of managing shared resources, consisting of a set of data and a set of operations (also known as methods or procedures). Inside a monitor, only one process (or thread) can execute the operations; other processes (or threads) must wait. This ensures that access to shared resources is mutual exclusive, preventing race conditions from occurring.
The main characteristics of monitors include:
- Mutual Exclusion: Operations inside a monitor are mutually exclusive; at any given time, only one thread can enter the monitor to execute operations. This avoids data inconsistency issues caused by multiple threads simultaneously accessing shared resources.
- Condition Variables: Monitors can define one or more condition variables for waiting and signaling between threads. Threads can transition into a waiting state via wait operations on condition variables until they are woken up by signals (such as signal) issued by other threads.
- Mutex Locks: To ensure mutual exclusive access to shared resources, monitors typically use mutex locks to protect shared variables. Mutex locks guarantee that only one thread can obtain access permissions for a shared variable at any given time.
- Data Abstraction: Monitors provide high-level abstraction by encapsulating shared resources and operations, making concurrent programming easier to understand and write. They hide underlying synchronization details, allowing programmers to focus on the logic of the problem without worrying about concurrency issues.
The use of monitors can simplify the design and implementation of concurrent programs, improving code readability and maintainability. It has been widely adopted and supported in many programming languages and operating systems, such as the synchronized keyword in Java and the std::mutex class in C++.
It should be noted that monitors are not a universal solution; they apply to specific synchronization problems. In some cases, other synchronization mechanisms (such as semaphores or read-write locks) may be more suitable for solving particular concurrency issues. Therefore, when selecting a synchronization mechanism, trade-offs must be made based on the specific situation and requirements.
Process Communication
Concepts
Synchronization and communication are two fundamental requirements for interaction between concurrent processes. Process synchronization primarily addresses critical section problems, while process communication mainly refers to information exchange between processes.
Methods of Process Communication
- Low-level Communication
- Semaphore Mechanism / PV Primitives
- High-level Communication
- Shared Memory Communication Method
- Message Passing Communication Method
- Buffer-based Message Communication
- Mailbox-based Communication
- File Sharing Communication Method
- Pipe-based Communication