Anonymní profil ivan – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil ivan – Programujte.comAnonymní profil ivan – Programujte.com

 

Příspěvky odeslané z IP adresy 88.212.40.–

ivan
C / C++ › multihtreading c++ a i7
21. 3. 2012   #155301

Zdravim, neviem v ktorom vlakne sa spytat, tak sa vopred ospravedlnujem.

Chcel sa opytat, ak mam viac vlaknovy program a viac jadrovy procesor, ci viem nejak ovplyvnit, jadra procesoru tak, aby vykonavali len moj program. Ci aj tu je proste pridelovany proc. cas na kazde vlaknu na isty cas a bud ho vyuzije, res. caka atd.

Dakujem

C / C++ › Pthreads vlakno
19. 3. 2012   #155170

Ok, problem som vyriesil, mozem ist v klude spat :D

Semafory som odstranil a do logu zapisujem len kym existuje vlakno1.

Este raz vdaka z prispevky.

C / C++ › Pthreads vlakno
19. 3. 2012   #155169

Chyba je tu:

v1 = pthread_create( &vlakno1,...
v2 = pthread_create( &vlakno1 ---> tu ma byt &vlakno2

Teraz prve vlakno generuje tak ako ma, akurat druhe co zapisuje do logu tam zapise iba 1x, asi niekde budem mat zle semafory, ale neviem prist na to. Ak niekto poradi budem vdacny :)

C / C++ › Pthreads vlakno
18. 3. 2012   #155160

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;
}

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý