Skip to content

Instantly share code, notes, and snippets.

@expalmer
Last active March 14, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expalmer/1ac1d39c61ba521335e878eaab783518 to your computer and use it in GitHub Desktop.
Save expalmer/1ac1d39c61ba521335e878eaab783518 to your computer and use it in GitHub Desktop.
Sistemas Distribuidos
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int nEp = 1;
int tam = 12;
int n = 4;
int *vA = (int *)malloc(tam * sizeof(int));
int *vB = (int *)malloc(tam * sizeof(int));
int *vC = (int *)malloc(tam * sizeof(int));
int v;
for (int i = 0; i < tam; ++i)
{
v = i + 1;
vA[i] = v;
vB[i] = v;
}
// Exemplo 1
for (int i = nEp - 1; i < tam;)
{
vC[i] = vA[i] + vB[i];
printf("=> %i [%i]\n", i, vC[i]);
i = i + n;
}
printf("======\n");
// Exemplo 2
int times = tam / n;
int ini = (nEp - 1) * times;
int fim = nEp * times;
for (int i = ini; i < fim; i++)
{
vC[i] = vA[i] + vB[i];
printf("=> %i [%i]\n", i, vC[i]);
}
}
function fatorial(n) {
let fat
for(fat = 1; n > 1; n -= 1) {
fat *= n
}
return fat
}
function getEpId() {
return 1
}
function getTotalEps() {
return 3
}
// Array que o usuario informa
const array = [1,2,3,4,5,6,7,8,9,123,3,1,2,314,51,5,1]
const tam = array.length
// funcao fake de pegar o id do processo
let epId = getEpId()
// funcao fake de pegar o total de CORES
let nEps = getTotalEps()
// array dos resultados
let arrayEps = []
// preenche o array com valor vazio
for(let i = 0; i < nEps; i++) {
arrayEps[i] = []
}
// faz o fatorial conforme processo id
for(let i = epId - 1; i < tam; i += nEps) {
let item = array[i]
arrayEps[epId - 1].push(fatorial(item))
}
console.log('arrayEps', arrayEps)
/*
1) Independete do tamanho do array de números, e do total de nEps, cada EP vai ser responsavel
por fazer o fatorial de cada um item conforme seu multiplo como mostrado acima.
Um dos problemas é que não tem implementado uma distribuição dos valores, pois um EP pode pegar
numero muito grandes para a fatoração, consequentemente esse processo vai ter que fazer um trabalho
muito maior.
De repete uma solução seria implementar alguma distribuição justa dos números para a quantidade de processos
PROBLEMAS: Ainda não implementamos uma forma de saber quando todos processos acabaram seu trabalho,
mas também temos o problema de algum estourar a meméoria, ou dar algum erro.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment