/
ger_cl.c
151 lines (118 loc) · 3.95 KB
/
ger_cl.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "logger.h"
/*This function chooses the bets balcao to join based on the number of clients in queue.
The chosen balcao will be the one that has the fewer clients on queue.
*/
void *chooseBalcao(void *arg){
void *ret;
int *pt = (int *)arg;
int balcao = 0, minClientes = INT_MAX, numBalcoes = pt[NUMOFBALCOES], currentBalcao = 1;
while(currentBalcao <= numBalcoes)
{
if(pt[BALCAODEFINING + NUMOFBALCAOVARIABLES*(currentBalcao-1) + 2] == -1)
if(minClientes > pt[BALCAODEFINING + NUMOFBALCAOVARIABLES*(currentBalcao-1) + 4])
{
minClientes = pt[BALCAODEFINING + NUMOFBALCAOVARIABLES*(currentBalcao-1) + 4];
balcao = currentBalcao;
}
currentBalcao++;
}
ret = malloc(sizeof(int));
*(int *)ret = balcao;
return ret;
}
int main(int argc, char *argv[])
{
//argv[1] = name of the shared memory
//argv[2] = number of clients
/*confirmação da existencia de memoria partilhada*/
char memPath[DEFAULT_STRING_SIZE];
sprintf(memPath,"%s",argv[1]);
int shmfd = shm_open(memPath,O_RDWR,0600);
if (argc != 3){
fprintf( stderr, "Usage: %s <memoriaPartilhada> <numdeClientes>\n", argv[0]);
exit(1);
}
int numberOfCli;
sscanf(argv[2],"%i",&numberOfCli);
if(numberOfCli <= 0)
{
fprintf(stderr, "Numero de Clientes Invalido\n");
exit(2);
}
if(shmfd == -1){
printf("Não há memória partilhada aberta!\n");
exit(3);
}
if (ftruncate(shmfd,SHM_SIZE) < 0)
{
fprintf(stderr, "Erro ao executar truncate da SHM -> %s\n", strerror(errno));
exit(4);
};
int *pt = (int *) mmap(0,SHM_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,shmfd,0);
if(pt == MAP_FAILED)
{
fprintf(stderr, "Erro ao executar o mapeamento da SHM -> %s\n", strerror(errno));
exit(5);
}
if(pt[NUMOFOPENBALCOES] == 0){
printf("Não há balcoes abertos!\n");
exit(6);
};
char semName[DEFAULT_STRING_SIZE];
sprintf(semName,"/%s",memPath);
sem_t *sem = sem_open(semName,0,0600,1);
if(sem == SEM_FAILED)
{
perror("WRITER failure in sem_open()");
exit(4);
}
pid_t pid;
/*Add number of clients to process
Is used to stop the balcao from closing while its being chosen*/
sem_wait(sem);
pt[NUMBEROFCLIENTS] = pt[NUMBEROFCLIENTS] + numberOfCli;
sem_post(sem);
//cycle to generate the multiple clients
while(numberOfCli > 0){
pid = fork();
if(pid < 0){
printf("Erro ao executar o fork de criação de Clientes\n");
exit(7);
}
else if(pid == 0){
/*Create client FIFO*/
char privateFIFOPathname[DEFAULT_STRING_SIZE], balcaoFIFO[DEFAULT_STRING_SIZE], balcaoAnswer[DEFAULT_STRING_SIZE];
int currentPID = getpid();
sprintf(privateFIFOPathname,"/tmp/fc_%d",currentPID);
if(mkfifo(privateFIFOPathname,0660) < 0){
printf("Erro ao criar o fifo privado do Cliente\n");
exit(8);
}
pthread_t tid;
void *retval; //return value of the chosen balcao
sem_wait(sem);//using this semaphore to acess the shm, if not used the chosen balcao might be wrong
pthread_create(&tid,NULL,chooseBalcao,pt);//choose what balcao to join
pthread_join(tid,&retval);
int chosenBalcao = *(int *)retval;
free (retval);// free the malloc in chooseBalcao
pt[BALCAODEFINING + NUMOFBALCAOVARIABLES*(chosenBalcao-1) + 4]++;
sprintf(balcaoFIFO,"/tmp/fb_%d",pt[BALCAODEFINING + NUMOFBALCAOVARIABLES*(chosenBalcao-1) + 3]);//getting the chosen balcao FIFO ID
int fdBalcao = open(balcaoFIFO,O_WRONLY);
write(fdBalcao,privateFIFOPathname,strlen(privateFIFOPathname)+1);//writing to the chosen balcao the client FIFO ID
close(fdBalcao);
writeLOG(memPath,time(NULL),CLIENT,chosenBalcao,CLIASKSSERVICE,privateFIFOPathname);
sem_post(sem);
int fd = open(privateFIFOPathname,O_RDONLY);
read(fd,balcaoAnswer,FIMDEATENDIMENTOSIZE+1);
if(strcmp(balcaoAnswer,FIMDEATENDIMENTO) == 0)
writeLOG(memPath,time(NULL),CLIENT,chosenBalcao,CLIENTENDSERVICE,privateFIFOPathname);
unlink(privateFIFOPathname);
sem_close(sem);
return 0;
}
else{
numberOfCli--;
}
}
exit(0);
}