Bonjour.
Le but de mon programme est d'implémenter le problème du lecteur/écrivain sur un tampon borné de 10 cases (ici des char), en utilisant donc des sémaphores.
Ces dernières ont été implémentées à la main en utilisant des conditions.

Le programme est composé de deux threads : un lecteur (le thread principal) et un écrivain (le thread créé).
Le thread producteur lit des caractères sur l'entrée et les place dans le buffer, que le thread principal lit et affiche sur la sortie standard.
Dès que le producteur lit un EOF sur l'entrée standard, il doit le placer dans le buffer et arrêter sa production.
Le lecteur s'arrêtera donc quant-à lui quand il lira un EOF dans le buffer.

Voici mon programme actuellement :
Code c : 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
 
#include <stdio.h>
#include <semaphore.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
 
void raler (char * msg)
{
    perror(msg);
    exit(EXIT_FAILURE);
}
 
typedef struct {
    int compteur;
    pthread_mutex_t * mut;
    pthread_cond_t *cond ;
} monsem_t;
 
struct tampon {
    char buf[10];
    monsem_t *sem_prod;
    monsem_t *sem_ecr; // ecrites
    monsem_t *sem_libres;
    monsem_t * sem_lec;
    int id_prod;
    int id_cons;
};
 
struct tampon * tampon;
 
int monsem_init(monsem_t * sem, int init)
{
    int retour =0;
    if(init < 0){
        retour =-1;
    } else {
        sem->mut=malloc(sizeof(pthread_mutex_t));
        if(sem->mut == NULL) retour =-1;
 
        if((errno =pthread_mutex_init (sem->mut,NULL)) >0 )
            retour=-1;
 
        sem->cond=malloc(sizeof(pthread_cond_t));
        if(sem->cond == NULL) retour =-1;
 
        if((errno =pthread_cond_init (sem->cond,NULL)) >0 )
            retour=-1;
 
        sem->compteur=init;
    }
 
return retour;
}
 
int monsem_p(monsem_t * sem)
{   
    int retour =0;
    if((errno = pthread_mutex_lock(sem->mut)) >0)
            retour =-1;
 
    sem->compteur--;    
    while(sem->compteur < 0)
        pthread_cond_wait(sem->cond,sem->mut);
 
    if((errno = pthread_mutex_unlock(sem->mut)) >0)
        retour=-1;
 
return retour;
}
 
int monsem_v (monsem_t * sem)
{   
    int retour =0;
    if((errno = pthread_mutex_lock(sem->mut)) >0)
        retour =-1;
 
    sem->compteur++;
 
    if(sem->compteur <= 0)
        pthread_cond_signal(sem->cond);
 
    if((errno = pthread_mutex_unlock(sem->mut)) >0)
        retour=-1;
 
return retour;
}
 
void * thread_produit (void * args)
{
    struct tampon *ag=(struct tampon *)args;
    char d='\0';
    int boucle =1;
    while(boucle){
        read(0,&d,sizeof(char));
        if(monsem_p(ag->sem_libres) == -1)
            raler("monsem_wait");
 
        if(monsem_p(ag->sem_prod) == -1)
            raler("sem_wait");
 
        printf("P %c\n",d);
        ag->buf[ag->id_prod]=d;
        ag->id_prod=(ag->id_prod+1)%10;
 
        if(monsem_v(ag->sem_prod) == -1)
            raler("monsem_wait");
 
        if(monsem_v(ag->sem_ecr) == -1)
            raler("sem_wait");
 
        if(d==EOF){
            boucle=0;
        }
    }
pthread_exit(NULL);
}
 
int main(){
 
    struct tampon * tampon;
    if( (tampon=malloc(sizeof (struct tampon)) ) == NULL)
        raler("malloc");
 
    monsem_t sem_lec; // Lecteur
    monsem_t sem_prod; // Écrivain
    monsem_t sem_ecr; // Écrites
    monsem_t sem_libres; // Libres
 
    if((errno = monsem_init(&sem_prod,1)) > 0)
        raler("sem_init");
 
    if((errno = monsem_init(&sem_lec,1)) > 0)
        raler("sem_init");
 
    if((errno = monsem_init(&sem_ecr,0)) > 0)
        raler("sem_init");
 
    if((errno = monsem_init(&sem_libres,10)) > 0)
        raler("sem_init");
 
    tampon->sem_ecr=&sem_ecr;
    tampon->sem_lec=&sem_lec;
    tampon->sem_prod=&sem_prod;
    tampon->sem_libres=&sem_libres;
    tampon->id_prod=0;
    tampon->id_cons=0;
 
    pthread_t prod_tid;
    if((errno = pthread_create(&prod_tid,NULL,thread_produit,tampon)) > 0)
            raler("Erreur prod pthread create ");
 
    int boucle=1;
    while(boucle){
 
        if(monsem_p(tampon->sem_ecr) == -1)
            raler("sem_wait");
 
        if(monsem_p(tampon->sem_lec) == -1)
            raler("sem_wait");
 
 
        if(tampon->buf[tampon->id_cons] == EOF){
            boucle = 0;
        } else {
            printf("C %c\n",tampon->buf[tampon->id_cons]);
            tampon->id_cons=(tampon->id_cons+1)%10;
        }
        if(monsem_v(tampon->sem_lec) == -1)
            raler("sem_post : semaphore invalide");
 
        if(monsem_v(tampon->sem_libres) == -1)
            raler("sem_post:semaphore invalide");
 
    }
 
    if((errno=pthread_join(prod_tid,NULL)) > 0)
        raler("pthread_join");
 
    if((errno = pthread_cond_destroy(tampon->sem_ecr->cond))!=0)
        raler("pthread_cond_destroy");
 
    if((errno = pthread_cond_destroy(tampon->sem_lec->cond))!=0)
            raler("pthread_cond_destroy");
 
    if((errno = pthread_cond_destroy(tampon->sem_libres->cond))!=0)
            raler("pthread_cond_destroy");
 
    if((errno = pthread_cond_destroy(tampon->sem_prod->cond))!=0)
        raler("pthread_cond_destroy");
 
free(tampon);
return 0;
}

Le programme est censé afficher quelquechose comme:
P char1
C char1
P char2
C char2

Deux problèmes :
- Lorsque j’exécute mon programme, je me retrouve avec quelquechose du type :
P char1
P
C char1
C

- Lorsque je lui envoie un EOF, il boucle à l'infini en affichant des P et des C non suivis par un caractère.

Je ne trouve pas à quel endroit cela bloque ...
Merci pour votre aide.