-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.h
More file actions
111 lines (84 loc) · 3.63 KB
/
functions.h
File metadata and controls
111 lines (84 loc) · 3.63 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
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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
/* longitud del puente,
media de la distribución exponencial del tiempo entre llegadas de nuevos automóviles,
speed promedio de los automóviles,
valores para Ki ,
tiempo de duración de la luz verde del semáforo
rango inferior y superior de las speedes de cada lado. */
#define TRUE 1
#define FALSE 0
#define EAST 0 /* east bound */
#define WEST 1 /* west bound */
#define TIMER 100000
static int CurrentDirection; /* current direction of cars*/
static int VehicleBridgeCount; /* # of vehicle on bridge */
static int Waiting[2]; /* # east/west bound waiting*/
static pthread_mutex_t puedePasar = PTHREAD_MUTEX_INITIALIZER; /* monitor lock */
static pthread_cond_t EastWest[2]; /* blocking east/west cars */
pthread_mutex_t *Bridge;
static int BridgeSize, /* longitud del puente en unidades */
TimeNewArrival, /* media de la distribución exponencial del tiempo entre llegadas de nuevos automóviles */
AvrgSpeed, /* speed promedio de los automóviles */
GreenLightTime, /* tiempo de duración de la luz verde del semáforo */
WestMaxSpeed, /* rango superior del oeste */
WestMinSpeed, /* rango inferior del oeste */
EastMaxSpeed, /* rango superior del este */
EastMinSpeed, /* rango inferior del este */
VehicleBridgeCount, /* # of vehicles on bridge */
Ambulance[2],
Waiting[2],
*positions; /* # east/west bound waiting*/
int BRIDGE_SIZE; /* max vehicle on bridge */
//int semaphoreTime; /* tiempo de la luz verde/roja en cambiar */
struct car{
int id; /* el ID del carro actual de este */
int speed;
int ambulance;// 0 = false, !0 = true
};
int nextCarCreation(int media){ /* −media ∗ ln(1 − r) */
//srand((unsigned int)time(NULL));
double r = (double)rand() / (RAND_MAX + 1.0);
return ((-media) * (log(1 - r)));
}
int carSpeed(int minS, int maxS, int avrgS) {
int range = maxS - minS + 1; // Total range of values
int r = rand() % range; // Random value in range [0, range-1]
// Map r to a value with tendency towards avrgS
int biasedSpeed = minS + r; // Value in the range [minS, maxS]
// Adjust to introduce bias towards avrgS
int deviation = abs(biasedSpeed - avrgS);
int adjustedSpeed = avrgS - deviation + (rand() % (2 * deviation + 1));
// Ensure the result is within bounds
if (adjustedSpeed < minS) adjustedSpeed = minS;
if (adjustedSpeed > maxS) adjustedSpeed = maxS;
return adjustedSpeed;
}
int isAmbulance(){
int r = rand() % 100;
return (r < 90) ? FALSE : TRUE;
}
int r01(){ //random entre 0 y 1, 0 east, 1 west
return rand() % 2;
}
void printPosiciones() {// PRINT POSICIONES VA A SER CADA 0.5 SEGUNDOS
printf("\033[34m%d]\033[0m ", positions[0]);
for (int i = 1; i <= BRIDGE_SIZE ; i++)
printf("%d ", positions[i]);
printf("\033[34m[%d\033[0m ◖⚆ᴥ⚆◗ → ", positions[BRIDGE_SIZE + 1]);
}
void BridgeInit(void){
VehicleBridgeCount = 0; /* no vehicle on bridge */
Waiting[0] = Waiting[1] = 0; /* no vehicle waiting */
for(int i = 0; i <= BRIDGE_SIZE + 1; i++)
pthread_mutex_init(&Bridge[i], NULL); /* Initialize todos los mutex */
}
void puedePasarInit(){
pthread_mutex_init(&puedePasar, NULL);
}