Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* LUIZ FELIPE MERIZIO TRIDAPALLI */
- #include <stdio.h>
- #include <time.h>
- #include <math.h>
- #include "pthread.h"
- #include <time.h>
- #include <windows.h>
- pthread_mutex_t meu_mutex;
- long int N = 5;
- long int total = 2;
- long int ultimo = 3;
- clock_t tempo;
- bool primo(long int num){
- int cont = 0;
- if (num %2 == 0){
- return false;
- } else {
- for (int i = 3 ; i < num; i+=2, cont++)
- if (num % i == 0) {
- return true;
- } else {
- return false;
- }
- }
- }
- void* encontra_primos(void *param )
- {
- while ( clock() - tempo <= 60000){
- if (primo(N) == true && N != ultimo){
- total++;
- ultimo = N;
- }
- N+=2;
- }
- pthread_exit(NULL);
- }
- void primosingle(){
- N = 5;
- total = 2;
- ultimo = 3;
- printf("\nEncontrando primos single...");
- while (clock() - tempo <= 60000){
- if (primo(N)){
- total++;
- ultimo = N;
- }
- N=N+2;
- }
- printf("\n\tTotal de %d sem thread!", total );
- printf("\n\tUltimo %d \n\n", ultimo );
- }
- void primomulti(){
- N = 5;
- total = 2;
- ultimo = 3;
- tempo = clock();
- pthread_t thd1, thd2, thd3, thd4;
- printf("\n\nEncontrando primos multithread...");
- tempo = clock();
- pthread_mutex_init( &meu_mutex, NULL );
- pthread_create(&thd1,NULL,encontra_primos,NULL);
- pthread_create(&thd2,NULL,encontra_primos,NULL);
- pthread_create(&thd3,NULL,encontra_primos,NULL);
- pthread_create(&thd4,NULL,encontra_primos,NULL);
- pthread_join( thd1, NULL );
- pthread_join( thd2, NULL );
- pthread_join( thd3, NULL );
- pthread_join( thd4, NULL );
- pthread_mutex_destroy( &meu_mutex );
- printf("\n\tTotal de %d com thread!", total );
- printf("\n\tUltimo %d \n\n", ultimo );
- }
- int main()
- {
- char opcao;
- printf("\n\t\tENCONTRA PRIMOS\n\n");
- printf("--> Tecle S para Single ou M para Multi: ");
- scanf("%c", &opcao);
- if (opcao == 'S' || opcao == 's'){
- primosingle();
- } else if (opcao == 'M' || opcao == 'm'){
- primomulti();
- } else {
- printf("\n\t\tOpcao incorreta!\n\n");
- }
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement