-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab6.c
125 lines (109 loc) · 3.23 KB
/
lab6.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//Ellen
//dre 120041556
//implementando barreira
#include <stdio.h>
#include <stdlib.h>
# include <pthread.h>
//variáveis globais
int bloqueadas=0;
pthread_mutex_t x_mutex;
pthread_cond_t x_cond;
int n;
int *vetor; //vetor que será usado pelas threads
//função da barreira
void barreira (int nthreads){
pthread_mutex_lock(&x_mutex); //inicio seção crítica
if(bloqueadas== (nthreads-1)){
//última thread a chegar na barreira
pthread_cond_broadcast(&x_cond);
bloqueadas=0;
}
else{
bloqueadas++;
pthread_cond_wait(&x_cond, &x_mutex);
}
pthread_mutex_unlock(&x_mutex);
}
//função que as threads irão executar
void * tarefa (void * arg){
int id = *((int *) arg);
int * somatorio;
somatorio= malloc(sizeof(int));
if(somatorio==NULL){
printf("ERRO--malloc()\n");
exit(1);
}
*somatorio=0; //inicializando
for(int j=0;j<n;j++){
for(int i=0; i< n;i++){
*somatorio+=*(vetor+i);
}
//printf("thread %d encontra a 1 barreira\n", id);
barreira(n); //aguarda as outras terminarem suas somas
//altera o vetor
srand(time(NULL));
pthread_mutex_lock(&x_mutex); //seção crítica
*(vetor+id)=rand() %10;
pthread_mutex_unlock(&x_mutex);
//printf("thread %d encontra a 2 barreira\n", id);
barreira(n); //aguarda as outras terminarem de escrever
}
pthread_exit((void *)somatorio);
}
//fluxo principal
int main (int argc, char * argv[]){
if(argc <2){
printf("Insira a quantidade de iteração\n");
exit(1);
}
n=atoi(argv[1]);
pthread_t threads[n]; //vetor com os id das threads no sistema
int id[n]; //vetor com os id das threads
int *retorno; //valor de retorno das threads
int result[n]; //vetor com o retorno de todas as threads
//inicializa o mutex e a variável de condição
pthread_mutex_init (&x_mutex,NULL);
pthread_cond_init (&x_cond, NULL);
//inicializando o vetor
vetor= malloc(sizeof(int)*n);
if(vetor==NULL){
printf("ERRO--malloc()\n");
exit(1);
}
srand(time(NULL));
for(int i=0;i<n;i++){
*(vetor+i)=rand() %10;
}
//criação das threads
for(int i=0; i<n;i++){
id[i]=i;
if (pthread_create(&threads[i], NULL, tarefa, (void *)&id[i])){
printf("ERRO--pthread_create()\n");
return 2;
}
}
//espera todas as threads acabarem a execução
for(int i=0; i<n; i++){
if (pthread_join(threads[i],(void **) &retorno)){
printf("ERRO--pthread_join()\n");
return 2;
}
result[i]=*retorno;
//printf("Retorno thread %d = %d \n",i,*retorno);
free(retorno);
}
//verifica se os retornos estão iguais
for(int i=1;i<n; i++){
if(result[i]!=result[0]){
printf("ERRO-- resultados inconsistentes\n");
break;
}
if(i==(n-1))
printf("Tudo certo! os valores retornados estão corretos e são iguais a %d\n",result[0]);
}
//destrou o mutex e a variável de condição
pthread_mutex_destroy(&x_mutex);
pthread_cond_destroy(&x_cond);
free(vetor);
return 0;
}