Bonjour,

A l execution, j obtiens "Erreur lors de l envoi du message : Invalid argument", j ai verifie les types des argument du msgsnd() mais non ...

Le msgsnd se trouve ds le processusaccueil

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
 
#include <sys/types.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> #include <sys/ipc.h> 
#include <sys/msg.h> 
#include <signal.h> 
#include <errno.h> 
#include <sys/wait.h> 
#include <unistd.h> 
#include <signal.h> #include <sys/select.h> 
#include <sys/sem.h> 
#include <pthread.h>
#include <sys/shm.h>
 
#define CLE_IPCMSG 123
#define LECTURE 0
#define ECRITURE 1 
 
typedef struct msgbuf{
    long mtype;
    char mtext[1];
    int pid;
    };
 
 
int client= 0;
int pipe_client_accueil[2]; 
 
 
int msg_id ;
 
 
 
int findejournee();  
void processusaccueil(); 
void processusclient();
void processusmecano();
int client_partir(); 
int traitanterreur();
void voiture_repare();
void attente_client();
void vire_client();
void affiche();
void v();
void p();
 
int main(int argc, char* argv[]) 
{ 
    int i;
    int temps;
    int pid_fils;
 
    key_t cle_msg_attente;
 
    int client_a_creer;
    int mecano_a_creer;
    int atelier_a_creer;
    int place_attente_a_creer;
    int total;
 
    if (argc-1 < 3){
        traitanterreur(0);
    }
 
    client_a_creer = atoi(argv[1]);
    mecano_a_creer = atoi(argv[2]);
    place_attente_a_creer = atoi(argv[3]);
    total = client_a_creer + mecano_a_creer + 2;
 
    int registre[total];
 
    for (i=0 ; i < total ; ++i)
    {
        registre[i] = 0;
    }
 
    srand(time(NULL));
    temps = (rand() % 21); 
 
    cle_msg_attente = ftok("/sys/shm.h",234);
 
 
    if ((msg_id = msgget(0,IPC_CREAT|IPC_EXCL|0660)) == -1)
    {
        perror("creation de la file de messages");
        exit(0);
    }
 
 
 
      if (pipe(pipe_client_accueil) == -1)
        traitanterreur(6); 
 
      printf("Bienvenue au garage !!!\n");
 
    switch(pid_fils = fork()) 
        { 
        case -1 :
            { 
                  traitanterreur(4); 
                  break;
            }
             case  0 :
            { 
                  processusaccueil(); 
                  break;
            }
        default :
            {
            registre[0] = pid_fils;
            } 
        }
 
      for (i=1;i<=mecano_a_creer;++i)
    {    
           switch(pid_fils = fork())
           {
                case -1 :
                { 
                      traitanterreur(7);
                      break;
                    }
 
                case 0 :
                {
                      processusmecano();
                      break;
                }
            default : 
                {
                registre[i] = pid_fils;
                }
           }
      }
 
 
      for (i = 1 ;i<= client_a_creer; i++)
    {         switch(pid_fils = fork()) 
        { 
            case -1 :
                { 
                  traitanterreur(6);
                  break;
                }
 
            case 0 :
                { 
                  processusclient();
                  break;
                }
            default :
                {
                registre[mecano_a_creer + i] = pid_fils;
                } 
                }
      }
 
    sleep(60);
 
    for (i=0 ; i < total ; ++i)
    {
        kill(registre[i],SIGUSR1);
    }
 
    if ((close(pipe_client_accueil[ECRITURE])) == -1){
        perror("Erreur close pipe ecriture");
        exit(0);
    }
 
    if ((msgctl(msg_id,IPC_RMID,NULL)) == -1){
        perror("Erreur destruction fil messages");
        exit(0);
    }
 
       exit(EXIT_SUCCESS); 
} 
 
 
void processusaccueil() 
{
 
    struct sigaction fin_de_journee;
    int pidclient;
    struct controle *etat;
    struct msgbuf nvclient;
    size_t taille;
 
    taille = sizeof(struct msgbuf) - sizeof(long);
 
    fin_de_journee.sa_handler = findejournee;
 
    if ((sigaction(SIGUSR1,&fin_de_journee,NULL)) == -1)
    {
        perror("Erreur ds sigaction accueil");
        exit(1);
    }
 
    while(1){
 
          if ((read(pipe_client_accueil[LECTURE],&pidclient,sizeof(int))) == -1)
        {
            perror("read pipe client accueil");
            exit(1);
        }
 
        nvclient.pid = pidclient;
        nvclient.mtype = 0;
 
        if ((msgsnd(msg_id,&nvclient,taille,0))== -1)
        {
            perror("Erreur lors de l envoi du message");
            exit(1);
        }
 
 
    } 
} 
 
