Calculate Factorial Array using Thread’s

Asked

Viewed 395 times

2

I’m having problems in the following exercise

Write a program that, given a vector with the first 5 numbers primes, pitch 5 threads. Each of them will calculate the value of the factorial of one of the vector positions and replaces that vector’s value position with calculated value. The main thread expects all threads finish, print vector and finish as well.

I have the following resolution

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

#define NTHREADS 5

void *Fatorial(int *array)
{
    printf("***Entrei na função fatorial***\n");
    int fat = 0, n = 0;
    for(fat = 1; n > 1; n = n - 1)
    {
        array[n] = array[n] * n;
    }
    pthread_exit(NULL);
}

int main()
{
    printf("Início da função\n");
    int *array[5] = {2,3,5,7,11};
    int i = 0,rc = 0;
    pthread_t tid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    printf("Antes do FOR\n");
    for(i = 0; i < NTHREADS; i++)
    {
        rc = array[i];
        rc = pthread_create(&tid,&attr,Fatorial,NULL);
        if (rc) 
        {              
            printf("ERROR - return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
        rc = pthread_join(tid, NULL);
        if (rc) 
        {
            printf("ERROR; return code from pthread_join() is %d\n", rc);
            exit(-1);
        }
    }
    printf("Array[] = ");
    for(i = 0; i < NTHREADS; i++)
    {
        printf("%d ",array[i]);
    }
    pthread_attr_destroy(&attr);
    pthread_exit(NULL);

    return 0;
}

One of my problems is how to have the array calculated in the auxiliary thread (which aims to calculate the factorial of an array position). Testanto with print’s I can notice that the program enters 5 times in the auxiliary thread so I think I am in a correct path. I still don’t understand how to work the array itself.

1 answer

1

Like each of the threads will change a different position of the vector, it can be declared as global, and the position for the factorial calculation, be informed as a parameter of the thread.

Below, follow the commented code (with a few more changes):

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

#define NTHREADS 5

// declara o array como global
// como cada thread irá escrever em uma posição diferente
// não precisa de lock
int array[5] = { 2, 3, 5, 7, 11 };

// informa a posição do array para o cálculo
void *Fatorial(void *pos)
{
    int res = 1, fat;
    int *posicao = (int *)pos; // obtém o parâmetro da posição
    printf("***Entrei na funcao fatorial %d***\n", *posicao);
    // calcula o fatorial
    for(fat = array[*posicao]; fat > 1; fat--)
    {
         res *= fat;
    }
    // armazena de volta no array
    array[*posicao] = res;
    pthread_exit(NULL);
}

int main()
{
  printf("Inicio da funcao\n");
  int i = 0,rc = 0, pos[NTHREADS]; // Um parâmetro para cada thread
  pthread_t tid[NTHREADS]; // Uma posição para cada thread
  pthread_attr_t attr;

  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  printf("Antes do FOR\n");

  // Primeiro cria as threads...
  for(i = 0; i < NTHREADS; i++)
  {
    // cria a thread e informa a posição para cálculo
    // como parâmetro
    pos[i] = i;
    rc = pthread_create(&tid[i], &attr, &Fatorial, (void *)&pos[i]);
    if (rc)
    {
      printf("ERROR - return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }

  // ...depois, aguarda a execução (join)
  for(i = 0; i < NTHREADS; i++)
  {
    rc = pthread_join(tid[i], NULL);
    if (rc)
    {
        printf("ERROR; return code from pthread_join() is %d\n", rc);
        exit(-1);
    }
  }
  printf("Array[] = ");
  for(i = 0; i < NTHREADS; i++)
  {
    printf("%d ",array[i]);
  }
  pthread_attr_destroy(&attr);
  pthread_exit(NULL);
  return 0;
}

Exit after execution:

Inicio da funcao
Antes do FOR
***Entrei na funcao fatorial 0***
***Entrei na funcao fatorial 1***
***Entrei na funcao fatorial 2***
***Entrei na funcao fatorial 3***
***Entrei na funcao fatorial 4***
Array[] = 2 6 120 5040 39916800

Browser other questions tagged

You are not signed in. Login or sign up in order to post.