-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshared_memory.h
More file actions
115 lines (73 loc) · 2.3 KB
/
shared_memory.h
File metadata and controls
115 lines (73 loc) · 2.3 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
112
113
114
115
#ifndef __SHARED_MEMORY__
#define __SHARED_MEMORY__
#define BAYS 3
#define SPOTS 4
#define MAX_ID_LENGTH 20
#define SEGMENT_PERM 0666
#define SHARED_MEMORY_KEY 1700146
#define MAX_FILENAME_LENGTH 25
#define MAX_BUFFER_SIZE 256
#define MAX_SLEEP_TIME 10
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <semaphore.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include "bus_funcs.h"
typedef struct Reference_Ledger
{
uint32_t waiting_to_enter;
uint8_t requesting_access_to_enter;
uint8_t is_ready_to_enter;
uint8_t has_parked;
uint32_t requesting_access_to_exit;
uint8_t has_left;
uint8_t exit_traffic;
Destination entering_bus_type;
Destination islet_for_entering_bus;
Bus_Bay bays[BAYS];
uint64_t sum_of_waiting_to_park_times;
uint16_t counter_waiting_to_park_times;
uint64_t sum_of_waiting_to_leave_times;
uint16_t counter_waiting_to_leave_times;
uint32_t buses_departed;
uint32_t total_buses;
} Reference_Ledger;
typedef struct shared_segment
{
Reference_Ledger ledger;
/* pretty much self explanatory */
sem_t station_manager_mutex;
/* used for entrance in the station */
sem_t entry_queue;
sem_t park_mutex;
/* used for exiting the station */
sem_t exit_queue;
/* used to preserve the "traffic critical section" of the shared segment */
sem_t traffic_mutex;
/* used to preserve the "bay critical section" of the shared segment */
sem_t bays_mutex[BAYS];
/* used to access logfile: many writers (buses), 1 reader (compotroller) */
sem_t logfile_mutex;
/* used to access and change statistics for the comptroller and buses */
sem_t statistics_mutex;
char logfile_name[MAX_FILENAME_LENGTH];
} shared_segment_t;
int get_shared_segment(key_t shmid_key, int* shmid, shared_segment_t** shared_memory);
int de_attach_shared_segment(shared_segment_t* shared_memory);
int initialize_shared_segment(shared_segment_t* shared_memory, uint32_t total_buses, char* logfile_name);
int free_shared_segment_memory(shared_segment_t* shared_memory);
int free_shared_segment(shared_segment_t* shared_memory, int shmid);
#endif