Home > Backend Development > C++ > Process synchronization in C/C++

Process synchronization in C/C++

王林
Release: 2023-09-16 11:09:07
forward
1615 people have browsed it

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.

Critical section problem

Every process has a reserved code segment, called critical section. In this section, the process can change public variables, update tables, write to files, etc. The key thing to note about critical sections is that while one process is executing in its critical section, other processes cannot execute in their critical section. Every process must request permission before entering its critical section. The section of code that implements this request is entry section, the end of the code is exit section, and the remaining code is The remaining part.

Given below is the structure of the key part of a specific process P1

Process synchronization in C/C++

There are the following three Requirement: Critical Section

  • Mutual Exclusion must be satisfied - if one process assumes P1 to execute in its critical section, than any other process assumes P2 cannot execute in its critical section Critical section execution.
  • Progress - If no process is executing in its critical section, and there are processes that want to enter the critical section of its critical section, then only those processes that are not executing in the remaining part can request to enter the critical section, And the option can be postponed indefinitely.
  • Bounded Wait - In bounded wait, there is a limit to the number of times a process can enter its critical section after it issues a request to enter its critical section and before the request is granted. of.

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

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

  • Intturn;
  • Boolean flag[2];

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

Process synchronization in C/C++

Peterson's solution preserves all three conditions -

  • mutually exclusive − only one process at a time Critical sections can be accessed.
  • Progress − Processes outside the critical section will not prevent other processes from entering the critical section.
  • Bounded Wait - Each process has a chance to enter its critical section without waiting indefinitely.

Synchronization Hardware

has been implemented using two types of instructions -

  • Test and Set()
  • swap()

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()

Process synchronization in C/C++

Semaphore

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--;
}
Copy after login

Function of the Signal() function:

signal(S) {
   S++;
}
Copy after login

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

Process synchronization in C/C++

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template