Change size of shared memory to MAX_SIZE instead of CIRC_BUF_SIZE

This commit is contained in:
zenon 2018-06-08 10:58:01 +02:00
parent 8605c6e820
commit 065f445c79
3 changed files with 4 additions and 7 deletions

View File

@ -35,13 +35,12 @@
#define SEM_WRITE_END "/write_end_01527193" #define SEM_WRITE_END "/write_end_01527193"
#define MAX_ITEMS 16 #define MAX_ITEMS 16
#define CIRC_BUF_SIZE 17
struct circ_buf { struct circ_buf {
int quit; int quit;
int head; int head;
int tail; int tail;
int data[CIRC_BUF_SIZE]; int data[MAX_ITEMS];
}; };
#endif /* ifndef COMMON_H */ #endif /* ifndef COMMON_H */

View File

@ -228,14 +228,14 @@ void putSolution(int *solution)
if (solution[0] == -1) { if (solution[0] == -1) {
wait_sem(sFreeSpace); wait_sem(sFreeSpace);
shared->data[shared->tail] = -1; shared->data[shared->tail] = -1;
shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE; shared->tail = (shared->tail + 1) % MAX_ITEMS;
sem_post(sUsedSpace); sem_post(sUsedSpace);
} else { } else {
int i = 0; int i = 0;
while (solution[i] != -1) { while (solution[i] != -1) {
wait_sem(sFreeSpace); wait_sem(sFreeSpace);
shared->data[shared->tail] = solution[i]; shared->data[shared->tail] = solution[i];
shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE; shared->tail = (shared->tail + 1) % MAX_ITEMS;
sem_post(sUsedSpace); sem_post(sUsedSpace);
i++; i++;
} }

View File

@ -62,7 +62,7 @@ void cleanup()
void initCircBuf() void initCircBuf()
{ {
shared->quit = 0; shared->quit = 0;
for (int i = 0; i < CIRC_BUF_SIZE; i++) { for (int i = 0; i < MAX_ITEMS; i++) {
shared->data[i] = -1; shared->data[i] = -1;
} }
return; return;
@ -92,9 +92,7 @@ void getSolution(sem_t *sUsedSpace, sem_t *sFreeSpace)
} }
if (shared->data[shared->head] == -1) { if (shared->data[shared->head] == -1) {
wait_sem(sUsedSpace);
shared->head = (shared->head + 1) % MAX_ITEMS; shared->head = (shared->head + 1) % MAX_ITEMS;
sem_post(sFreeSpace);
} else { } else {
int i = 0; int i = 0;
while (shared->data[shared->head] != -1) { while (shared->data[shared->head] != -1) {