-
Notifications
You must be signed in to change notification settings - Fork 0
/
cabinet.c
72 lines (50 loc) · 1.71 KB
/
cabinet.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <semaphore.h>
#include "thread_pool.h"
void* createPatient(void* args){
//Give a random time before creating a patient
sleep(rand() % GEN_PATIENT_TIME);
int nextId = *((int*)args);
Task t = {
// Give next id
.patientId = nextId,
};
timespec_get(&t.waitBegin, TIME_UTC);
submitTask(t);
}
int main(int arc, char * argv[]){
pthread_mutex_init(&mutexQueue, NULL);
pthread_cond_init(&condQueue, NULL);
pthread_t doctors[DOCTOR_NUM];
pthread_t patients[PATIENT_NUM];
int patientArgs[PATIENT_NUM];
activePatients = PATIENT_NUM;
// Create Doctors threads
for (int i = 0; i < DOCTOR_NUM; i++) {
if (pthread_create(&doctors[i], NULL, &startThread, NULL) != 0)
perror("Failed to create doctor thread");
// Memorize the thread for id later
doctorId[i] = doctors[i];
}
srand(time(NULL));
// Create Patients
for(int i = 1 ; i <= PATIENT_NUM ; i++){
patientArgs[i-1] = i;
if(pthread_create(&patients[i-1], NULL, createPatient, &patientArgs[i-1]))
perror("Failed to create patient thread");
}
// Join Patients
for(int i = 0 ; i < PATIENT_NUM ; i++)
if (pthread_join(patients[i], NULL) != 0)
perror("Failted to join patient thread");
// Join Doctors
for (int i = 0; i < DOCTOR_NUM; i++)
if (pthread_join(doctors[i], NULL) != 0)
perror("Failed to join doctor thread");
pthread_mutex_destroy(&mutexQueue);
pthread_cond_destroy(&condQueue);
return 0;
}