Linux, as an open-source and robust operating system, supports multiple platforms and has a wide range of development tools and application software. In particular, it is very suitable for cutting its kernel.Embedded System. When the real-time and accurate data requirements are high, the system often causes data loss due to the process coordination problem during data collection and processing. The classic Process Synchronization model can be used to solveEmbedded Linux SystemSynchronization read/write conflicts.
Introduction
When processing real-time collected and updated data, the data update speed does not match the data processing speed. In this case, data loss may result in inaccurate data processing results and even unpredictable consequences. Therefore, a mechanism is required to coordinate data update and data processing, this ensures data integrity and accuracy of processing results. As a multi-task and multi-user operating system, Linux supports concurrent running of multiple processes in the system. Due to the dynamic characteristics of the process itself, it is very suitable to describe real-time data processing, the problem of synchronization and communication between Linux processes can solve the problem of real-time data processing.
In Linux, processes usually have four statuses: running (running), blocking (BLOCKED), ready (ready), and terminated. When multiple processes are executed concurrently, the competition between processes often occurs. We hope that processes can coordinate their actions so that a process can execute an action, that is, control synchronization, only when other processes are executed to a specific point. At the same time, when concurrent processes access Shared data, there should be no competition conditions. This is ensured by implementing mutex when accessing shared data, that is, data access synchronization.
The basic technology used to implement synchronization is to block a process until a specific condition is met. To implement data access synchronization, a process is blocked until another process completes access to shared data.
1. producer-consumer problem model of a finite-Length Buffer Zone
When only one producer and consumer exist, the production process and consumption process correspond to the same data structure and share the same data space. How to coordinate the production process and consumption process so that the data used by the consumption process every time is written by the new production personnel of the production process, in addition, the newly written data of the production process will not overwrite the data that has not been read and used by the consumption process, which is a key issue for model implementation.
In the producer-a-Consumer problem model, the producer process continuously produces products and puts them into the buffer zone. The consumer process continuously removes the products from the buffer zone for consumption. When the product in the buffer zone is full, it indicates that the production speed is higher than the consumption speed and the supply is oversupply. At this time, the producer must wait for the product to be consumed. When the buffer zone is empty, it indicates that the consumption speed is higher than the production speed, when supply is in short supply, the consumer process must wait for the production of the product. Production and consumption processes must run synchronously to achieve a balance between supply and demand.
Two common strategies are called strongreadersynchronization and strongwritersynchronization )". In strong reader synchronization, the reader is always given priority. As long as the writer does not perform write operations at present, the reader can gain access. In strong writer synchronization, the writer always obtains priority, as long as the strong reader does not perform read operations, the writer can gain access. Producer-consumer synchronization is different from pure read-write synchronization. Consumers can delete or destroy resources by accessing resources.
A producer-consumer problem model with a limited buffer zone is composed of several producer and consumer processes and a limited buffer pool. Each buffer zone can store one information record, and one producer produces one information record at a time. After a record is generated, the record will be written to the buffer zone after it enters an empty buffer separately. A consumer process consumes one information record at a time. When it needs to be consumed, it waits for a separate buffer to enter a full buffer and reads the records.
The above description shows that the solution to the producer-consumer problem model must meet the following conditions:
· The producer should not cover a full buffer;
· The consumer should not use an empty buffer;
· Producers and consumers should access the data buffer zone in mutex mode;
· Data must follow the FIFO mode;
· You cannot wait.
The Data Writing Process must avoid checking the buffer repeatedly until an empty buffer is found, and the read process must also avoid checking until a buffer is found to be full. This is equivalent to a busy wait in the system, which is difficult to avoid when only the critical segment (CS) algorithm is used for process synchronization.
To address the limitations of the problem model solution, the semaphore method is used to solve the process synchronization problem of real-time update data processing, that is, the above-mentioned producer-consumer problem model.
A semaphore is a non-negative shared integer and can only be used for initialization and non-negative operations. An operation that does not overlap with any other operations on data D. The operation P and V are defined as not split operations. The non-severability of P and V means that these operations cannot be executed concurrently, avoiding competition for semaphores. The operation semantics of P and V is defined:
According to the semantics defined above, for operations on a semaphores, P and V are the values of S, or a process that suspends or wakes up a P operation on S. The suspended process is blocked, thus avoiding the busy waiting problem. The semaphores of a binary system only take 0 and 1 for mutual exclusion.
In P and V Operations, blocking and awakening processes requires the participation of the process management component of the operating system. Therefore, semaphores are implemented by the operating system rather than applications.
Description of producer-1 consumer problem model:
2. Structure Design
The execution of the producer-consumer problem model for a limited buffer zone includes the following components: sharing data as a buffer zone group, operating on access to a buffer zone, and process as a producer-consumer.
In producer-to-consumer synchronization, resources are created by the producer. Unlike read programs, consumers can delete or destroy resources by accessing resources. Because producer and consumer processes share a buffer, they must be synchronized during entry insertion and deletion. Synchronization exceptions listed in table l must be avoided during implementation.
The traditional semaphore solution for a producer-consumer problem uses two semaphores to indicate the number of entries in the buffer zone and the number of idle slots respectively. When a process requires a specific type of resources, it can call a function to reduce the corresponding semaphore. Similarly, when a process releases resources, it can perform incremental operations on the corresponding semaphores through function calls. Since semaphores will never fall below zero, processes cannot use nonexistent resources. Therefore, the Count semaphore is always initialized to the number of available resources at the beginning.
Define the cyclic queue buffer to store the data to be processed. The console data processing process consumes data from the cyclic queue buffer and marks the data storage space as "obsolete ". The data collection and writing process can only store data in the cyclic queue buffer marked as "obsolete", as shown in 1.
If you do not have multiple producers or consumers, you do not need to lock the loop buffer. The producer is the only process that allows you to modify the write index and the position of the array to which the index points. As long as the writer saves the new value to the buffer before updating and writing the index, the reader will always see the consistent data structure. At the same time, the reader is the only process that can access the index and the data that the index points. As long as the two pointers do not overlap with each other, the producer and consumer can access the buffer zone when there is no actual state, as shown in figure 2.
For a single producer and consumer, the solution is implemented by using a fixed producer-consumer Problem Model Using semaphore.
The above uses semaphores to solve the priority buffer issue. The values of semaphores "empty" and "full" indicate the number of empty and full buffers, respectively, as shown in 3. The buffer pointers I and j are used to ensure that the buffer is provided and used in the FIFO order. As long as there are some full and empty buffers in the system, the data update process and data processing process can be executed without competing states concurrently. The authorARM embedded platform HHARM2410-R5The test case is successfully implemented according to the above scheme.
3. Discussion
The above structure design simplifies the producer and consumer into one. When multiple producers and consumers exist, you can design multiple counters to calculate the number of parallel readers and writer based on the solution mentioned above), the number of readers or waiting for readers (pre_reader), writers, or waiting for writers (pre_writer. The value of the counter is increased or decreased at the corresponding position in the process. Readers and writers must be blocked before being allowed to read and write data. This can be done through the P operation. When the reader or writer is blocked, the conditions for starting reading or writing are not met. These conditions change with the change of any counter value. Therefore, the process must perform the corresponding v operation after reading or writing.
When implementing a multi-read and multi-write Process Synchronization solution, we must avoid competition conditions for different counters.