Process synchronization in the operating system

xiaoxiao2021-03-05  23

Process synchronization includes two aspects of the mutual exclusion and process of the process, which is a means of operating system management shared resources. From the examination, many candidates have a bad understanding of this part of knowledge. First, you should first determine the problem with the process synchronization problem, which is a process mutual exclusion or a process synchronization, or a mixing problem of mutual exclusion and synchronization. Then, according to the number of shared resources and the correct definition semaphore and its initial values ​​are correctly defined according to the rules of the shared resource. Then determine the P operations and V operations that should be implemented on different semaphors, with these P operations and V operations to ensure that the concurrent process uses the shared resources correctly. Below we have made some analysis, let candidates understand the process synchronization in the analysis, master the process synchronization.

Realize the mutual exclusion of the process with PV operation

Implementing the mutual exclusion of the process with a PV operation, as long as a semaphore is associated with a set of associated critical regions, the initial value of the semaphore is "1". Each process is to invoke a P operation before entering the critical zone, and test whether you can immediately enter the critical area; after performing the process of the inconvenience area, call V operation means you withdraw from the critical area. Example: The observer and reporter are two concurrent execution processes, and the observer is constantly observing and the passing truck count, the reporter is scheduled to print the observer's count value, and the two processes are executed by the procedures are as follows: BeginCount: Integer COUNT: = 0; CobeginProcess ObserverBeginl1: Observe A Car; / * Observed a truck * / count: = count 1; goto L1nd; Process Reporterbeginl2: print count; count: = 0; goto l2nd; Use PV operations to manage it without generating errors related to time.

Analysis: The observer and the reporter share the variable count, the critical area of ​​the observer process is "count: = count 1", the critical area of ​​the reporter's process is "Print Count; count: = 0". The execution of the critical area is not limited to the execution of the critical regions. When they are executed, the shared variable counT will appear as follows: count: = count 1; print count; count: = 0; print count; count; count : = Count 1; count: = 0; print count; count: = 0; count: = count 1; When executed by first, two sequences, the number of trucks observed in a period of time Print it correctly and then restart the count. When executed according to the second sequence, after the number of trucks observed in a certain period of time, the observer observed that there was a truck through the count 1, but because of the next execution is count: = 0, The observer will count from "0", so the sum of the printed counT value will be less than the actual number of trucks. This error-related error is because they are executed in the critical area, and the two processes have alternately accessed the shared variable COUNT. When managing with PV operations. Just ensure that the mutual exclusion enters the critical region. Solution: BeginCount: Integer; s: Semaphore; count: = 0; s: = 1; CobeginProcess ObserverBeginl1: Observe A Car; / * Watch a truck * / P (s); count: = count 1; v (V) s); goto L1END; Process ReporterBeginl2: p (s); print count; count: = 0; v (s); goto l2nd; coEnd; end; synchronization of processes with PV operation

A set of sematics should be defined when implementing the synchronization of the process with a PV operation, where each semaphore corresponds to a message, and determines the initial value according to the physical integration of each message amount. By calling the P operation to measure whether the message you need is reached, send the message required by other processes by calling V operation. Example: Three processes A, B, C, share two buffers B1 and B2. The N pieces of products can be stored in the buffer B1, and M pieces can be stored in the buffer B2. Process A Each time a product is produced and stored in the buffer B1; the process B receives it from the buffer B1 to the buffer B2, and the process C is from the buffer B2 each time. Take out a product to consume. To prevent the product from being stored in a full buffer, or take the product from the empty buffer, or repeat the product, try the mutual constraint between them. Analysis: Process B is transmitted to B2 after taking out the product from B1. Therefore, the process B is the consumer is also a producer. Process A and Process B shared buffer B1, which needs to be synchronized, and a pair of signals S1 and S2 should be defined to implement messaging between processes A and B. Process B and Process C Share buffer B2, or define a pair of semaphors S3 and S4 to achieve synchronization. Process B Take a product from the buffer B1 and should be called immediately, the notification process A adds a unit of a storage product in the buffer B1, since the capacity of the buffers B1 and B2 is not equal, the process B is The P operation should be invoked before the removed product is stored in the buffer B2 to test whether there is an empty cell in which the buffer B2 can store the product. It is not guaranteed that the correct stores and taking the product, the pointers R1, R2, T1, T2 can be used to point out the currently stored product and the relative position of the product, and modify the pointer value each time, each time, to point to The next relative position. Solution: Begin S1, S2, S3, S4: Semaphore; R1, R2, T1, T2: INTEGER; S1: = N; S2: = 0; S3: = m; S4: = 0; R1: = R2: = T1 : = T2: = 0; CobeginProcess Abegin L1: Produce A Product; / * Production of a product * / p (S1); B1 [R1]: = Poduct; R1: = (R1 1) mod N; v (S2 Goto L1END; Process Bbeginl2: P (S2); x: = B1 [T1]; T1: = (T1 1) MOD N; V (S1); P (S3); B2 [R2]: = x; R2: = (R2 1) MOD M; V (S4); Goto L2END; Process Cbegin L3: P (S4); Y: = B2 [T2]; T2: = (T2 1) MOD M; V (S3 ); Consume; / * Consumer product * / goto l3nd; COEND; END; implement process synchronization and mutual exclusion with PV operation

转载请注明原文地址:https://www.9cbs.com/read-35872.html

New Post(0)