Perfect multi-thread programming solution and multi-thread programming Solution
This is a technical demonstration written last year to find a job: taking multi-thread brute force MD5 password cracking as an example to demonstrate a perfect multi-thread programming solution.
Ten years ago, I wrote a single-thread brute-force MD5 password cracking program (because at that time the CPU was still single-core). This time I made the original program multi-threaded.
This technical demonstration has written multiple versions of different implementation methods. It took two days (from January 1, May 3-20, 2014 to January 1, May 4 ).
The brute force password cracking principle is not the focus of this article. I will not detail it here.
For example, if the character set is a-z 26 lowercase letters and the length is 8 characters, a, B ,... z, aa, AB ,... zy, zz, aaa ,... aaaaaaaa, aaaaaaab ,... zzzzzzzy and zzzzzzzz, these 26 lowercase letters with a length of less than 8 characters, all combinations are tried again, so they are called brute force cracking. If the original encrypted password is within this range, it must be found. If the original encrypted password is not in this range, all attempts within the range may fail to be found. The attempt is to encrypt each attempt string generated by yourself in the same encryption mode as the encrypted password, and then compare the encrypted attempt string with the encrypted password, if they are the same, they are found.
This type of brute-force cracking requires a large amount of CPU computing. The scenario where the CPU is required for ultra-large computing is a very important application scenario of multithreading technology.
We use instances to form some concepts about brute-force cracking calculation:
To try a combination of 26 lower-case letters:
After the test, you must calculate and compare the 26 types of test strings (a-z.
After the test, you must calculate and compare the 26 + 26*26 = 702 types of test strings.
After an 8-digit test, you need to calculate and compare the 8-power + 26's 7-power +... + 26 = 217.18 billion types of trial strings.
It is estimated that it will take more than four hours to run 3.4 cores with four threads (the number of threads exceeds the actual number of CPU cores, and the speed will not increase. It may take more than 16 hours for a single thread to run 217.18 billion.
If you are interested, you can download the program included in this article and try it by yourself.
We can see how meaningful the use of multithreading technology is in the scenario where the CPU is required for ultra-large computing.
Multi-threaded access and processing of the same data source, a very important issue is that the access needs to be mutually exclusive, otherwise, may cause omission of data processing, or repeated data processing. A small amount of repeat data is acceptable, but missing data processing is unacceptable. We will not discuss the reasons for repeated and omissions. Therefore, when multiple threads access and process the same data source, they must be mutually exclusive.
Next, let's take a look at several implementation solutions I have tried.
Multithreading solution 1:
There is only one attempt string. Take multiple threads in turn, and then perform computation and comparison.
In this way, each thread is continuously trying. However, when one thread is generating an attempt string, the other threads cannot access the attempt string, and the thread needs to block and wait. This is expected to happen frequently, which will affect the speed.
Program implementation,
If mutex is used for thread mutex, the speed will be greatly affected, because the execution of some mutex code is much slower than the single-thread version. ("Ddzzzz" md5 cracking. The four threads use mutex for mutex, Which is 118984 ms. A single thread is about 13500 ms. Four threads are more than eight times slower than a single thread)
If InterlockedIncrement64 is used, there are too many restrictions on the program.
Multithreading solution 2:
Divide the task into n equal parts (n is the number of cpu cores), create n threads, and each thread completes 1 copy.
There is no mutex problem.
Poor logic clarity. It is difficult to modify the Code if it is used in different application scenarios in the future.
No program is implemented.
Multithreading solution 3:
Divide the task into a fixed number of parts, and then create n threads (n is the number of cores of the cpu). Each thread has one copy. After the job is finished, it receives one copy, until all the copies are completed.
In the thread, you need to complete the code. A portion must be mutually exclusive. The occurrence frequency of mutex is very small.
No program is implemented.
Multithreading solution 4:
Create n threads (n is the number of cpu cores ).
Each thread extracts a part of the total task for processing each time. After processing, this part of the task is retrieved. Until all tasks are processed. The advantage is that the distribution is relatively uniform. Continuity is also good.
Program implementation,
"Ddzzzz" md5 cracking, 3 threads 4641 ms. The best effect.
Why is multithreading solution 4 a perfect multi-threaded programming solution:
1. In a scenario with high execution efficiency requirements, the execution efficiency is very high.
2. The code is very logical and has a wide range of applicability. Few changes can be applied in many application scenarios.
Procedures included in this article (http://pan.baidu.com/s/1i3ip4nb) Description:
The MD5Calc.exe graphic interface converts the specified plaintext password to an MD5 encrypted password.
JiurlMd5CrackGui.exe graphical interface, multi-thread brute-force cracking demo program, implemented in multithreading solution 4 in this article. You can set the number of threads used by yourself. The default number of threads is 1 less than the number of CPU cores.
TryCharset.txt specifies the character set to be tried.
CryptedPassword.txt specifies the password to be cracked.
Temporary Home: http://blog.sina.com.cn/ddqqppb
QQ exchange group on the tall: 91877299