Salut tous,

Voila je bloque sur un problème auquel je ne parviens toujours pas a trouver de solution:

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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
 
#define BUFF_SIZE   5		/* nombre total d'espaces disponibles */
#define NP          3		/* nombre total de producteurs */
#define NC          3		/* nombre total de  consommateurs */
#define NITERS      4		/* nombre d'objets produits/consommés */
 
typedef struct {
    int buf[BUFF_SIZE];   /* variable partagee */
    int in;         	  /* buf[in % BUFF_SIZE] est le premier espace libre */
    int out;        	  /* buf[out % BUFF_SIZE] est le premier espace plein */
    sem_t full;     	  /* controle du nombre d'espaces pleins */
    sem_t empty;    	  /* controle du nombre d'espaces libres */
    sem_t mutex;    	  /* mutex pour les données partagées */
} sbuf_t;
 
sbuf_t shared;
 
void *Producer(void *arg)
{
    int i, item, index;
 
    index = (int)arg;
    char c;
    for (i=0, c='A'; i < NITERS, c<='Z'; i++, c++) {
 
        /* Element produit */
        item = i;	
 
        /* Prepare to write item to buf */
 
        /* S'il n'y a aucun espace libre, on attend */
        sem_wait(&shared.empty);
        /* Si un autre thread utilise le buffer, on attend */
        sem_wait(&shared.mutex);
        shared.buf[shared.in] = item;
        shared.in = (shared.in + 1) % BUFF_SIZE;
        printf("[P%d] Produit %c ...\n", index, c);
	fflush(stdout);
        /* Relache le buffer */
        sem_post(&shared.mutex);
        /* Incremente le nombre d'espaces pleins */
        sem_post(&shared.full);
 
        if(i % 2 == 1){
	   sleep(1);
	}
 
    }
    return NULL;
}
//for(char c='a';c<='z';c++) printf("%c",c); 
 
void *Consumer(void *arg)
{
    int i, index;
 
    index = (int)arg;
    void item;
    char c;
 
    /* TODO : completer le code */
    for (i=0, c='A'; i < NITERS, c<='Z'; i++, c++) {
 
	/* S'il n'y a aucun espace libre, on attend */
        sem_wait(&shared.full);
        /* Si un autre thread utilise le buffer, on attend */
        sem_wait(&shared.mutex);
 
        item = shared.buf[shared.out];
 
        shared.out = (shared.out + 1) % BUFF_SIZE;
 
	/*********************************************************/
	printf("-----> [C%d] Consomme %c ...\n", index, c);
	fflush(stdout);
	/*********************************************************/
 
	/* Relache le buffer */
        sem_post(&shared.mutex);
 
        /* Incremente le nombre d'espaces pleins */
        sem_post(&shared.empty);
 
    }
    return NULL;
}
 
int main()
{
    pthread_t idP, idC;
    int index;
 
    sem_init(&shared.full, 0, 0);
    sem_init(&shared.empty, 0, BUFF_SIZE);
 
    /* TODO : initialiser mutex */
    sem_init(&shared.mutex,0,1);
 
 
    for (index = 0; index < NP; index++)
    {  
       /* Cree un nouveau producteur */
       pthread_create(&idP, NULL, Producer, (void*)index);
    }
 
    for (index = 0; index < NC; index++)
    { 
	/* TODO : creer NC consommateurs */
	pthread_create(&idC, NULL, Consumer, (void*)index);
    }
 
      /* TODO : attendre la fin de tous les threads avec pthread_join */
 
      pthread_join(idP, NULL);	/* attend la fin de thread Producer */
      pthread_join(idC, NULL);	/* attend la fin de thread Consumer */
 
 
    pthread_exit(NULL);
}
On me demande d'afficher en sortie cet affichage, mais sa bloque
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
[P1] Producing A ...
      [P1] Producing B ...
       ------> [C1] Consuming A ...
       ------> [C1] Consuming B ...
      [P2] Producing A ...
      [P2] Producing B ...
       ------> [C2] Consuming A ...
       ------> [C2] Consuming B ...
      [P3] Producing A ...
      [P3] Producing B ...
       ------> [C3] Consuming A ...
       ------> [C3] Consuming B ...
      [P1] Producing C ...
      [P1] Producing D ...
       ------> [C1] Consuming C ...
       ------> [C1] Consuming D ...
Alors que moi sa m'affiche cela: je ne parviens pas a commencer par
[P1]
[P1]
[C1]
[C1]


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
[P2] Produit A ...
[P2] Produit B ...
-----> [C2] Consomme A ...
[P1] Produit A ...
[P1] Produit B ...
-----> [C1] Consomme A ...
-----> [C0] Consomme A ...
-----> [C2] Consomme B ...
[P0] Produit A ...
-----> [C1] Consomme B ...
[P0] Produit B ...
-----> [C0] Consomme B ...
[P2] Produit C ...
[P2] Produit D ...
-----> [C2] Consomme C ...
-----> [C2] Consomme D ...
[P1] Produit C ...
[P1] Produit D ...
-----> [C2] Consomme E ...
-----> [C2] Consomme F ...