-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
233 lines (171 loc) · 5.76 KB
/
common.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef __COMMON_H
#define __COMMON_H
/*_____PATH_MACROS______*/
#define PORT_PATH "./ports"
#define VESSEL_PATH "./vessels"
#define WEATHER_PATH "./weather"
#define SETTINGS_PATH "./settings.txt"
/*______SEM_MACROS______*/
#define SEM_KEY 91543
/* keys for docks are the pids of the ports in order to have a specific sem for port */
/* to use with sem_cmd() to specify semop */
#define WAIT - 1
#define SIGNAL 1
#define WAIT_VESSELS -init_vessels
#define WAIT_PORTS -init_ports
/* to use with sem_cmd() to specify sem_num */
#define PORTS_SUPPLY_SEM 0
#define PORTS_DEMAND_SEM 1
#define VESSELS_SEM 2
#define START_MAIN 3
#define VESSELS_INFO_SEM 4
#define START_PORTS 5
#define START_VESSELS 6
#define HANDLER_V_SEM 7
#define WEATHER_SEM 8
#define START_WEATHER 9
#define DOCKS 0
/*______SHM_MACROS______ */
#define SHM_TIME_KEY 054271
#define SHM_GOODS_KEY 012412
#define SHM_SUPPLY_KEY 023717
#define SHM_DEMAND_KEY 012543
#define SHM_PORTSINFO_KEY 024371
#define SHM_VESSELSINFO_KEY 037744
#define SHM_GOODSINFO_KEY 034762
#define SHM_WEATHERINFO_KEY 035720
#define SHM_VESSELSPOSITIONINFO_KEY 034210
#define ARRAYVESSELPID_KEY 065143
#define PORTS_COUNTER_KEY 065214
#define VESSELS_COUNTER_KEY 025341
#define GOODS_KEY 017364
#define SIMTIME_KEY 032357
#define MAINPID_KEY 065321
#define DAILY_SUPPLY_KEY 012743
#define DAILY_DEMAND_KEY 065423
/* key for demand queue of each port is the pid of the port */
/*------------------------------------------------------------------------------------------------------------------------------------------*/
/*_______structs_______*/
/* supply is implemented as msg queues */
typedef struct {
long lot_id;
int good_type;
int quantity;
int expiration_date;
} supply;
/* SHARED MEMORY INFOS */
typedef struct {
int good_type;
int weight;
int lifetime;
} good_t;
typedef struct {
pid_t port_pid;
double x, y;
int good_type;
int quantity;
int unit_weight;
int expiration_date;
} port_supply;
typedef struct {
pid_t port_pid;
double x, y;
int good_type;
int quantity;
} port_demand;
typedef struct {
int traveling_loaded;
int traveling_unloaded;
int in_port;
} vessels_info;
typedef struct {
pid_t vessel_pid;
int in_port; /* boolean: 0=false, 1=true */
} vessel_position_info;
typedef struct {
pid_t pid;
int goods_in_port;
int goods_sent;
int goods_received;
int n_docks;
int busy_docks;
int total_produced;
int total_requested;
} ports_info;
typedef struct {
int good_type;
int in_ports;
int on_vessels;
int expired_on_vessels;
int expired_in_ports;
int delivered_to_ports;
} goods_info;
typedef struct {
int weather_pid;
int vessels_hit_num;
int vessels_sunk_num;
int ports_hit;
} weather_info;
/* global variables containing sizes or quantity of stuff init by user useful to every process */
extern int so_fill, day, init_docks, vessels_speed, vessels_capacity, load_speed, init_ports, init_vessels,
storm_duration, swell_duration;
extern size_t shm_good_size, shm_supply_size, shm_demand_size, shm_portsinfo_size, shm_vesselsinfo_size,
shm_goodsinfo_size, shm_weatherinfo_size, shm_vessels_position_info_size;
/*_________functions_prototypes:_________*/
/* semaphores */
/* get semaphore set */
int get_sem();
/* get the semaphore to access docks of the port specified in pid_port */
int get_docks(pid_t port_pid);
/* remove semaphore set */
void rm_sem(int sem_id);
/* performs the operation specified by sem_op on the sem_numth semaphore of the sem_id semaphore's set */
int sem_cmd(int sem_id, unsigned short sem_num, short sem_op, short sem_flg);
/* shared memory */
/* get + attach shm segment for goods */
int getGoodsSeg(good_t **goods_array);
/* get + attach shm segment for ports_supply */
int getSupplySeg(port_supply **supply_array);
/* get + attach shm segment for ports_demand */
int getDemandSeg(port_demand **demand_array);
/* get + attach shm segment for time variable sharing */
int getTime(double **time_var);
/* get + attach shm segment for ports info */
int getPortsInfoSeg(ports_info **info_ports);
/* get + attach shm segment for vessels info */
int getVesselsInfoSeg(vessels_info **info_vessels);
/* get + attach shm segment for goods info */
int getGoodsInfoSeg(goods_info **info_goods);
/* get + attach shm segment for weather info */
int getWeatherInfoSeg(weather_info **info_weather);
/* get + attach shm segment for vessel position info */
int getVesselPositionInfoSeg(vessel_position_info **info_position_vessels);
/* get + attach shm for array vessels pid */
int getArrayVesselsPidSeg(pid_t **array_vessels_pid);
/* get + attach shm for the port counter */
int getPortsNumber(int **port_counter);
int getGoodsNumber(int **goods_number);
int getSimTime(int **sim_time);
int getSupplyDailyFill(long **daily_supply);
int getDemandDailyFill(long **daily_demand);
/* messaged queues */
/* get the msg queue of the port specified by port_pid */
int get_msg(pid_t port_pid);
/* send a message(supply) to the queue specified by msgqid and check the errors */
int snd_msg(int msgqid, supply *to_send);
/* receive a message(supply) from the queue */
void rcv_msg(int msqid, supply *to_receive, long lot_id); /* da fare */
/* remove a message queue */
void rm_msg(int msqid);
/* nanosleep */
/* simulate travelling and loading/unloading: calculate sec eand nsec and call nanosleep */
void working(double time);
/* signals */
/* function from "progetto esempio" used to block signals */
void block_signals(int count, ...);
/* function from "progetto esempio" used to unblock signals */
void unblock_signals(int count, ...);
/* function from "progetto esempio " used to set new handler
returns the old sigaction struct */
struct sigaction set_handler(int sig, void (*func)(int));
#endif