Use of the signal lamp Concept
In multi-process applications, the coordination between processes is particularly strict. Signals can be used for synchronization or mutual exclusion between different modules, while signals are generally implemented using global variables. The traffic lights in the program simulate the traffic lights in the real world, and provide synchronous or mutually exclusive access to resources between processes. The logical structure of a typical traffic signal synchronization program is as follows:
First, two operations are required for semaphores, namely P and V. The procedure for P (s) operations is as follows:
A. s = S-1.
B. If s> = 0, the process that calls P (s) continues to run.
C. If s is <0, the Process calling P (s) is blocked and inserted into the blocking queue waiting for semaphore s.
The procedure of the V operation is the opposite of that of the P operation. The procedure of the V (s) operation is as follows:
A. s = s + 1.
B. If s> 0, the process that calls V (s) continues to run.
C. If s <= 0, wake up a process from the blocked queue of the waiting semaphore s, and then call the process of V (s) to continue execution.
1. Use semaphores to achieve mutex
First, define s as the public semaphores for mutual exclusion between two processes (generally defined as a global integer variable in PowerBuilder). The initial value is 1, indicating that operations of a mutex have not started. You only need to place mutually exclusive operations between P (s) and V (s) operations to Implement Asynchronous execution of two mutually exclusive operations. For example, you can make the following arrangements for process p1 and process p2 to achieve mutual exclusion:
Process p1
Process p2
P (s)
P (s)
S1 program segment
S2 program segment
V (s)
V (s)
Because the initial value of semaphores is 1, the semaphore of the first process P1 is 0 after the P operation is executed, and the P1 process can be executed normally. If the process P2 starts to execute at this time, after the P operation is executed, the semaphore is-1, so process P2 can only be in the waiting state. When process P1 completes program segment S1, the V operation is executed, and the semaphore changes to 0, it can wake up process p2 to start execution. Therefore, the two mutually exclusive program segments S1 and S2 are not executed at the same time, and the two processes P1 and P2 can work together well.
For example, to retrieve a database with mutual exclusion between two processes on a client, first define the global variable:
Int gi_message = 1
Write the following script in related events in the first window:
Gi_message = gi_message-1
Do While gi_message <0
Yield ()
Loop
Dw_1.settransobject (sqlca)
Dw_1.retrieve ()
Gi_message ++
Write the same script in the related events of the second window, so that the two windows can perform data retrieval with mutual exclusion.
2. Use semaphores for synchronization
The initial value of semaphore S is 0. The Synchronization Model between two processes is as follows:
Process p1
Process p2
L1: P (S)
L2: V (s)
Let process P1 reach L1 first. When it executes P (S), it makes S =-1, so process P1 enters the blocking status and enters the blocked queue of semaphores; then the P2 process reaches the L2 point. When it executes V (s), the s value is changed to 0, so the P1 process is awakened to continue the execution. It can be seen that when process P1 reaches L1, process P1 will be in the waiting state temporarily unless process P2 has passed L2. That is to say, P1 must be synchronized with process P2 at L1.
For example, you can improve closewithreturn so that non-response windows can pass parameters normally. The key lies in the use of semaphores so that window 1 enters the waiting state after window 2 is opened, and continues execution only after window 2 is closed and parameters are returned. First, define two global integer variables:
Int gi_message = 0 // semaphore
String gs_return // used for parameter passing
Compile the following script in the appropriate event in window 1 (for example, a clicked event with a key:
String ls_return
Open (w_test) // Open another window
Gi_message = gi_message-1 // The semaphore executes the P operation
Do While gi_message <0
Yield ()
Loop
Ls_Return = gs_Return
MessageBox ("prompt", ls_Return)
Write the following script in the Clicked event of the "close" button in the w_test window:
Gs_Return = "test"
Close (Parent)
Write the following script in the Close event of the w_test window:
Gi_message ++ // execute the V Operation