-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
61 lines (44 loc) · 1.35 KB
/
server.cpp
File metadata and controls
61 lines (44 loc) · 1.35 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
#include "procedure.h"
#define TUTTI 0
#define DIM 1000
int main(int argc,char* argv[]){
int IdCodaInvio;
int IdCodaRicezione;
pthread_attr_t attributo;
pthread_t Thread_Server[DIM];
buf* b;
int numeroT;
numeroT=0;
pthread_attr_init(&attributo);
pthread_attr_setdetachstate(&attributo,PTHREAD_CREATE_JOINABLE);
InstanziaCode(IdCodaInvio,IdCodaRicezione);
while(1){
msg NuovoMessaggio;
RiceviMessaggio(NuovoMessaggio,IdCodaInvio,TUTTI);
b=(buf*) malloc(sizeof(buf));
//inizializziamo il monitor
pthread_mutex_init(&(b->mutex),0);
b->pid=NuovoMessaggio.pid;
b->valore1=NuovoMessaggio.valore1;
b->valore2=NuovoMessaggio.valore2;
b->idCoda=IdCodaRicezione;
//verifico se è il terminatore
if((NuovoMessaggio.valore1 == -1) && (NuovoMessaggio.valore2 == -1))
{
NuovoMessaggio.msgtype=NuovoMessaggio.pid;
InviaMessaggio(NuovoMessaggio,IdCodaRicezione);
printf("\nTerminatore ricevuto,inizio procedure di terminazione\n");
for(int i=0;i<=numeroT;i++){
pthread_join(Thread_Server[numeroT],0);
}
pthread_attr_destroy(&attributo);
pthread_mutex_destroy(&(b->mutex));
sleep(5); //do il tempo ai client di consumare gli ultimi messaggi
RimuoviCode(IdCodaInvio,IdCodaRicezione);
exit(0);
}
pthread_create(&(Thread_Server[numeroT]),&attributo,consuma,(void*)b);
numeroT=numeroT+1;
}
return 0;
}