-
Notifications
You must be signed in to change notification settings - Fork 0
/
i-banco.h
67 lines (57 loc) · 1.8 KB
/
i-banco.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
#ifndef I_BANCO_H
#define I_BANCO_H
#include "commandlinereader.h"
#include "contas.h"
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#define COMANDO_DEBITAR "debitar"
#define COMANDO_CREDITAR "creditar"
#define COMANDO_LER_SALDO "lerSaldo"
#define COMANDO_SIMULAR "simular"
#define COMANDO_SAIR "sair"
#define COMANDO_SAIR_AGORA "agora"
#define COMANDO_TRANSFERIR "transferir"
#define OP_DEBITAR 0
#define OP_CREDITAR 1
#define OP_LER_SALDO 2
#define OP_TRANSFERIR 3
#define OP_SAIR 4
#define OP_SIMULAR 5
#define BUFFER_SIZE 100
#define NUM_TRABALHADORAS 3 /* Number of threads in pool */
#define CMD_BUFFER_DIM (2 * NUM_TRABALHADORAS) /* command buffer size */
/* Command structure */
typedef struct {
int operacao;
int idConta;
int idContaDestino;
int valor;
pid_t tpid;
} comando_t;
int sigflag = 0; /* flag signal global */
int sairflag = 0;
extern int forks; /* number of forks created */
pthread_t tid[NUM_TRABALHADORAS]; /* array containing id of threads */
comando_t cmd_buffer[CMD_BUFFER_DIM]; /* command buffer */
int buff_write_idx = 0, buff_read_idx = 0; /* write and read indexes */
int numCommands = 0; /* total waiting commands */
sem_t hasCommand, tooManyCommands;
pthread_mutex_t bufferMutex; /* Mutex for cmd buffer */
extern pthread_mutex_t accountsMutexes[];
extern pthread_mutex_t logMutex;
pthread_cond_t podeSimular;
FILE* logfile;
int cmdpipe_fd, answerpipe_fd;
char *cmdpipe = "/tmp/i-banco-pipe";
char *answerpipe = "/tmp/i-banco-answer";
int sigusr1flag = 0; /* flag signal USR 1 global */
int main (int argc, char** argv);
void sigusr1Handler();
void writeCommand(int operacao, int idConta,int idContaDestino, int valor);
void writecmd(comando_t cmd);
int readCommand();
void* worker(); /* thread main handling function */
#endif