-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.c
102 lines (100 loc) · 3.22 KB
/
consumer.c
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
// Kacper Dobek index 148247
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <time.h>
#include <stdbool.h>
#define N 5
int main()
{
bool first = true;
sem_t *Sp, *Sc, *Sq1, *Sq2, *Ccheck;
Sq1 = sem_open("/q1", O_CREAT, 0600, 1);
Sq2 = sem_open("/q2", O_CREAT, 0600, 1);
Sp = sem_open("/producers", O_CREAT, 0600, N);
Sc = sem_open("/consumers", O_CREAT, 0600, 0);
Ccheck = sem_open("/Ccheck", O_CREAT | O_EXCL, 0600, 0); // I use this semaphore with O_EXCL flag just to check if there are any producers already running
if (Ccheck == SEM_FAILED)
first = false;
if (Sc == SEM_FAILED || Sq1 == SEM_FAILED || Sq2 == SEM_FAILED)
{
perror("sem_open");
exit(1);
}
int q1_fd = shm_open("/q1fd", O_CREAT | O_RDWR, 0600); // the queue for producers
ftruncate(q1_fd, N * sizeof(int));
int *q1 = mmap(NULL, N * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, q1_fd, 0);
if (q1 == MAP_FAILED)
{
printf("Q1 mapping failed\n");
exit(1);
}
int q2_fd = shm_open("/q2fd", O_CREAT | O_RDWR, 0600); // the queue for consumers
ftruncate(q2_fd, N * sizeof(int));
int *q2 = mmap(NULL, N * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, q2_fd, 0);
if (q2 == MAP_FAILED)
{
printf("Q2 mapping failed\n");
exit(1);
}
int buffer_fd = shm_open("/main_buffer", O_CREAT | O_RDWR, 0600); // the main buffer for produced items
ftruncate(buffer_fd, N * sizeof(int));
int *buffer = mmap(NULL, N * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, buffer_fd, 0);
if (buffer == MAP_FAILED)
{
printf("Buffer mapping failed\n");
exit(1);
}
int pa_fd = shm_open("/pa", O_CREAT | O_RDWR, 0600); // the addition index of q1 (producers)
ftruncate(pa_fd, sizeof(int));
int *pa = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, pa_fd, 0);
int cr_fd = shm_open("/cr", O_CREAT | O_RDWR, 0600); // the removal index of q2 (consumers)
ftruncate(cr_fd, sizeof(int));
int *cr = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, cr_fd, 0);
srand(time(NULL));
if (first == true) // if no consumers already exist, set pa and cr to 0
{
*pa = 0;
*cr = 0;
}
while (1)
{
sem_wait(Sc);
sem_wait(Sq2);
int r = q2[*cr];
*cr = (*cr + 1) % N;
sem_post(Sq2);
sleep(rand() % 5 + 4);
int product = buffer[r];
printf("Consumed %d\n", product);
sem_wait(Sq1);
q1[*pa] = r;
*pa = (*pa + 1) % N;
sem_post(Sq1);
sleep(rand() % 4 + 2);
sem_post(Sp);
}
sem_close(Sp);
sem_close(Sc);
sem_close(Sq1);
sem_close(Sq2);
sem_close(Ccheck);
munmap(q1, N * sizeof(int));
munmap(buffer, N * sizeof(int));
munmap(q2, N * sizeof(int));
munmap(pa, sizeof(int));
munmap(cr, sizeof(int));
sem_unlink("/producers");
sem_unlink("/consumers");
sem_unlink("/q1");
sem_unlink("/q2");
shm_unlink("/q1fd");
shm_unlink("/q2fd");
shm_unlink("/main_buffer");
}