-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedmem.c
69 lines (47 loc) · 1.47 KB
/
sharedmem.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
/*
Operating Systems - Final Project
@author Miguel Dinis | 2019219010 | miguelbarroso@student.dei.uc.pt | github.com/MigDinny
@author Rodrigo Ferreira | 2019220060 | rferreira@student.dei.uc.pt | github.com/IronMan988
*/
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include "include.h"
/*
@TODO: #4 init shmem values!!! they probably must not be null or 0.
first proccess to acquire lock to shared memory is kinda random which can bring some unexpected results
*/
int init_shared_memory() {
shmid = shmget(IPC_PRIVATE, sizeof(shmem_t) + sizeof(team_t)*config.nTeams + sizeof(car_t)*config.nTeams*config.nCars , IPC_CREAT|0700);
if (shmid < 0) return -1;
shmem = (shmem_t*) shmat(shmid, NULL, 0);
if (shmem == (shmem_t*) - 1) return -1;
// init values
shmem->status = OFF;
shmem->nTeams = 0;
shmem->notSIGUSR1 = 1;
shmem->finishing = 0;
shmem->runningCarsTotal = 0;
shmem->quitCars = 0;
shmem->lastCarID = -1;
for (int i = 0; i < 5; i++) {
shmem->carsWIDs[i] = -1;
}
teams = (team_t*) (shmem+1);
cars = (car_t*) (teams + config.nTeams);
return 0;
}
sem_t *init_shared_memory_mutex() {
sem_unlink("MUTEX");
return sem_open("MUTEX", O_CREAT|O_EXCL,0700, 1);
}
void clean_all_shared() {
sem_close(shmutex);
sem_unlink("MUTEX");
shmdt(shmem);
shmctl(shmid, IPC_RMID, NULL);
}