** 这是针对大学课程的,我实际上并没有尝试破解密码 ** 下面是我的源代码,但基本上我想要发生的是父进程将密码排队到 std::list<> 尝试列表中。然后子线程从队列的前面抓取,目前只打印出该值。
正如您在下面的代码中看到的那样,我尝试在弹出前端时使用 std::mutex 来阻止尝试列表,但是两个线程同时通过锁并从前端读取。然后其中之一出现分段错误,程序崩溃。
段错误的特定代码部分是......
mutex.lock();
password = attemptList.front();
attemptList.pop_front();
size = attemptList.size();
std::cout << password << std::endl;
mutex.unlock();
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <chrono>
#include <shared_mutex>
#include <unistd.h>
#include <sys/ipc.h>
#include <mutex>
#include <sys/shm.h>
#include <sys/wait.h>
#include <thread>
#include <vector>
#include <algorithm>
#include <list>
#define MAX_LENGTH 4
#define MAX_QUEUE_SIZE 1000
#define CHARACTER_LIST "abcdefghijklmnopqrstuvwxyz"
void enqueue_passwords(const std::string& charList);
void bruteforce();
void do_join(std::thread& t);
void join_all(std::vector<std::thread>& v);
std::list<std::string> attemptList;
std::mutex mutex;
bool conclude = false;
int main(int argc, char* argv[]) {
auto start = std::chrono::high_resolution_clock::now();
int index;
std::vector<std::thread> threads;
for (index = 0; index < 2; index++) {
threads.emplace_back(std::thread(bruteforce));
}
enqueue_passwords(CHARACTER_LIST);
join_all(threads);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout << duration.count() << " milliseconds" << std::endl;
return 0;
}
void bruteforce() {
double size = 0;
std::string password;
while (!conclude) {
do {
mutex.lock();
size = attemptList.size();
mutex.unlock();
if (size == 0) {
usleep(300);
}
} while (size == 0);
while(size != 0) {
mutex.lock();
password = attemptList.front();
attemptList.pop_front();
size = attemptList.size();
std::cout << password << std::endl;
mutex.unlock();
}
}
}
void enqueue_passwords(const std::string& charList) {
const int maxLength = MAX_LENGTH;
const int charListLength = charList.length();
char password[MAX_LENGTH + 1];
memset(password, '\0', MAX_LENGTH + 1);
int index;
int number;
double permutations = 0;
double count = 0;
double passPosition = 0;
double size = 0;
// Calculate number of permutations possible
for (index = 0; index < maxLength; index++) {
permutations += charListLength * powl(charList.length(), maxLength - index - 1);
}
std::cout << "Permutations: " << permutations << std::endl << std::endl;
password[0] = charList[0];
while (count < permutations) {
do {
mutex.lock();
size = attemptList.size();
mutex.unlock();
if (size > MAX_QUEUE_SIZE) {
usleep(250);
}
} while (size > MAX_QUEUE_SIZE);
// Loop over current set of characters ,changing the last one
for (index = 0; index < charListLength; index++) {
password[int(passPosition)] = charList[index];
// ENQUEUE HERE //
mutex.lock();
attemptList.push_back(std::string(password));
mutex.unlock();
// ENQUEUE HERE //
if (count > permutations) {
break;
}
count++;
}
// Iterate over remaining indexes, except for the last one
for (number = int(passPosition); number >= 0; number--) {
if (password[number] != charList[charListLength - 1]) {
password[number]++;
break;
} else {
if (number == 0) {
passPosition++;
for (index = 0; index < passPosition + 1; index++) {
password[index] = charList[0];
}
break;
}
password[number] = charList[0];
}
}
}
conclude = true;
}
void do_join(std::thread& t) {
t.join();
}
void join_all(std::vector<std::thread>& v) {
std::for_each(v.begin(), v.end(), do_join);
}