void processusmecano()
{
 
    int pidclient;
    int i;
    int temps;
 
    struct controle *etat;
    struct msgbuf client;
    struct sigaction fin_de_journee;
 
    size_t taille;
 
    taille = sizeof(struct msgbuf) - sizeof(long);
 
    fin_de_journee.sa_handler = findejournee;
 
    if ((sigaction(SIGUSR1,&fin_de_journee,NULL)) == -1)
    {
        printf("Erreur ds sigaction accueil");
        exit(1);
    }
 
    while (1){
 
        srand(time(NULL));
        temps = ((rand() % 9) +1);
 
          if ((msgrcv(msg_id,&client,taille,0,0)) == -1)
        {
            perror("read pipe accueil mecano");
            exit(2);
        }
 
        printf("Voiture en reparation\n");
        sleep(temps);
        printf("Voiture repare:%d\n",pidclient);
 
        kill(client.pid,SIGUSR2);
        printf("Au revoir et merci de votre visite!!!");
        affiche();
    }
}
 
void processusclient() 
{
 
    int pid;
    int temps;
 
    struct sigaction partir;
    struct sigaction repare;
    struct controle *etat;
    sigset_t sigmask;
 
    srand(time(NULL));
    temps = (rand() % 10 + 1);
 
    pid = getpid();
 
      partir.sa_handler = client_partir;
      sigemptyset(&partir.sa_mask);
 
    repare.sa_handler = voiture_repare;
      sigemptyset(&repare.sa_mask);
 
    sigemptyset(&sigmask);
 
      if ((sigaction(SIGUSR1,&partir,NULL)) == -1)
    {
            perror("sigaction client partir\n");
            exit(1);
    }
 
    if ((sigaction(SIGUSR2,&repare,NULL)) == -1)
    {
            perror("sigaction client repare\n");
            exit(1);
    }
 
    sleep(temps);
 
         if ((write (pipe_client_accueil[ECRITURE],&pid,sizeof(int))) == -1)
    {
        perror("write client accueil\n");
        exit(1);
    }
 
      printf("client a fini d ecrire\n");
      affiche();
      sigsuspend(&sigmask); 
} 
 
int findejournee(){
    exit(0);
}
 
int client_partir()
{
    struct controle *etat;
    printf("salle complete\n");
    affiche();
    exit(0);
}
 
void voiture_repare()
{
    struct controle *etat;    
    printf("merci , au revoir escroc\n");
    exit(0);
}
 
int traitanterreur(int erreur) 
{ 
  switch(erreur){
 
  case 0 :
    printf("Pas assez d arguments : ./projetlo41 [nbre clients] [nbremecanos] [nbreplaceattente]");
    break; 
  case 1 : 
    printf("Erreur lors de la creation de la semaphore\n"); 
    break; 
  case 2 : 
    printf("Erreur lors de la creation du pipe accueil -> mecano\n"); 
    break; 
  case 3 : 
    printf("Erreur lors de la creation du pipe mecano -> client\n"); 
    break; 
  case 4 : 
    printf("Erreur de la creation du processus accueil\n"); 
    break; 
  case 5 : 
    printf("Erreur lors de la def. du signal accueil\n"); 
    break; 
  } 
  exit(erreur); 
}
 
  /*fonction P: la val du sémaphore est décrémentée de 1 s'il est différent de 0, sinom le processus appelant est bloqué et est placé dans une file d'attente liée au sem*/
void p(int semid)
{
    struct sembuf  sempar;
 
    sempar.sem_num=0;
    sempar.sem_op=-1;
    sempar.sem_flg=0;
 
    if(semop(semid, &sempar, 1)==-1)
        perror("Erreur lors de P!!!");
}
 
/*fonction V: la val de la sémaphore est incrémentée de 1 s'il n'y a pas de processus dans la file d'attente, sinom la sem reste inchangée et on libère le 1er proces.*/
void v(int semid)
{
    struct sembuf sempar;
 
    sempar.sem_num=0;
    sempar.sem_op=1;
    sempar.sem_flg=0;
 
    if(semop(semid, &sempar, 1)==-1)
        perror("Erreur lors de V!!!");
}