-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
42 lines (34 loc) · 736 Bytes
/
Copy pathmain.cpp
File metadata and controls
42 lines (34 loc) · 736 Bytes
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
#include <bits/stdc++.h>
#include <semaphore.h>
#include <unistd.h>
#include <thread>
using namespace std;
int items = 0;
sem_t notEmpty, notFull;
void* Producer() {
while (1) {
sem_wait(¬Full);
sleep(0.2);
cout << "Producer produces item.Items Present = " << ++items << endl;
sem_post(¬Empty);
sleep(0.5);
}
}
void* Consumer() {
while (1) {
sem_wait(¬Empty);
sleep(0.9);
cout << "Consumer consumes item.Items Present = " << --items << endl;
sem_post(¬Full);
sleep(1);
}
}
int main() {
sem_init(¬Empty, 0, 0);
sem_init(¬Full, 0, 1);
thread producer_thread(Producer);
thread consumer_thread(Consumer);
producer_thread.join();
consumer_thread.join();
return 0;
}