-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprod_prodcons_cons.c
More file actions
305 lines (258 loc) · 5.56 KB
/
Copy pathprod_prodcons_cons.c
File metadata and controls
305 lines (258 loc) · 5.56 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <sys/wait.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/stat.h>
#define T 5
#define N 20
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
};
struct Tampon
{
pid_t buffer[T];
int existeElement;
unsigned dbt;
unsigned fin;
};
struct my_msgbuf
{
long mtype;
char mtext[200];
};
int shmid;
int semid;
int msqid;
struct Tampon *tampon;
struct sembuf sem = {0, 0, 0};
struct my_msgbuf buf;
void prodCons();
void cons();
void prod();
void sem_init(int semid, int num, int val);
void sem_action(int semid, int num, int sem_operation, struct sembuf *p);
enum
{
FULL,
EMPT,
PRIV
};
int main(int argc, char const *argv[])
{
key_t key = ftok(argv[0], 2);
/*
* Allouer de l’espace pour le tampon
*/
if ((shmid = shmget(key, sizeof(struct Tampon), S_IRUSR | S_IWUSR | IPC_CREAT)) == -1)
{
perror("Echec de shmget\n");
error(1);
}
/*
* Creation de 3 semaphores
*/
if ((semid = semget(key, 3, S_IRUSR | S_IWUSR | IPC_CREAT)) == -1)
{
perror("creation des semaphores non effectuee\n");
error(1);
}
/*
* Creation de la file de messages
*/
if ((msqid = msgget(key, S_IRUSR | S_IWUSR | IPC_CREAT)) == -1)
{
perror("creation de la file impossible\n");
exit(1);
}
buf.mtype = 1;
/*
* Attachement du processus a la zone de memoire
*/
if ((tampon = shmat(shmid, NULL, 0)) == -1)
{
perror("attachement impossible\n");
error(1);
}
tampon->dbt = 0;
tampon->fin = 0;
tampon->existeElement = 0;
sem_init(semid, FULL, 0);
sem_init(semid, EMPT, T);
sem_init(semid, PRIV, 0);
/*
* Call Producteur
*/
if (fork() == 0)
prod();
/*
* producteurConsomateur
*/
if (fork() == 0)
prodCons();
/*
* Consomateur
*/
if (fork() == 0)
cons();
/*
* Affichages des etats du tampon
*/
int cpt;
int i = 0;
while (1)
{
cpt = 0;
if (tampon->existeElement)
{
i = tampon->dbt;
printf("\nTampon : ");
do
{
cpt++;
printf("%d\t", tampon->buffer[i]);
i = (i + 1) % T;
} while (i != tampon->fin);
printf("\n");
}
else
printf("\nTampon est vide !!!\n");
sleep(1);
}
return 0;
}
void prod()
{
char str[200];
int cpt = 1;
while (cpt <= N)
{
sprintf(str, "%d", cpt);
strncpy(buf.mtext, str, 2);
int len = strlen(buf.mtext);
/*
* envoi d'un message
*/
if (msgsnd(msqid, &buf, len + 1, 0) == -1)
{ /* +1 for '\0' */
perror("envoi impossible\n");
exit(1);
}
printf("\nProd ==> %d\n", cpt);
cpt++;
sleep(1);
}
printf("Prod Termine\n");
exit(0);
}
void prodCons()
{
int cpt;
/*
* attachement du processus a la zone de memoire
*/
if ((tampon = shmat(shmid, NULL, 0)) == -1)
{
perror("attachement impossible\n");
error(1);
}
do
{
sem_action(semid, EMPT, -1, &sem);
/*
* reception d'un message
*/
if (msgrcv(msqid, &buf, sizeof buf.mtext, 0, 0) == -1)
{
perror("reception impossible\n");
exit(1);
}
cpt = atoi(buf.mtext);
tampon->buffer[tampon->fin] = atoi(buf.mtext);
tampon->fin = (tampon->fin + 1) % (T);
tampon->existeElement = 1;
sem_action(semid, FULL, 1, &sem);
printf("\nProdCons ==> %d\n", cpt);
sleep(1);
} while (cpt < N);
/*
* Detruire la file de messages
*/
sem_action(semid, PRIV, -1, &sem);
msgctl(msqid, IPC_RMID, NULL);
printf("\nprodCons est termine\n");
exit(0);
}
void cons()
{
int value;
/*
* attachement du processus a la zone de memoire
*/
if ((tampon = shmat(shmid, NULL, 0)) == -1)
{
perror("attachement impossible\n");
error(1);
}
do
{
sem_action(semid, FULL, -1, &sem);
value = tampon->buffer[tampon->dbt];
tampon->dbt = (tampon->dbt + 1) % (T);
if (tampon->dbt == tampon->fin)
tampon->existeElement = 0;
sem_action(semid, EMPT, 1, &sem);
printf("\nCons ==> %d\n", value);
sleep(2);
} while (value < N);
sem_action(semid, PRIV, 1, &sem);
/*
* detachement du segment
*/
if (shmdt(tampon) == -1)
{
perror("detachement impossible");
exit(1);
}
/*
* destruction l'ensemble de semaphores
*/
semctl(semid, 0, IPC_RMID);
/*
* destruction du segment
*/
if (shmctl(shmid, IPC_RMID, NULL) == -1)
{
perror("destruction impossible");
exit(1);
}
printf("\nCons est termine\n");
exit(0);
}
void sem_init(int semid, int num, int val)
{
union semun arg;
arg.val = val;
if (semctl(semid, num, SETVAL, arg) == -1)
{
perror("semctl\n");
error(1);
}
}
void sem_action(int semid, int num, int opr, struct sembuf *p)
{
p->sem_num = num; /* numero de semaphore */
p->sem_op = opr; /* operation de (decrementation/incrementation) */
p->sem_flg = SEM_UNDO; /* pour defaire les operations */
if (semop(semid, p, 1) == -1)
{
perror("semop()");
exit(1);
}
}