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
| void consum(shared_mem *shm) {
int quantite;
sem_getvalue(&shm -> quantite, &quantite);
if(quantite > 0) {
if(sem_wait(&shm -> mutex) == -1) {
perror("sem_wait");
exit(EXIT_FAILURE);
}
for(int i = 0; i < shm -> n; ++i) {
errno = 0;
char c = shm -> buffer[shm -> queue];
if(write(STDOUT_FILENO, &c, 1) == -1 ) {
if(errno != 0) {
perror("write");
exit(EXIT_FAILURE);
} else {
fprintf(stderr, "write: can not write requested data\n");
exit(EXIT_FAILURE);
}
}
shm -> queue += 1;
if(shm -> queue == BUFFER_SIZE) {
shm -> queue = 0;
}
}
if (sem_post(&shm -> mutex) == -1) {
perror("sem_post");
exit(EXIT_FAILURE);
}
for(int i = 0; i <= shm -> n; ++i) {
if(sem_wait(&shm -> quantite) == -1) {
perror("sem_wait");
exit(EXIT_FAILURE);
}
}
}
} |
Partager