Skip to content

Commit ababdaf

Browse files
committed
facul/so/leitores_escritores: add código fornecido do manzano
Signed-off-by: lucasew <[email protected]>
1 parent 148319c commit ababdaf

File tree

5 files changed

+577
-0
lines changed

5 files changed

+577
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
O problema do Jantar dos filósofos, sem controle de impasses.
3+
4+
Compilar com gcc -Wall filosofos.c -o filosofos -lpthread
5+
6+
Carlos Maziero, DINF/UFPR 2020
7+
*/
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <unistd.h>
12+
#include <pthread.h>
13+
#include <semaphore.h>
14+
15+
#define NUMFILO 5
16+
17+
pthread_t filosofo [NUMFILO] ; // threads filosofos
18+
sem_t hashi [NUMFILO] ; // um semaforo para cada palito (iniciam em 1)
19+
20+
// espaços para separar as colunas de impressão
21+
char *space[] = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t" } ;
22+
23+
// espera um tempo aleatório entre 0 e n segundos (float)
24+
void espera (int n)
25+
{
26+
sleep (random() % n) ; // pausa entre 0 e n segundos (inteiro)
27+
usleep (random() % 1000000) ; // pausa entre 0 e 1 segundo (float)
28+
}
29+
30+
// filósofo comendo
31+
void come (int f)
32+
{
33+
printf ("%sCOMENDO\n", space[f]) ;
34+
espera (2) ;
35+
}
36+
37+
// filósofo meditando
38+
void medita (int f)
39+
{
40+
printf ("%smeditando\n", space[f]) ;
41+
espera (2) ;
42+
}
43+
44+
// pega o hashi
45+
void pega_hashi (int f, int h)
46+
{
47+
printf ("%squer h%d\n", space[f], h) ;
48+
sem_wait (&hashi [h]) ;
49+
printf ("%spegou h%d\n", space[f], h) ;
50+
}
51+
52+
// larga o hashi
53+
void larga_hashi (int f, int h)
54+
{
55+
printf ("%slarga h%d\n", space[f], h) ;
56+
sem_post (&hashi [h]) ;
57+
}
58+
59+
// corpo da thread filosofo
60+
void *threadFilosofo (void *arg)
61+
{
62+
int i = (long int) arg ;
63+
while (1)
64+
{
65+
medita (i) ;
66+
pega_hashi (i, i) ;
67+
pega_hashi (i, (i+1) % NUMFILO) ;
68+
come (i) ;
69+
larga_hashi (i, i) ;
70+
larga_hashi (i, (i+1) % NUMFILO) ;
71+
}
72+
pthread_exit (NULL) ;
73+
}
74+
75+
// programa principal
76+
int main (int argc, char *argv[])
77+
{
78+
long i, status ;
79+
80+
// para o printf não se confundir com a threads
81+
setvbuf (stdout, 0, _IONBF, 0) ;
82+
83+
// inicia os hashis
84+
for(i=0; i<NUMFILO; i++)
85+
sem_init (&hashi[i], 0, 1) ;
86+
87+
// inicia os filosofos
88+
for(i=0; i<NUMFILO; i++)
89+
{
90+
status = pthread_create (&filosofo[i], NULL, threadFilosofo, (void *) i) ;
91+
if (status)
92+
{
93+
perror ("pthread_create") ;
94+
exit (1) ;
95+
}
96+
}
97+
98+
// a main encerra aqui
99+
pthread_exit (NULL) ;
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Sistema produtor-consumidor usando variáveis de condição.
3+
4+
ATENÇÃO: este exemplo está mais simples que aquele que aparece no vídeo.
5+
6+
Compilar com gcc -Wall pc-cvar.c -o pc-cvar lpthread
7+
8+
Carlos Maziero, DINF/UFPR 2020
9+
*/
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <unistd.h>
14+
#include <pthread.h>
15+
16+
#define NUM_PROD 5
17+
#define NUM_CONS 3
18+
#define VAGAS 6
19+
20+
pthread_mutex_t mutex ; // mutex para acesso ao buffer
21+
pthread_cond_t itens_cv ; // vc para controle de itens
22+
pthread_cond_t vagas_cv ; // vc para controle de vagas
23+
24+
int num_itens, num_vagas ; // contadores de itens e vagas
25+
26+
// espera um tempo aleatório entre 0 e n segundos (float)
27+
void espera (int n)
28+
{
29+
sleep (random() % n) ; // pausa entre 0 e n segundos (inteiro)
30+
usleep (random() % 1000000) ; // pausa entre 0 e 1 segundo (float)
31+
}
32+
33+
// corpo de produtor
34+
void *prodBody (void *id)
35+
{
36+
long tid = (long) id ;
37+
38+
while (1)
39+
{
40+
// requisita acesso exclusivo ao buffer
41+
pthread_mutex_lock (&mutex);
42+
43+
// se não houver vaga, libera o buffer e espera
44+
while (num_vagas == 0)
45+
pthread_cond_wait (&vagas_cv, &mutex);
46+
47+
// coloca um item no buffer
48+
num_vagas-- ;
49+
num_itens++ ;
50+
printf ("P%02ld: put item (%d items, %d places)\n",
51+
tid, num_itens, num_vagas) ;
52+
53+
// sinaliza um novo item
54+
pthread_cond_signal (&itens_cv);
55+
56+
// libera o buffer
57+
pthread_mutex_unlock (&mutex) ;
58+
59+
espera (2) ;
60+
}
61+
}
62+
63+
// corpo de cosumidor
64+
void *consBody (void *id)
65+
{
66+
long tid = (long) id ;
67+
68+
while (1)
69+
{
70+
// requisita acesso exclusivo ao buffer
71+
pthread_mutex_lock (&mutex) ;
72+
73+
// se não houver item, libera o buffer e espera
74+
while (num_itens == 0)
75+
pthread_cond_wait (&itens_cv, &mutex) ;
76+
77+
// retira um item do buffer
78+
num_itens-- ;
79+
num_vagas++ ;
80+
printf ("\t\t\t\t\tC%02ld: got item (%d items, %d places)\n",
81+
tid, num_itens, num_vagas) ;
82+
83+
// sinaliza uma nova vaga
84+
pthread_cond_signal (&vagas_cv) ;
85+
86+
// libera o buffer
87+
pthread_mutex_unlock (&mutex) ;
88+
89+
espera (2) ;
90+
}
91+
}
92+
93+
// programa principal
94+
int main (int argc, char *argv[])
95+
{
96+
pthread_t produtor [NUM_PROD] ;
97+
pthread_t consumidor [NUM_CONS] ;
98+
long i ;
99+
100+
// inicia contadores
101+
num_itens = 0 ;
102+
num_vagas = VAGAS ;
103+
104+
// inicia variaveis de condição e mutexes
105+
pthread_mutex_init (&mutex, NULL) ;
106+
pthread_cond_init (&itens_cv, NULL) ;
107+
pthread_cond_init (&vagas_cv, NULL) ;
108+
109+
// cria produtores
110+
for (i=0; i<NUM_PROD; i++)
111+
if (pthread_create (&produtor[i], NULL, prodBody, (void *) i))
112+
{
113+
perror ("pthread_create") ;
114+
exit (1) ;
115+
}
116+
117+
// cria consumidores
118+
for (i=0; i<NUM_CONS; i++)
119+
if (pthread_create (&consumidor[i], NULL, consBody, (void *) i))
120+
{
121+
perror ("pthread_create") ;
122+
exit (1) ;
123+
}
124+
125+
// main encerra aqui
126+
pthread_exit (NULL) ;
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Sistema produtor-consumidor usando semáforos.
3+
4+
Compilar com gcc -Wall pc-sem.c -o pc-sem -lpthread
5+
6+
Carlos Maziero, DINF/UFPR 2020
7+
*/
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <unistd.h>
12+
#include <pthread.h>
13+
#include <semaphore.h>
14+
15+
#define NUM_PROD 5
16+
#define NUM_CONS 3
17+
#define VAGAS 6
18+
19+
sem_t vagas, itens ; // semáforos para controle de vagas e peças
20+
sem_t buffer ; // mutex para acesso ao buffer
21+
22+
int num_itens, num_vagas ; // contadores de vagas e peças
23+
24+
// espera um tempo aleatório entre 0 e n segundos (float)
25+
void espera (int n)
26+
{
27+
sleep (random() % n) ; // pausa entre 0 e n segundos (inteiro)
28+
usleep (random() % 1000000) ; // pausa entre 0 e 1 segundo (float)
29+
}
30+
31+
// corpo de produtor
32+
void *prodBody (void *id)
33+
{
34+
long tid = (long) id ;
35+
36+
printf ("P%02ld: Olá!\n", tid) ;
37+
38+
while (1)
39+
{
40+
sem_wait (&vagas) ;
41+
sem_wait (&buffer) ;
42+
num_itens++ ;
43+
num_vagas-- ;
44+
printf ("P%02ld: put an item (%d itens, %d vagas)!\n",
45+
tid, num_itens, num_vagas) ;
46+
sem_post (&buffer) ;
47+
sem_post (&itens) ;
48+
espera (2) ;
49+
}
50+
}
51+
52+
// corpo de consumidor
53+
void *consBody (void *id)
54+
{
55+
long tid = (long) id ;
56+
57+
printf ("C%02ld: Olá!\n", tid) ;
58+
59+
while (1)
60+
{
61+
sem_wait (&itens) ;
62+
sem_wait (&buffer) ;
63+
num_itens-- ;
64+
num_vagas++ ;
65+
printf ("C%02ld: got an item (%d itens, %d vagas)!\n",
66+
tid, num_itens, num_vagas) ;
67+
sem_post (&buffer) ;
68+
sem_post (&vagas) ;
69+
espera (2) ;
70+
}
71+
}
72+
73+
// programa principal
74+
int main (int argc, char *argv[])
75+
{
76+
pthread_t produtor [NUM_PROD] ;
77+
pthread_t consumidor [NUM_CONS] ;
78+
long i ;
79+
80+
num_itens = 0 ;
81+
num_vagas = VAGAS ;
82+
83+
// inicia semaforos
84+
sem_init (&buffer, 0, 1) ;
85+
sem_init (&vagas, 0, VAGAS) ;
86+
sem_init (&itens, 0, 0) ;
87+
88+
// cria produtores
89+
for (i=0; i<NUM_PROD; i++)
90+
if (pthread_create (&produtor[i], NULL, prodBody, (void *) i))
91+
{
92+
perror ("pthread_create") ;
93+
exit (1) ;
94+
}
95+
96+
// cria consumidores
97+
for (i=0; i<NUM_CONS; i++)
98+
if (pthread_create (&consumidor[i], NULL, consBody, (void *) i))
99+
{
100+
perror ("pthread_create") ;
101+
exit (1) ;
102+
}
103+
104+
// main encerra aqui
105+
pthread_exit (NULL) ;
106+
}

0 commit comments

Comments
 (0)