-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOSIXThread.c
More file actions
137 lines (100 loc) · 3.59 KB
/
POSIXThread.c
File metadata and controls
137 lines (100 loc) · 3.59 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
/* ATENÇÃO!!!!!!
* PARA COMPILAR COM O GCC INCLUIR O ARGUMENTO -lpthread
* EXEMPLO: gcc POSIXThread.c -o Posix -lpthread
*/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
void * rotina(void *arg);
int main(int argc, char *argv[]){
pthread_t threadA;
pthread_t threadB;
pthread_t threadC;
pthread_t threadD;
pthread_t threadE;
void *thread_res;
int rstatus;
/* Inicializa a thread A */
rstatus = pthread_create(&threadA, NULL,rotina,(void*)("Thread A"));
/* Se o status for diferente de 0 houve algum erro na craição da thread*/
if(rstatus != 0){
printf("Erro ao criar thread \n");
exit(EXIT_FAILURE);
}
printf("Thread A criada com sucesso");
/* Inicializa a thread B */
rstatus = pthread_create(&threadB, NULL,rotina,(void*)("Thread B"));
/* Se o status for diferente de 0 houve algum erro na craição da thread*/
if(rstatus != 0){
printf("Erro ao criar thread \n");
exit(EXIT_FAILURE);
}
printf("Thread B criada com sucesso. \n");
/* Inicializa a thread C */
rstatus = pthread_create(&threadC, NULL,rotina,(void*)("Thread C"));
/* Se o status for diferente de 0 houve algum erro na craição da thread*/
if(rstatus != 0){
printf("Erro ao criar thread \n");
exit(EXIT_FAILURE);
}
printf("Thread C criada com sucesso. \n");
/* Inicializa a thread D */
rstatus = pthread_create(&threadD, NULL,rotina,(void*)("Thread D"));
/* Se o status for diferente de 0 houve algum erro na craição da thread*/
if(rstatus != 0){
printf("Erro ao criar thread. \n");
exit(EXIT_FAILURE);
}
printf("Thread D criada com sucesso. \n");
/* Inicializa a thread E */
rstatus = pthread_create(&threadE, NULL,rotina,(void*)("Thread E"));
/* Se o status for diferente de 0 houve algum erro na craição da thread*/
if(rstatus != 0){
printf("Erro ao criar thread. \n");
exit(EXIT_FAILURE);
}
printf("Thread E criada com sucesso. \n");
/* Aqui aguardamos o encerramento das threads, uma a uma*/
rstatus = pthread_join(threadA, &thread_res);
if (rstatus != 0){
printf("Erro na finalização da thread A. \n");
exit(EXIT_FAILURE);
}
printf("Thread A finalizada. Retorno =%s \n", (char*) thread_res);
rstatus = pthread_join(threadB, &thread_res);
if (rstatus != 0){
printf("Erro na finalização da thread B. \n");
exit(EXIT_FAILURE);
}
printf("Thread B finalizada. Retorno =%s \n", (char*) thread_res);
rstatus = pthread_join(threadC, &thread_res);
if (rstatus != 0){
printf("Erro na finalização da thread C. \n");
exit(EXIT_FAILURE);
}
printf("Thread C finalizada. Retorno =%s \n", (char*) thread_res);
rstatus = pthread_join(threadD, &thread_res);
if (rstatus != 0){
printf("Erro na finalização da thread D. \n");
exit(EXIT_FAILURE);
}
printf("Thread D finalizada. Retorno =%s \n", (char*) thread_res);
rstatus = pthread_join(threadE, &thread_res);
if (rstatus != 0){
printf("Erro na finalização da thread E. \n");
exit(EXIT_FAILURE);
}
printf("Thread E finalizada. Retorno =%s \n", (char*) thread_res);
return 0;
}
void * rotina(void *arg){
int repeticoes = 5;
while(repeticoes >= 0){
printf("%s: Saída número %i.\n", (char*)arg, repeticoes);
repeticoes--;
/* Comando para ceder o controle da thread, deixando outra assumir a vez */
sched_yield();
}
pthread_exit(arg);
}