-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.cpp
More file actions
53 lines (46 loc) · 1.63 KB
/
consumer.cpp
File metadata and controls
53 lines (46 loc) · 1.63 KB
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
#include <stdio.h>
#include <semaphore.h>
#include <unistd.h>
#include <iostream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
//create variables for shared memory and a table
int SMemory;
int* table;
// allocates a shared memory
SMemory = shm_open("table", O_CREAT | O_RDWR, 0666); // creates shared memory object -> table
ftruncate(SMemory, sizeof(int)); // size of shared memory
table = static_cast<int*>(mmap(0, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, SMemory, 0)); // maps object to the address
sem_t* full = sem_open("full", O_CREAT, 0666, 0); // creates semaphores
sem_t* empty = sem_open("empty", O_CREAT, 0666, 3);
sem_t* mutex = sem_open("mutex", O_CREAT, 0666, 1);
// iterate through the operation five times
for (int i = 0; i < 5; ++i) {
sem_wait(full);
sleep(1);
sem_wait(mutex); // unlock mutex
if (*table > 0) { // if the table has exisiting items = not empty:
--(*table);// consume an item in the table
std::cout << "An item has been consumed from the table." << std::endl << "The table now contains " << *table << " items\n";
}
else {
std::cout << "Table is empty!\n";
}
sem_post(mutex); // close the mutex
sem_post(empty);
}
// closes and unlinks the semaphores
sem_close(full);
sem_close(empty);
sem_close(mutex);
sem_unlink("full");
sem_unlink("empty");
sem_unlink("mutex");
// deallocate the shared memory
munmap(table, sizeof(int));
close(SMemory);
shm_unlink("table");
return 0;
}