-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontas.h
125 lines (87 loc) · 2.74 KB
/
contas.h
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
/**************************************************/
/* Projecto SO 2016/2017 */
/* */
/* Joao Miguel Campos, n 75785 */
/* Carolina Monteiro, n 77990 */
/* */
/* */
/* Grupo 32 */
/* */
/**************************************************/
#ifndef CONTAS_H
#define CONTAS_H
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define NUM_CONTAS 10
#define TAXAJURO 0.1
#define CUSTOMANUTENCAO 1
#define ATRASO 1
#define NUM_TRABALHADORAS 3 /*numero de threads*/
#define CMD_BUFFER_DIM (NUM_TRABALHADORAS * 2) /*dimensao do buffer circular*/
#define MAXFILHOS 20
#define COMANDO_DEBITAR "debitar"
#define COMANDO_CREDITAR "creditar"
#define COMANDO_LER_SALDO "lerSaldo"
#define COMANDO_SIMULAR "simular"
#define COMANDO_SAIR "sair"
#define COMANDO_TRANSFERIR "transferir"
#define COMANDO_SAIR_TERMINAL "sair-terminal"
#define OP_LERSALDO 0
#define OP_CREDITAR 1
#define OP_DEBITAR 2
#define OP_SIMULAR 3
#define OP_SAIR 4
#define OP_TRANSFERIR 5
typedef struct {
int operacao;
int idConta;
int valor;
int idContaDestino;
char nome[1024];
} comando_t;
comando_t cmdbuffer[CMD_BUFFER_DIM];
pthread_mutex_t cadeadoC;
pthread_mutex_t mutexContas[NUM_CONTAS];
pthread_mutex_t mutexCount;
pthread_cond_t cond;
sem_t escrita;
sem_t leitura;
int count; /* Numero de comandos por executar. */
int fd, newF;
int contasSaldos[NUM_CONTAS];
pid_t pidFilhos[MAXFILHOS];
time_t start, end;
double diff;
void inicializarContas();
int contaExiste(int idConta);
int debitar(int idConta, int valor, int num);
int creditar(int idConta, int valor, int num);
int lerSaldo(int idConta, int num);
void simular(int numAnos);
void handler(int sig);
/*** PARTE 2 ***/
comando_t produzir(int op, int idOri, int val, int idDest, char nome[50]);
void writeBuf(comando_t item);
void* thr_consumer(void *arg);
comando_t readBuf();
int consume(comando_t item, int t_num);
int testMutexLock(pthread_mutex_t *cadeado);
int testMutexUnlock(pthread_mutex_t *cadeado);
int testSemWait(sem_t *semaforo);
int testSemPost(sem_t *semaforo);
int testSemDestroy(sem_t *semaforo);
int testMutexDestroy(pthread_mutex_t *cadeado);
/*** PARTE 3 ***/
int transferir(int idConta, int idContaDest, int valor, int num);
int debitarTransf(int idConta, int valor);
int creditarTransf(int idConta, int valor);
int lerSaldoTransf(int idConta, int num);
int min(int x, int y);
int max(int x, int y);
#endif