Process synchronization is a technology that solves the problem of concurrent access to shared data, which may lead to data inconsistency. Collaborating processes refer to processes that can affect other processes or be affected by other processes, resulting in inconsistent process data. Therefore, process synchronization is required to ensure data consistency.
Given below is the structure of the key part of a specific process P1
There are the following three Requirement: Critical Section
There are two commonly used methods in operating systems to deal with critical parts.
Preemptible Kernel - The preemptible kernel allows a process to be preempted when the process is preempted. Runs in kernel mode.
Non-preemptive kernel - A non-preemptive kernel does not allow processes running in kernel mode to be preempted.
Peterson's solution is a classically based software solution to the critical section problem. It is limited to two processes that alternate between their critical and remaining parts. Peterson's part requires sharing two data items between the two processes, which is
here , the variable turn indicates whose turn it is to enter the critical section, and the flag array indicates whether the process is ready to enter the critical section.
If turn == i, it means that process Pi is allowed to enter its critical section.
If flag[j] is TRUE, it means that process j is ready to enter its critical section
The following is the structure of process P in the Peterson scheme
Peterson's solution preserves all three conditions -
has been implemented using two types of instructions -
Test and Set () are hardware solutions to solve synchronization problems. Among them, there is a shared variable shared by multiple processes called Lock, which can have a value of 0 and 1, where 1 means acquiring the lock and 0 means releasing the lock.
Whenever a process attempts to enter the critical section they need to query the lock value. If the value of lock is 1, then they must wait until the value of lock does not change to 0.
The following is the mutually exclusive implementation of TestAndSet()
The semaphore is A synchronization tool used to overcome problems caused by the TestAndSet() and Swap() instructions. The semaphore S is an integer variable that can be accessed through the two standard atomic operations of wait() and signal()
wait() function:
wait(S) { While S <= 0 ; // no operation S--; }
Function of the Signal() function:
signal(S) { S++; }
When a process is modifying the value of the semaphore, other processes cannot operate the same semaphore value at the same time.
The mutual exclusion implementation using semaphores is given below
The operating system uses two types of semaphores , they are:
Counting semaphore - The value of this type of semaphore can change within an unlimited domain
Binary semaphore - The value of this type of semaphore can vary between 0 and 1. They are also called mutex locks. It is used by the operating system to resolve critical section problems in multiple processes.
The above is the detailed content of Process synchronization in C/C++. For more information, please follow other related articles on the PHP Chinese website!