-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumatorioPthreads.c
89 lines (69 loc) · 1.64 KB
/
SumatorioPthreads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* FirstMultiThreading.c - Primera aplicación multi-hilo
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define DMaxThreads 100
/* Definición Estructuras */
typedef struct Intervalo {
int begin;
int end;
} TIntervalo;
/* Prototipos Funciones */
long int *Sumatorio(TIntervalo *i);
int main(int argc, char *argv[])
{
int N, M;
int h;
pthread_t Tids[DMaxThreads];
TIntervalo Interval[DMaxThreads];
long int SumaTotal=0, *SumaParcial, ResultadoCorrecto;
if (argc<3)
{
fprintf(stderr,"Argumentos insuficientes.\n");
exit(-1);
}
N = atol(argv[1]);
M = atoi(argv[2]);
if (M>DMaxThreads)
M=DMaxThreads;
/* Creación Hilos*/
for (h=0;h<M;h++)
{
if (h==0)
Interval[h].begin = 1;
else
Interval[h].begin = Interval[h-1].end;
if (h==(M-1))
Interval[h].end = N+1;
else
Interval[h].end = Interval[h].begin + ((N-Interval[h].begin)+1)/(M-h);
if (pthread_create(&Tids[h], NULL, (void *(*) (void *)) Sumatorio, &Interval[h])!=0)
{
perror("Error creación hilos.\n");
exit(-1);
}
//printf("Hilo %d Creado -> Intervalo (%d,%d].\n",h+1,Interval[h].begin, Interval[h].end);
}
SumaTotal=0;
/* Esperar finalización Hilos*/
for (h=0;h<M;h++)
{
pthread_join(Tids[h],(void**)&SumaParcial);
SumaTotal += *SumaParcial;
free(SumaParcial);
}
ResultadoCorrecto = ((long int)N * ((long int)N+1))/2;
printf("Sumatorio %d primeros numeros: %ld (resultado esperado %ld).\n",N,SumaTotal,ResultadoCorrecto);
exit(0);
}
long int *Sumatorio(TIntervalo *i)
{
int x;
long int *resp=malloc(sizeof(long int));
*resp=0;
for(x=i->begin;x<i->end;x++)
*resp += x;
return (resp);
}