Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- #include "pthread.h"
- #include <windows.h>
- #include <stdbool.h>
- pthread_mutex_t mutreta;
- long int num = 5;
- long int total = 2;
- long int ultimo = 3;
- clock_t tempo;
- int tempofinal = 1;
- int opcao = 1;
- void entrada(void);
- bool Primo(long int n){
- if (n%2 == 0){
- return false;
- }else if (n%3 == 0){
- return false;
- }else if (n%10 == 5){
- return false;
- }
- int n2 = 5;
- while (n2 <= n/2){
- if(n%n2 == 0 )
- return false;
- n2= n2+2;
- }
- return true;
- }
- void* Encontrar(void *encontro){
- while (clock() - tempo <= tempofinal){
- if(Primo(num) == true && num > ultimo){
- total++;
- pthread_mutex_lock(&mutreta);
- ultimo = num;
- pthread_mutex_unlock(&mutreta);
- }
- num+=2;
- }
- pthread_exit(NULL);
- }
- int imprime(){
- if (Primo(ultimo) == true){
- total++;
- printf("\n Numero de threads %d", opcao);
- printf("\n Total de %ld com thread!", total);
- printf("\n ultimo: %ld \n", ultimo);
- }
- if(Primo(ultimo)== false){
- ultimo= ultimo+2;
- imprime();
- }
- }
- void ciclos(){
- num = 5;
- total = 2;
- ultimo = 3;
- pthread_mutex_init( &mutreta, NULL );
- pthread_t pthd[opcao];
- for (int contador =0; contador < opcao; contador++){
- pthread_create(&pthd[contador],NULL,Encontrar,NULL);
- }
- tempo = clock();
- for (int contador = 0; contador < opcao; contador++ ){
- pthread_join( pthd[contador], NULL );
- }
- pthread_mutex_destroy( &mutreta );
- imprime();
- }
- int main()
- {
- printf("\n Programa Para encontrar primos \n");
- printf("\n Tempo 1 ou 2 minutos?");
- scanf("%d", &tempofinal);
- if (tempofinal == 1){
- tempofinal = 60000;
- }else if (tempofinal == 2){tempofinal = 120000;}
- printf("\n quantos nucleos?");
- scanf("%d", &opcao);
- ciclos();
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement