-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hacker.c
160 lines (134 loc) · 3.56 KB
/
Hacker.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
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
// default libs
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <time.h>
#include <string.h>
#include <sys/shm.h>
#include <signal.h>
#include <unistd.h>
int sem, memcp, memp;
int nbArrivee;
int *type_win_hack;
struct sembuf Pmutex = {0, -1, 0};
struct sembuf Vmutex = {0, 1, 0};
struct sembuf PwindowsQueue = {1, -1, 0};
struct sembuf VwindowsQueue = {1, 1, 0};
struct sembuf PhackerQueue = {2, -1, 0};
struct sembuf VhackerQueue = {2, 1, 0};
struct sembuf Psembarrier = {3, -1, 0};
struct sembuf Vsembarrier = {3, 1, 0};
struct sembuf Pmutex2 = {4, -1, 0};
struct sembuf Vmutex2 = {4, 1, 0};
void barrier(int n)
{
semop(sem, &Pmutex2, 1);
type_win_hack[2]++;
// printf("on board now %d", &type_win_hack[2]);
if (type_win_hack[2] >= n)
{
semop(sem, &Vmutex2, 1);
for (int i = 0; i < n; i++)
{
semop(sem, &Vsembarrier, 1);
// nbArrivee = 0;
}
}
else
{
semop(sem, &Vmutex2, 1);
semop(sem, &Psembarrier, 1);
}
}
void row_boat(int p)
{
int i;
printf("bateau demmarer par processus %d!!\n", p);
}; // end row
// la fct handler pour qst 6
void handler()
{
printf("\nje vais quitter\n");
row_boat(getpid());
exit(2);
}
void declarer_arrivee(int n_hackers, int n_windows)
{
printf("boaaard h: %d, w: %d\n", n_hackers, n_windows);
} // end print boat
int main(void)
{
int is_captain = 0;
int p;
key_t clef;
// printf("\nstarting hacker program");
// recuperatino des semaphores
clef = ftok("./creat.c", 2);
if (clef == -1)
{
printf("\n erreur : la clé n’a pas été créée");
exit(1);
}
sem = semget(clef, 5, 0666);
if (sem == -1)
{
printf("\n Erreur de récupération des sémaphores");
exit(2);
}
// recuperation de la memoire partagee
clef = ftok("creat.c", 3);
memp = shmget(clef, 3 * sizeof(int), 0666);
if (memp == -1)
{
printf("\n Erreur de récupération de la mémoire partagée");
exit(2);
}
/* Attachement des segments partagés */
type_win_hack = shmat(memp, 0, 0);
///////////////////////////////////////////////
// it's a windows!
semop(sem, &Pmutex, 1);
type_win_hack[0]++;
// The last processus enters here:
if (type_win_hack[0] == 4)
{
for (int i = 0; i < 4; i++)
semop(sem, &VwindowsQueue, 1);
declarer_arrivee(type_win_hack[0], type_win_hack[1]);
is_captain = 1;
printf("\ni'm captain my pid est %d\n", getpid());
// ajoutee pour la question 6
signal(SIGINT, handler);
}
else if (type_win_hack[1] == 2 && type_win_hack[0] == 2)
{
semop(sem, &VwindowsQueue, 1);
semop(sem, &VhackerQueue, 1);
semop(sem, &VhackerQueue, 1);
declarer_arrivee(type_win_hack[0], type_win_hack[1]);
is_captain = 1; // it becomes the captain
printf("\ni'm captain my pid est %d\n", getpid());
// ajoutee pour la question 6
signal(SIGINT, handler);
}
else
{
// If it's not the last thread, it enters here
// ajouter pour faire kill 2 pour n'importe quel process
printf("\ni'm not captain my pid est %d\n", getpid());
semop(sem, &Vmutex, 1);
declarer_arrivee(type_win_hack[0], type_win_hack[1]);
}
barrier(4);
sleep(15);
if (is_captain == 1)
{
// kill(p, 2);
row_boat(getpid());
semop(sem, &Vmutex, 1);
}
sleep(1);
return 0;
}