[Linux] A brief discussion of semaphores


tips: system V is a set of standards. Shared memory, semaphores, and message queues belong to system V.

1. Disadvantages of shared memory

Insert image description here

When process A and process B communicate, if process A writes "Hello World" to the shared area of ​​physical memory, but when process A writes "Hello", process B reads it from the memory, so only "Hello" is read, which leads to the incomplete reading of the information that process A wants to send to process B, resulting in incomplete data!

Because shared memory does not have any protection mechanism! !

Unlike pipes, pipes have synchronization and mutual exclusion issues that can resolve data inconsistencies.

In order to solve the above problems, several concepts are introduced first.

Introduction of new concepts

  • 1. When A and B see the same resource, this resource is called a shared resource. If it is not protected, it may lead to data inconsistency problem< a i=2>.
  • 2. Only one execution flow is allowed to access the shared resource, then this resource has the mutual exclusion function.
  • 3. If a resource is only allowed to be accessed by one execution flow at any time, this resource is called critical resource. The critical resource is generally memory. For example, pipelines are also critical resources.
  • 4. If there are 100 lines of code, we are implementing inter-process communication through pipes at this time, and the way we access the pipe is through code, and the pipe is a critical resource, so those who access the pipe Lines of code, 5 to 10 lines of code, are accessing critical resources, and this part of the code that accesses critical resources is called critical section.

Why is the printed content messed up when printing concurrently with multiple processes and threads? ?

If multiple processes want to print information to the same monitor, they must first ensure that all processes see the same resource. The monitor is also a file and a shared resource. Since there is no protection, while process 1 is printing, process 2 is also printing to the monitor file, causing confusion.

2. Understanding semaphores

The essence of a semaphore is a counter, similar to but not equal to int count = n;

It describes the critical amount of resources! ! !

When we go to the movies, we have to buy tickets before we see them. The essence of buying tickets is a reservation mechanism for resources.
Then the ticket counter will be -1 for every ticket sold, which means that there will be one less resource in the cinema!
When the ticket counter reaches 0, the cinema's resources have been applied for.

Corresponds to critical resources (only one execution flow can access the resource):
The most worrying situation is:

  • A critical resource is accessed by multiple execution flows.
  • If n critical resources are accessed by > n execution flows, the first situation will occur.

So in order to solve this problem, there is the concept of counter.
In the management of the operating system, critical resources are limited. When an execution flow applies to access resources, the operating system will allocate critical resources to the execution flow through an allocation algorithm to ensure different execution flows. Access different critical resources. And when the critical resource counter = 0, the critical resource has been applied for, and if there is an execution flow that wants to apply for critical resources, the operating system will prohibit the execution flow from applying for resources.

Insert image description here

Therefore, the mutual exclusion of pipelines can be explained as: only one execution flow is accessing critical resources, which is called mutual exclusion!

in conclusion:

  • 1. The application for the counter is successful, which means that I currently have permission to access the resource.
  • 2. After applying for counter resources, I don’t need to access the resources I want now, because the counter resource I applied for at this time is a reservation mechanism for resources.
  • 3. The counter can effectively guarantee the number of execution flows entering critical resources.
  • 4. Each execution flow that wants to access critical resources must first apply for counter resources. Programmers call this counter resource a semaphore.
  • 5. We call a counter with only two states of 0 and 1 a binary counter; it is essentially a lock.

Then why should the counter value be 1?
When we look at the critical resource as a whole, only one execution flow can apply for the critical resource. Apply as a whole, release as a whole! This is mutual exclusion! ! !


However, to access critical resources, first apply for counter resources.

Counter resources exist to protect critical resources that can only be accessed by one execution flow.

Counter resources are also shared resources because they can be seen by multiple execution flows and can be requested by multiple execution flows. If you want to protect others, you must first protect yourself! ! !

However, counters themselves are not secure.

int count = 1;
count--;

Among themcount-- this statement is just one code in C language, but at the assembly level, it will be translated into 3 statements:
1. Copy the contents of the count variable from the memory to the CPU register
2. Perform count in the CPU –
3. Copy the calculation result back to count The memory location of the variable.

The specific reasons why it is unsafe will be discussed later.

atomicity

When we apply for semaphore resources, we essentially apply the counter –. This operation is called P operation.
When releasing the semaphore resource, it is essentially a counter ++. This operation is called a V operation.

Therefore, the operations of applying for and releasing the semaphore are called PV operations. This operation is calledatomic! ! !

The concept of atomicity is simply understood as: either don't do it, or do it as soon as you do it. There is no concept of doing it! ! !

Why are PV operations called atomic?

PV operation is essentially an operation on counters –, ++.
After translated into assembly statements, each assembly statement is atomic! ! !

Because an assembly statement can only mean that it is either not executed, or it has been executed, and there is no saying that it is being executed!

Guess you like

Origin blog.csdn.net/w2915w/article/details/134878853