"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet or .../gentleliu, the article is for learning communication only, do not use for commercial purposes"When the operating system enters the multi-channel batch processing system era. There are multiple tasks in a system, and each task is dispatched according to certain algorithms to use shared resources such as memory and CPU.
When one of the tasks waits for another resource, the task is able to sleep temporarily, and the operating system dispatches another task to continue running the amount, which maximizes the utilization of the system resources. Instead of just running the next task after a task has been completed, as was done with a single-channel batch system.
However, the problem of multitasking concurrency is also introduced.
Concurrency is multiple tasks at the same time, in today's general large application system, a function basically has a number of tasks together, each task coordination. cooperate with each other and exchange information. In this way, we need to consider the synchronization and mutual exclusion of concurrent tasks. The so-called synchronization, is one thing need to follow the order to complete, when a task and there is a task to communicate, task A gets the information of a task B, when the task B does not return information, task A continues to wait until B returns information back, A and then continue to run.
Async is a concept that is relative to synchronization, when task A requests information from B. You do not have to wait for the B information to return, and a request is completed to do the next thing directly. The so-called mutual exclusion is that some resources can only be occupied by one task at a time. In the case where a resource is occupied by task A. Other Task B that needs to occupy the resource must wait for task B to use the resource after it is freed after the resource is released. Typically this resource is called a critical resource, and sometimes a program does not agree to run concurrently. This procedure is called the critical section.
To resolve synchronization mutual exclusion issues.The basic thing is to figure out the synchronization relationship between the activists,There are somethe problem of the mutual exclusion of variables in the problem. Let's take a look at producer consumer issues. Producer Consumer problem is a classic process synchronization problem. It describes: There is a group of producer processes in the production of products, and this product to the consumer process to consume. To enable producer and consumer processes to execute concurrently, a buffer pool with buffers is set between them. The producer process can put the products it produces into a buffer, and the consumer process can get a product consumption from one buffer. Although all of the producer and consumer processes are executed in an asynchronous manner. But they must be kept in sync. That is, do not agree with the consumer process to an empty buffer to fetch products, also do not agree with the producer process to a full product buffer to run the product. Let's describe this problem in this way, if there are n buffers in the buffer pool. Each buffer holds a message that the producer and consumer processes (or threads) are mutually exclusive to the buffer. Only if the buffer pool is not full, the producer can feed the message to the buffer pool, and the consumer can take a message from the buffer pool only if the buffer pool is not empty. At this point producers and consumers need to stay in sync. When the buffer pool is empty, the producer notifies the consumer not to fetch the data again. Notifies consumers of the ability to fetch data when it is not empty.
In Linux, there are many ways to achieve synchronization and mutual exclusion, such as semaphores, threads also have dedicated threads to repel locks and conditional variables. This series of articles will analyze and demonstrate examples of various implementations of synchronous mutual exclusion, and most of the demo examples are based on the producer consumer model.
Linux synchronization and Mutual exclusion applications (0): Basic concepts