Ahoj,
chcel by som v jednom vlakne generovat slova a v druhom, aby raz za cas zapisalo do logu (bud v nejakom intervale napr 10s, alebo ked bude moct), neviem si dat rady uz, program mi vygeneruje par slov a zapise do logu a skonci. Chcel by som aby dosiel na koniec a vygeneroval vsetky slova. Samostatne generovat slova mi ide.
Vopred Dakujem za rady a pripomienky, niektore detaily v programe mi prepacte, este tam bude viac zmien.
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include <time.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
using namespace std;
unsigned long word_counter = 0; //pocitadlo vygenerovanych hesiel
char allowed_characters[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; //znaky pre generovanie hesla
int max_password_lenght = 6;
int size_of_allowed_characters = sizeof(allowed_characters) / sizeof(char);
sem_t mutex;
unsigned long all_possible_passwords_count(){
unsigned long total_passwords = 0;
int password_size = max_password_lenght;
for (int i = password_size; i > 0; i--){
total_passwords += pow(size_of_allowed_characters, password_size);
password_size--;
}
return total_passwords;
}
void * trace_to_file(void *txt){
//sem_wait(&mutex); /* down semaphore */
ofstream trace;
trace.open("trace_log.txt", ios::out);
unsigned long total = all_possible_passwords_count();
trace << "Total: " << total << endl;
trace << "Used: " << word_counter << endl;
trace << "To end: " << total - word_counter << endl;
trace << "--------------------------------------------" << endl;
trace.close();
//sem_post(&mutex); /* up semaphore */
}
void printout(string text){
cout << text << endl;
}
void generate_next_word (string word){
if (word.length() <= max_password_lenght){
printout(word);
word_counter++;
string new_word;
for (int i = 0; i < size_of_allowed_characters; i++){
string add_character;
add_character = allowed_characters[i];
new_word = word + add_character;
generate_next_word(new_word);
}
}
}
void get_time(){
time_t rtime;
struct tm * timeinfo;
time(&rtime);
timeinfo = localtime(&rtime);
cout << asctime(timeinfo) << endl;
}
void * call_start(void *txt){
sem_wait(&mutex); /* down semaphore */
for (int i = 0; i < size_of_allowed_characters; i++){ //-lpthread
string word;
word = allowed_characters[i];
generate_next_word(word);
}
sem_post(&mutex); /* up semaphore */
}
int main(){
time_t seconds1, seconds2;
seconds1 = time(NULL);
cout << "All possible passwords from array of numbers 0-9 are: " << endl;
pthread_t vlakno1, vlakno2;
int v1, v2;
char pokus1[] = "textikA";
sem_init(&mutex, 0, 1); /* initialize mutex to 1 - binary semaphore */
/* second param = 0 - semaphore is local */
v1 = pthread_create( &vlakno1, NULL, &call_start, (void*)&pokus1);
v2 = pthread_create( &vlakno1, NULL, &trace_to_file, (void*)&pokus1);
pthread_join(vlakno1, NULL);
pthread_join(vlakno2, NULL);
sem_destroy(&mutex); /* destroy semaphore */
exit(0);
//vypis casu
seconds2 = time(NULL);
cout << seconds2-seconds1 << endl;
}