"Article Summary "
The timing of program processing is important for software that works together by multiple modules. When the order of message processing is confusing, the program will have an exception.
This article is based on the author's actual project experience. The timing problem between software modules is analyzed concretely, which provides a deliberate reference for the analysis and solution of related software problems.
"keywords "
C Language Timing Module Development message
I. Descriptive narrative of the problem
In a software version number, there are two modules (module A and Module B) to communicate.
After the communication link is set up. Module A sends a message to Module B, which carries the user number and serial number. After the message is sent successfully, module a stores the user number in a global variable according to the serial number.
Module B is after receiving a message from module A. Parse out the user number and do the related processing.
After processing is complete, module B returns the user number and serial number to module A as it is. Module A first finds the original sent user number based on the received serial number and is compared to the user number returned by the Received module B. If the user number is the same, it may be processed.
The communication between module A and Module B is seen in 1.
Figure 1 Communication between Module A and module B
When the software version number is self-measured, it is found that the user number sent by module A does not match the user number returned by Module B, so the process may not continue.
View the log for module A. The printed user number and the received user number are the same. The serial number is the same, but there is a problem when using the strncmp function (the user number variable is a string type).
Second, the program running process of module a
Because the user number of the comparison is completed in module A, so here is the main focus on module a program running process.
Module A of the program running flow 2 see:
Figure 2 Program run flow for module a
Can be seen from Figure 2. Timing is important when module A is interacting with module B in the process of message interaction. Assume that the order in which messages are processed is undefined. An abnormal termination of the module a program will occur.
three, the cause of the initial analysis
To determine whether the sending user number and the received user number are really different, we added a specific log before the program statement for the two numbers. You want to print out the sent user number, serial number, and received user number, serial number.
After the log is added, we run the program again, discovering that the received user number and serial number are correct, while the sent user number and serial number are printed with a null value.
How could that be? Did you not copy the user number and serial number to the global variable when you sent it?
We added a log after copying the program statement that sent the user number and serial number, and wanted to print out the value of the user number and serial number that was sent.
After running the program again. found that the user number and serial number sent are correct. It seems that the copy has succeeded.
Through the above analysis, can basically determine the copy and resolution are no problem. It may seem that the processing timing has caused a problem where the user number does not match.
iv. positioning of the problem
We looked at the logs in detail again and found a strange phenomenon, that is, the log is printed in the Receive module B returns the message time than the copy user number and serial number of the time earlier.
Why is it? We looked at the code in comparison. After sending the message to Module B, module A sleeps for a period of time before the message is copied. Module B Returns a message within a very short period of time, when module A is still dormant. There was no time to copy the message into the global variable.
Is module a The trouble of sleeping time? In order to verify our conjecture, we stared directly at the code that was running dormant to try it out. Once the program runs again, look at the logs and find everything is fine. No information is printed that the user number sent by module A does not match the user number returned by Module B, and the process of module a may be running.
It seems that this sleep time is in trouble.
It is true that module B returns a message in a very short time, so that when compared to a user number in a global variable, a user number of a string type is actually compared to a null value, and of course there is a failure.
This also coincides with the fact that the sent user number and serial number are printed with null values.
Now that the truth is in the white. We begin to change the code to keep module a dormant code until the code that copies the user number from the serial number in module A to a global variable is placed before the code that sends the message to Module B. Once again, the modified program was tested, and everything was OK.
v. Summary
In the process of this time series problem, mainly rely on the program log to locate the problem.
Through this issue to troubleshoot. We have summed up some of the following experiences:
(1) The detailed log helps to locate the problem. To get a clearer picture of where the problem appears. We can add some test logs to the key statements of the program. For analysis purposes.
(2) in the process of message processing order is very important, we must clarify the relationship between the operation of the program. Prevent the process from "offside" occurs.
(3) in the process of troubleshooting, do not leave no matter what a clue, to timely verify their ideas. A lot of programs to test.
There are bugs in the program, so let's not dwell on the problems that arise in the program. To master the method of troubleshooting, so talent enough to achieve "status quo". By solving the different problems. We have the ability to exercise, our development capacity will be improved.
(I Weibo: Http://weibo.com/zhouzxi?)
Topnav=1&wvr=5, No.: 245924426, welcome attention. )
The process of troubleshooting program timing problem in C language